package com.kickstarter.ui.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kickstarter.R;
import com.kickstarter.libs.ApiCapabilities;
import com.kickstarter.libs.BaseActivity;
import com.kickstarter.libs.DiscoveryUtils;
import com.kickstarter.libs.KSColorUtils;
import com.kickstarter.libs.StatusBarUtils;
import com.kickstarter.libs.qualifiers.RequiresPresenter;
import com.kickstarter.models.Category;
import com.kickstarter.presenters.DiscoveryFilterPresenter;
import com.kickstarter.services.DiscoveryParams;
import com.kickstarter.ui.adapters.DiscoveryFilterAdapter;

import java.util.List;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.BindDrawable;
import butterknife.ButterKnife;
import butterknife.OnClick;

@RequiresPresenter(DiscoveryFilterPresenter.class)
public class DiscoveryFilterActivity extends BaseActivity<DiscoveryFilterPresenter> {
  DiscoveryFilterAdapter adapter;
  LinearLayoutManager layoutManager;

  @Bind(R.id.close_button) TextView closeButton;
  @Bind(R.id.discovery_filter_layout) RelativeLayout layout;
  @Bind(R.id.recycler_view) RecyclerView recyclerView;
  @BindColor(R.color.dark_blue_gradient_start) int darkBlueGradientStartColor;
  @BindColor(R.color.text_dark) int darkColor;
  @BindColor(R.color.white) int lightColor;
  @BindDrawable(R.drawable.dark_blue_gradient) Drawable darkBlueGradientDrawable;

  @Override
  protected void onCreate(@Nullable final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.discovery_filter_layout);
    ButterKnife.bind(this);

    layoutManager = new LinearLayoutManager(this);
    final DiscoveryParams discoveryParams = getIntent().getParcelableExtra(getString(R.string.intent_discovery_params));
    adapter = new DiscoveryFilterAdapter(presenter, discoveryParams);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);

    loadParams(discoveryParams);

    presenter.initialize(DiscoveryParams.builder().build()); // TODO: Replace with params from discovery intent
  }

  @OnClick(R.id.close_button)
  public void closeActivity() {
    onBackPressed();
  }

  public void loadCategories(@NonNull final List<Category> categories) {
    adapter.takeCategories(categories);
  }

  public void startDiscoveryActivity(@NonNull final DiscoveryParams newDiscoveryParams) {
    final Intent intent = new Intent().putExtra(getString(R.string.intent_discovery_params), newDiscoveryParams);
    setResult(Activity.RESULT_OK, intent);
    finish();
  }

  private void loadParams(@NonNull final DiscoveryParams params) {
    if (ApiCapabilities.canSetStatusBarColor() && ApiCapabilities.canSetDarkStatusBarIcons()) {
      if (params.isCategorySet()) {
        final Category category = params.category();
        StatusBarUtils.apply(this, category.secondaryColor(this), category.overlayShouldBeLight());
      } else {
        StatusBarUtils.apply(this, KSColorUtils.darken(darkBlueGradientStartColor, 0.1f), true);
      }
    }

    if (params.isCategorySet()) {
      recyclerView.setBackgroundColor(params.category().colorWithAlpha());
    } else {
      recyclerView.setBackground(darkBlueGradientDrawable);
    }

    closeButton.setTextColor(DiscoveryUtils.overlayTextColor(this, params));
  }
}

@@ -112,26 +112,119 @@
 oid.
-graphics.drawable.
+content.res.Configuration;%0Aimport android.graphics.drawable.Drawable;%0Aimport android.graphics.drawable.Gradient
 Draw
@@ -338,24 +338,73 @@
 n.Nullable;%0A
+import android.support.v4.content.ContextCompat;%0A
 import andro
@@ -492,16 +492,52 @@
 erView;%0A
+import android.widget.LinearLayout;%0A
 import a
@@ -834,32 +834,71 @@
 StatusBarUtils;%0A
+import com.kickstarter.libs.ViewUtils;%0A
 import com.kicks
@@ -1640,16 +1640,89 @@
 layout;%0A
+  @Bind(R.id.background_gradient) LinearLayout backgroundGradientLayout;%0A
   @Bind(
@@ -2601,115 +2601,455 @@
     
-presenter.initialize(DiscoveryParams.builder().build()); // TODO: Replace with params from discovery intent
+final RelativeLayout.LayoutParams layoutParams;%0A    if (ViewUtils.isPortrait(this)) %7B%0A      layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);%0A    %7D else %7B%0A      layoutParams = new RelativeLayout.LayoutParams(900, RelativeLayout.LayoutParams.MATCH_PARENT);%0A    %7D%0A    backgroundGradientLayout.setLayoutParams(layoutParams);%0A%0A    presenter.initialize(discoveryParams);
 %0A  %7D
@@ -4045,40 +4045,329 @@
     
-recyclerView.setBackgroundC
+layout.setBackgroundColor(params.category().colorWithAlpha());%0A      final Drawable backgroundDrawable = params.category().imageWithOrientation(this, getResources().getConfiguration().orientation);%0A      if (backgroundDrawable != null) %7B%0A        layout.setBackground(backgroundDrawable);%0A      %7D%0A%0A      final int c
 olor
-(
+ = 
 para
@@ -4389,53 +4389,337 @@
 olor
-WithAlpha());%0A    %7D else %7B%0A      recyclerView
+();%0A      final int%5B%5D gradientColors = new int%5B%5D %7BKSColorUtils.setAlpha(color, 242), KSColorUtils.setAlpha(color, 0)%7D;%0A      GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, gradientColors);%0A      backgroundGradientLayout.setBackground(gradientDrawable);%0A    %7D else %7B%0A      layout
 .set

Load category image in landscape, portrait