package com.kickstarter.ui.viewholders;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Pair;
import android.view.View;
import android.widget.TextView;

import com.kickstarter.KSApplication;
import com.kickstarter.R;
import com.kickstarter.libs.Money;
import com.kickstarter.libs.utils.DateTimeUtils;
import com.kickstarter.models.Project;
import com.kickstarter.models.Reward;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

public class RewardViewHolder extends KsrViewHolder {
  public @Bind(R.id.pledge_minimum) TextView minimumTextView;
  public @Bind(R.id.reward_backers_count) TextView backersCountTextView;
  public @Bind(R.id.reward_description) TextView descriptionTextView;
  public @Bind(R.id.estimated_delivery_date) TextView estimatedDeliveryTextView;
  public @Bind(R.id.green_overlay) View greenOverlayView;
  public @Bind(R.id.selected) TextView selectedTextView;
  public @Bind(R.id.limited) TextView limitedTextView;
  public @Bind(R.id.all_gone) TextView allGoneTextView;
  public @Bind(R.id.white_overlay) View whiteOverlayView;

  @Inject Money money;

  private final Context context;
  private final Delegate delegate;
  private Project project;
  private Reward reward;

  public interface Delegate {
    void rewardClicked(@NonNull final RewardViewHolder viewHolder, @NonNull final Reward reward);
  }

  public RewardViewHolder(@NonNull final View view, @NonNull final Delegate delegate) {
    super(view);
    this.delegate = delegate;
    this.context = view.getContext();

    ((KSApplication) context.getApplicationContext()).component().inject(this);
    ButterKnife.bind(this, view);
  }

  public void onBind(@NonNull final Object datum) {
    final Pair<Project, Reward> projectAndReward = (Pair<Project, Reward>) datum;
    project = projectAndReward.first;
    reward = projectAndReward.second;

    minimumTextView.setText(String.format(
      context.getString(R.string.Pledge_or_more),
      money.formattedCurrency(reward.minimum(), project.currencyOptions())));
    backersCountTextView.setText(String.format(
      context.getString(R.string._backers),
      Integer.toString(reward.backersCount()))); // check Integer formatting
    descriptionTextView.setText(reward.description());
    estimatedDeliveryTextView.setText(
      reward.estimatedDeliveryOn().toString(DateTimeUtils.estimatedDeliveryOn()));

    setRewardCardState();
  }

  public void setRewardCardState() {

    if (project.isBacking()) {
      view.setClickable(false);

      if (project.isBackingRewardId(reward.id())) {
        selectedTextView.setVisibility(View.VISIBLE);
        greenOverlayView.setVisibility(View.VISIBLE);
      } else {
        selectedTextView.setVisibility(View.GONE);
        greenOverlayView.setVisibility(View.INVISIBLE);
      }

    } else {
      view.setClickable(true);
    }

    if (reward.isAllGone()) {
      allGoneTextView.setVisibility(View.VISIBLE);
      limitedTextView.setVisibility(View.GONE);
      whiteOverlayView.setVisibility(View.VISIBLE);
      view.setClickable(false);
    } else if (reward.isLimited()) {
      allGoneTextView.setVisibility(View.GONE);
      limitedTextView.setVisibility(View.VISIBLE);
      limitedTextView.setText(String.format(context.getString(R.string.Limited_left_of),
        reward.remaining(),
        reward.limit()));
      whiteOverlayView.setVisibility(View.INVISIBLE);
      view.setClickable(true);
    }
  }

  @Override
  public void onClick(@NonNull final View view) {
    delegate.rewardClicked(this, reward);
  }
}

@@ -2440,478 +2440,173 @@
     
-setRewardCardState();%0A  %7D%0A%0A  public void setRewardCardState() %7B%0A%0A    if (project.isBacking()) %7B%0A      view.setClickable(false
+toggleAllGoneRewardView(
 );%0A
-%0A 
     
- if (project.isBackingRewardId(reward.id())) %7B%0A        selectedTextView.setVisibility(View.VISIBLE);%0A        greenOverlayView.setVisibility(View.VISIBLE);%0A      %7D else %7B%0A        selectedTextView.setVisibility(View.GONE);%0A        greenOverlayView.setVisibility(View.INVISIBLE
+toggleLimitedRewardView();%0A    toggleSelectedRewardView();%0A    toggleClickableReward(
 );%0A
-    
   %7D%0A%0A  
-  %7D else %7B%0A      view.setClickable(true);%0A    %7D%0A
+public void toggleAllGoneRewardView() %7B
 %0A   
@@ -2687,56 +2687,8 @@
 E);%0A
-      limitedTextView.setVisibility(View.GONE);%0A
     
@@ -2743,70 +2743,14 @@
     
-  view.setClickable(false);%0A    %7D else if (reward.isLimited())
+%7D else
  %7B%0A 
@@ -2795,16 +2795,153 @@
 w.GONE);
+%0A      whiteOverlayView.setVisibility(View.INVISIBLE);%0A    %7D%0A  %7D%0A%0A  public void toggleLimitedRewardView() %7B%0A    if (reward.isLimited()) %7B
 %0A      l
@@ -3135,57 +3135,664 @@
     
-  whiteOverlayView.setVisibility(View.INVISIBLE);
+%7D else %7B%0A      limitedTextView.setVisibility(View.GONE);%0A    %7D%0A  %7D%0A%0A  public void toggleSelectedRewardView() %7B%0A    if (project.isBackingRewardId(reward.id())) %7B%0A      greenOverlayView.setVisibility(View.VISIBLE);%0A      selectedTextView.setVisibility(View.VISIBLE);%0A    %7D else %7B%0A      greenOverlayView.setVisibility(View.INVISIBLE);%0A      selectedTextView.setVisibility(View.GONE);%0A    %7D%0A  %7D%0A%0A  public void toggleClickableReward() %7B%0A    if (project.isBacking()) %7B%0A      view.setClickable(false);%0A    %7D%0A    else if (!project.isLive()) %7B%0A      view.setClickable(false);%0A    %7D%0A    else if (reward.isAllGone()) %7B%0A      view.setClickable(false);%0A    %7D%0A    else %7B
 %0A   

refactor reward card view toggling for clarity