package com.maxiee.heartbeat.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.maxiee.heartbeat.R;
import com.maxiee.heartbeat.common.cloudview.CloudView;
import com.maxiee.heartbeat.database.utils.LabelUtils;
import com.maxiee.heartbeat.ui.LabelDetailActivity;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by maxiee on 15-6-24.
 */
public class LabelCloudFragment extends Fragment{

    private CloudView mCloudView;
    private NestedScrollView mCloudLayout;
    private RelativeLayout mEmptyLayout;
    private ImageView mImageEmpty;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_label_cloud, container, false);

        mCloudView = (CloudView) v.findViewById(R.id.cloud_view);
        mCloudLayout = (NestedScrollView) v.findViewById(R.id.cloud_layout);
        mEmptyLayout = (RelativeLayout) v.findViewById(R.id.empty);
        mImageEmpty = (ImageView) v.findViewById(R.id.image_empty);

        updateCloud();

        return v;
    }

    public void updateCloud() {

        Map<Long,Integer> allLabels = LabelUtils.getFreq(getActivity());
        List<Pair<String, Integer>> labels = new LinkedList<>();

        if (allLabels != null) {
            ArrayList<Map.Entry<Long, Integer>> list = new ArrayList<>(allLabels.entrySet());
            for (Map.Entry<Long, Integer> label: list) {
                labels.add(new Pair<String, Integer>(
                        LabelUtils.getLabelByLabelId(getActivity(), label.getKey()).getLabel(),
                        label.getValue()));
            }
        }

        if (!labels.isEmpty()) {
            mCloudLayout.setVisibility(View.VISIBLE);
            mEmptyLayout.setVisibility(View.GONE);
            mCloudView.addLabels(labels);
            mCloudView.setOnLabelClickListener(new CloudView.OnLabelClickListener() {
                @Override
                public void onClick(String label) {
                    Intent i = new Intent();
                    i.setAction(Intent.ACTION_MAIN);
                    i.setClass(getActivity(), LabelDetailActivity.class);
                    i.putExtra("tag_text", label);
                    startActivity(i);
                }
            });
        } else {
            mCloudLayout.setVisibility(View.GONE);
            mEmptyLayout.setVisibility(View.VISIBLE);
            Glide.with(getActivity()).load(R.drawable.empty_bg1).into(mImageEmpty);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        updateCloud();
    }
}

@@ -707,16 +707,74 @@
 l.Map;%0A%0A
+import butterknife.Bind;%0Aimport butterknife.ButterKnife;%0A%0A
 /**%0A * C
@@ -861,23 +861,43 @@
 t%7B%0A%0A    
-private
+@Bind(R.id.cloud_view)     
  CloudVi
@@ -895,24 +895,34 @@
   CloudView 
+          
 mCloudView;%0A
@@ -925,23 +925,43 @@
 ew;%0A    
-private
+@Bind(R.id.cloud_layout)   
  NestedS
@@ -970,16 +970,19 @@
 ollView 
+   
 mCloudLa
@@ -991,23 +991,43 @@
 ut;%0A    
-private
+@Bind(R.id.empty)          
  Relativ
@@ -1034,16 +1034,21 @@
 eLayout 
+     
 mEmptyLa
@@ -1061,25 +1061,55 @@
     
-private ImageView
+@Bind(R.id.image_empty)     ImageView          
  mIm
@@ -1233,25 +1233,24 @@
 nceState) %7B%0A
-%0A
         View
@@ -1321,25 +1321,24 @@
 false);%0A
-%0A
         
 mCloudVi
@@ -1333,276 +1333,32 @@
     
-mCloudView = (CloudView) v.findViewById(R.id.cloud_view);%0A        mCloudLayout = (NestedScrollView) v.findViewById(R.id.cloud_layout);%0A        mEmptyLayout = (RelativeLayout) v.findViewById(R.id.empty);%0A        mImageEmpty = (ImageView) v.findViewById(R.id.image_empty
+ButterKnife.bind(this, v
 );%0A%0A
@@ -2940,11 +2940,131 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public void onDestroyView() %7B%0A        super.onDestroyView();%0A        ButterKnife.unbind(this);%0A    %7D
 %0A%7D%0A

use butter knife.