package com.kickstarter.ui.toolbars;

import android.content.Context;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.widget.TextView;

import com.kickstarter.KSApplication;
import com.kickstarter.R;
import com.kickstarter.libs.BaseActivity;
import com.kickstarter.libs.Environment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

public class KSToolbar extends Toolbar {
  protected @Nullable @Bind(R.id.title_text_view) TextView titleTextView;
  private final CompositeSubscription subscriptions = new CompositeSubscription();

  public KSToolbar(final @NonNull Context context) {
    super(context);
  }

  public KSToolbar(final @NonNull Context context, final @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public KSToolbar(final @NonNull Context context, final @Nullable AttributeSet attrs, final int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Nullable @OnClick(R.id.back_button)
  protected void backButtonClick() {
    if (getContext() instanceof BaseActivity) {
      ((BaseActivity) getContext()).back();
    } else {
      ((AppCompatActivity) getContext()).onBackPressed();
    }
  }

  protected @NonNull Environment environment() {
    return ((KSApplication) getContext().getApplicationContext()).component().environment();
  }

  /**
   * If the toolbar has a textview with id title_text_view, set its title.
   */
  public void setTitle(final @NonNull String title) {
    if (this.titleTextView != null) {
      this.titleTextView.setText(title);
    }
  }

  @CallSuper
  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();
    ButterKnife.bind(this);
  }

  @CallSuper
  @Override
  protected void onAttachedToWindow() {
    super.onAttachedToWindow();
  }

  @CallSuper
  @Override
  protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();

    this.subscriptions.clear();
  }

  protected final void addSubscription(final @NonNull Subscription subscription) {
    this.subscriptions.add(subscription);
  }
}

@@ -63,16 +63,79 @@
 ontext;%0A
+import android.graphics.Canvas;%0Aimport android.graphics.Paint;%0A
 import a
@@ -171,16 +171,16 @@
 lSuper;%0A
-
 import a
@@ -258,16 +258,65 @@
 llable;%0A
+import android.support.v4.content.ContextCompat;%0A
 import a
@@ -611,16 +611,111 @@
 ronment;
+%0Aimport com.kickstarter.libs.qualifiers.WebEndpoint;%0Aimport com.kickstarter.libs.utils.Secrets;
 %0A%0Aimport
@@ -980,16 +980,94 @@
 xtView;%0A
+%0A  private Paint backgroundPaint;%0A  private @WebEndpoint String webEndpoint;%0A%0A
   privat
@@ -1214,16 +1214,36 @@
 ontext);
+%0A%0A    init(context);
 %0A  %7D%0A%0A  
@@ -1355,16 +1355,36 @@
  attrs);
+%0A%0A    init(context);
 %0A  %7D%0A%0A  
@@ -1534,16 +1534,277 @@
 leAttr);
+%0A%0A    init(context);%0A  %7D%0A%0A  @Override%0A  protected void onDraw(final Canvas canvas) %7B%0A    super.onDraw(canvas);%0A%0A    if (!this.webEndpoint.equals(Secrets.WebEndpoint.PRODUCTION)) %7B%0A      canvas.drawRect(0, 0, getWidth(), getHeight(), this.backgroundPaint);%0A    %7D
 %0A  %7D%0A%0A  
@@ -2873,24 +2873,24 @@
 cription) %7B%0A
-
     this.sub
@@ -2922,11 +2922,300 @@
 on);%0A  %7D
+%0A%0A  private void init(final @NonNull Context context) %7B%0A    this.backgroundPaint = new Paint();%0A    this.backgroundPaint.setStyle(Paint.Style.FILL);%0A    this.backgroundPaint.setColor(ContextCompat.getColor(context, R.color.accent));%0A%0A    this.webEndpoint = environment().webEndpoint();%0A  %7D
 %0A%7D%0A

ENHANCE ❗️Visual Indication when you're not hitting prod (#334)