package com.maxiee.heartbeat.ui;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.TypedValue;
import android.view.MenuItem;
import android.widget.TextView;

import com.maxiee.heartbeat.R;
import com.maxiee.heartbeat.common.TimeUtils;
import com.maxiee.heartbeat.database.utils.EventUtils;
import com.maxiee.heartbeat.database.utils.LabelUtils;
import com.maxiee.heartbeat.model.Event;
import com.maxiee.heartbeat.model.Label;
import com.maxiee.heartbeat.ui.adapter.EventListAdapter;
import com.maxiee.heartbeat.ui.common.BaseActivity;
import com.maxiee.heartbeat.ui.common.RecyclerInsetsDecoration;

import java.util.ArrayList;

/**
 * Created by maxiee on 15-6-27.
 */
public class LabelDetailActivity extends BaseActivity{

    private Toolbar mToolbar;
    private RecyclerView mRecyclerView;
    private TextView mLabelHint;
    private String mLabel;
    private ArrayList<Event> mEventList;
    private int mAccentColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_label_detail);

        Intent i = getIntent();

        mLabel = i.getStringExtra("tag_text");

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        setTitle("");

        mLabelHint = (TextView) findViewById(R.id.label_hint);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext()));
        mRecyclerView.addItemDecoration(new RecyclerInsetsDecoration(this));

        TypedValue accentValue = new TypedValue();
        getTheme().resolveAttribute(R.attr.colorAccent, accentValue, true);
        mAccentColor = accentValue.data;

        if (mLabel != null) {
            long labelKey = LabelUtils.hasLabel(this, mLabel);
            updateEventList(labelKey);
            showHintText();
        }
    }

    public void updateEventList(long labelKey) {
        // Temp Label
        Label label = new Label(labelKey, "");
        mEventList = EventUtils.getEvents(this, label);
        mRecyclerView.setAdapter(new EventListAdapter(mEventList));
    }

    public void showHintText() {
        if (mEventList.size() < 1) return;
        long earlistEventTime = mEventList.get(mEventList.size() - 1).getTimestamp();
        long latestEventTime = System.currentTimeMillis();
        ArrayList<String> hint = new ArrayList<>();
        hint.add(mLabel + "\n");
        hint.add(getString(R.string.label_hint_001));
        hint.add(TimeUtils.parseTime(this, earlistEventTime));
        hint.add(getString(R.string.label_hint_002));
        hint.add(String.valueOf(mEventList.size()));
        hint.add(getString(R.string.label_hint_003));
        hint.add(String.valueOf(
                TimeUtils.countDaysBetween(
                        earlistEventTime,
                        latestEventTime)));
        hint.add(getString(R.string.label_hint_004));
        String textToSpan = "";
        for (int i=0; i<hint.size(); i++) {
            textToSpan += hint.get(i);
        }
        Spannable spannable = new SpannableString(textToSpan);
        int offset = 0;
        for (int i=0; i<hint.size(); i++) {
            if (i == 0) {
                spannable.setSpan(
                        new RelativeSizeSpan(1.5f),
                        0,
                        mLabel.length(),
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                );
                spannable.setSpan(
                        new StyleSpan(Typeface.BOLD),
                        0,
                        mLabel.length(),
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                );
            }
            ForegroundColorSpan span;
            if (i==0 || i==2 || i==4 || i==6) {
                span = new ForegroundColorSpan(mAccentColor);
            } else {
                span = new ForegroundColorSpan(getResources().getColor(R.color.window_background));
            }
            int newOffset = offset + hint.get(i).length();
            spannable.setSpan(
                    span,
                    offset,
                    newOffset,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            );
            offset = newOffset;
        }
        mLabelHint.setText(spannable, TextView.BufferType.SPANNABLE);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            this.onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

@@ -490,40 +490,8 @@
 an;%0A
-import android.util.TypedValue;%0A
 impo
@@ -516,16 +516,16 @@
 nuItem;%0A
+
 import a
@@ -580,16 +580,63 @@
 beat.R;%0A
+import com.maxiee.heartbeat.common.ThemeUtils;%0A
 import c
@@ -1068,16 +1068,74 @@
 yList;%0A%0A
+import butterknife.Bind;%0Aimport butterknife.ButterKnife;%0A%0A
 /**%0A * C
@@ -1227,23 +1227,43 @@
 y%7B%0A%0A    
-private
+@Bind(R.id.toolbar)        
  Toolbar
@@ -1263,16 +1263,24 @@
 Toolbar 
+        
 mToolbar
@@ -1285,23 +1285,43 @@
 ar;%0A    
-private
+@Bind(R.id.recyclerview)   
  Recycle
@@ -1326,16 +1326,19 @@
 lerView 
+   
 mRecycle
@@ -1348,23 +1348,43 @@
 ew;%0A    
-private
+@Bind(R.id.label_hint)     
  TextVie
@@ -1385,16 +1385,23 @@
 extView 
+       
 mLabelHi
@@ -1404,16 +1404,17 @@
 elHint;%0A
+%0A
     priv
@@ -1674,16 +1674,48 @@
 detail);
+%0A        ButterKnife.bind(this);
 %0A%0A      
@@ -1740,17 +1740,16 @@
 tent();%0A
-%0A
         
@@ -1792,65 +1792,8 @@
 );%0A%0A
-        mToolbar = (Toolbar) findViewById(R.id.toolbar);%0A
     
@@ -1890,17 +1890,16 @@
 (true);%0A
-%0A
         
@@ -1917,143 +1917,8 @@
 );%0A%0A
-        mLabelHint = (TextView) findViewById(R.id.label_hint);%0A        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);%0A
     
@@ -2096,166 +2096,77 @@
     
-TypedValue accentValue = new TypedValue();%0A        getTheme().resolveAttribute(R.attr.colorAccent, accentValue, true);%0A        mAccentColor = accentValue.data
+mAccentColor = ThemeUtils.getAttributeColor(this, R.attr.colorAccent)
 ;%0A%0A 

remove unused code.