package com.xamoom.android.xamoomcontentblocks.ViewHolders;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.ColorUtils;
import android.support.v4.util.LruCache;
import android.support.v7.widget.ContentFrameLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xamoom.android.xamoomcontentblocks.Adapters.ContentBlockAdapter;
import com.xamoom.android.xamoomcontentblocks.ListManager;
import com.xamoom.android.xamoomcontentblocks.XamoomContentFragment;
import com.xamoom.android.xamoomsdk.APIListCallback;
import com.xamoom.android.xamoomsdk.EnduserApi;
import com.xamoom.android.xamoomsdk.R;
import com.xamoom.android.xamoomsdk.Resource.Content;
import com.xamoom.android.xamoomsdk.Resource.ContentBlock;
import com.xamoom.android.xamoomsdk.Resource.Style;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import at.rags.morpheus.Error;

public class ContentBlock11ViewHolder extends RecyclerView.ViewHolder  {
  private TextView mErrorTextView;
  private TextView mTitleTextView;
  private RecyclerView mRecyclerView;
  private Button mLoadMoreButton;
  private ProgressBar mProgressBar;
  private EnduserApi mEnduserApi;
  private ContentBlockAdapter adapter;
  private ListManager mListManager;
  private Style mStyle;
  private ContentBlock mContentBlock;

  private String mCursor = null;
  private boolean hasMore = true;
  private boolean[] test = new boolean[1];

  public ContentBlock11ViewHolder(View view, Fragment fragment, EnduserApi enduserApi,
                                  ListManager listManager,
                                  XamoomContentFragment.OnXamoomContentFragmentInteractionListener
                                      onXamoomContentFragmentInteractionListener) {
    super(view);
    mEnduserApi = enduserApi;
    mListManager = listManager;

    mErrorTextView = (TextView) view.findViewById(R.id.content_list_error_text_view);
    mTitleTextView = (TextView) view.findViewById(R.id.content_list_title_text_view);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.content_list_recycler_view);
    mProgressBar = (ProgressBar) view.findViewById(R.id.content_list_progress_bar);
    mLoadMoreButton = (Button) view.findViewById(R.id.content_list_load_more_button);

    setupButton(fragment.getContext());
    setupRecyclerView(fragment, enduserApi, onXamoomContentFragmentInteractionListener);
  }

  private void setupButton(Context context) {
    // set color depending on colorAccent with default color as fallback (darkgray)
    // if style is set, it will use this color as button text color
    int color = getThemeColor(context, R.attr.colorAccent, mLoadMoreButton.getCurrentTextColor());
    if (mStyle != null && mStyle.getHighlightFontColor() != null) {
      color = Color.parseColor(mStyle.getHighlightFontColor());
    }
    mLoadMoreButton.setTextColor(color);

    mLoadMoreButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        showLoading();
        downloadContent(mContentBlock);
      }
    });
  }

  private void setupRecyclerView(Fragment fragment, EnduserApi enduserApi,
                                 XamoomContentFragment.OnXamoomContentFragmentInteractionListener onXamoomContentFragmentInteractionListener) {
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(fragment.getContext());
    layoutManager.setAutoMeasureEnabled(true);
    mRecyclerView.setLayoutManager(layoutManager);

    XamoomContentFragment contentFragment = (XamoomContentFragment) fragment;

    adapter = new ContentBlockAdapter(fragment, new ArrayList<ContentBlock>(),
        contentFragment.isShowSpotMapContentLinks(), contentFragment.getYoutubeApiKey(), contentFragment);
    adapter.setEnduserApi(enduserApi);
    adapter.setOnXamoomContentFragmentInteractionListener(onXamoomContentFragmentInteractionListener);
    mRecyclerView.setAdapter(adapter);
  }

  public void setupContentBlock(final ContentBlock contentBlock, boolean offline) {
    mContentBlock = contentBlock;
    mErrorTextView.setVisibility(View.GONE);
    mLoadMoreButton.setVisibility(View.GONE);

    adapter.setOffline(offline);

    clearContentBlocks();

    // set title text or remove the layoutParams to have same margins as when using a title
    if (contentBlock.getTitle() != null) {
      mTitleTextView.setText(contentBlock.getTitle());
    } else {
      LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mTitleTextView.getLayoutParams();
      params.setMargins(0,0,0,0);
      mTitleTextView.setLayoutParams(params);
    }

    ArrayList<Content> contents = mListManager.getContents(getAdapterPosition());
    if (contents != null) {
      hideLoading();
      displayContents(contents);
    } else {
      showLoading();
      downloadContent(contentBlock);
    }
  }

  private void downloadContent(ContentBlock contentBlock) {
    mListManager.downloadContent(getAdapterPosition(), contentBlock.getContentListTags(),
        contentBlock.getContentListPageSize(), new APIListCallback<List<Content>, List<Error>>() {
          @Override
          public void finished(List<Content> result, String cursor, boolean hasMore) {
            hideLoading();
            displayContents((ArrayList<Content>) result);
          }

          @Override
          public void error(List<Error> error) {
            hideLoading();
            mErrorTextView.setVisibility(View.VISIBLE);
          }
        });
  }

  private void showLoading() {
    mLoadMoreButton.setVisibility(View.GONE);
    mProgressBar.setVisibility(View.VISIBLE);
  }

  private void hideLoading() {
    mLoadMoreButton.setVisibility(View.VISIBLE);
    mProgressBar.setVisibility(View.GONE);
  }

  private void clearContentBlocks() {
    adapter.setContentBlocks(new ArrayList<ContentBlock>(0));
    adapter.notifyDataSetChanged();
  }

  @ColorInt
  private int getThemeColor(@NonNull final Context context, @AttrRes final int attributeColor, int fallback) {
    final TypedValue value = new TypedValue();
    value.data = fallback;
    context.getTheme().resolveAttribute (attributeColor, value, true);
    return value.data;
  }

  private void displayContents(ArrayList<Content> contents) {
    adapter.getContentBlocks().clear();
    for (Content content : contents) {
      ContentBlock block = new ContentBlock();
      block.setContentId(content.getId());
      block.setBlockType(6);
      adapter.getContentBlocks().add(block);
    }
    adapter.notifyDataSetChanged();
  }

  public void setStyle(Style style) {
    mStyle = style;
  }
}

@@ -7041,14 +7041,43 @@
  style;%0A
+    adapter.setStyle(style);%0A
   %7D%0A%7D%0A

Add style setting of adapter