package com.maxiee.attitude.common.cloudview;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Pair;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RemoteViews;
import android.widget.TextView;

import com.maxiee.attitude.R;

import org.w3c.dom.Text;

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

/**
 * Created by maxiee on 15-6-24.
 */

@RemoteViews.RemoteView
public class CloudView extends ViewGroup {

    private static final int MAX_SIZE = 100;

    public CloudView(Context context) {
        super(context);
    }

    public CloudView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CloudView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void addLabels(List<Pair<String, Integer>> labels) {

        if (labels == null) {
            return;
        }

        float maxFreq = computeMaxFreq(labels);

        final TypedValue accentValue = new TypedValue();
        getContext().getTheme().resolveAttribute(R.attr.colorAccent, accentValue, true);
        int  accentColor = accentValue.data;

        final TypedValue grayValue = new TypedValue();
        getContext().getTheme().resolveAttribute(R.attr.colorPrimary, grayValue, true);
        int grayColor = grayValue.data;

        float redStep = (Color.red(accentColor) - Color.red(grayColor)) / maxFreq;
        float greenStep = (Color.green(accentColor) - Color.green(grayColor)) / maxFreq;
        float blueStep = (Color.blue(accentColor) - Color.blue(grayColor)) / maxFreq;

        for (Pair<String, Integer> label: labels) {
            TextView tagView = new TextView(getContext());
            tagView.setText(label.first);
            tagView.setTextSize(label.second / maxFreq * MAX_SIZE);
            tagView.setTextColor(
                    Color.rgb(
                            (int) (Color.red(grayColor) + redStep * label.second),
                            (int) (Color.green(grayColor) + greenStep * label.second),
                            (int) (Color.blue(grayColor) + blueStep * label.second)
                    )
            );
            LayoutParams layoutParams = new LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT
            );
            layoutParams.setMargins(5, 5, 5, 5);
            tagView.setLayoutParams(layoutParams);
            addView(tagView);
        }

    }

    private float computeMaxFreq(List<Pair<String, Integer>> labels) {
        float maxFreq = 1;

        for (Pair<String, Integer> label: labels) {
            if (label.second > maxFreq) {
                maxFreq = label.second;
            }
        }

        return maxFreq;
    }

    private float computeSize(float score) {
        return score * MAX_SIZE;
    }

    @Override
    public boolean shouldDelayChildPressedState() {
        return false;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int mViewGroupWidth = getMeasuredWidth();
        int mViewGroupHeight = getMeasuredHeight();

        int heightMax = 0;
        int mPainterPosX = l;
        int mPainterPosY = t;

        int childCount = getChildCount();
        for (int i=0; i<childCount; i++) {
            View childView = getChildAt(i);
            int width = childView.getMeasuredWidth();
            int height = childView.getMeasuredHeight();

            LayoutParams layoutParams = (LayoutParams) childView.getLayoutParams();

            if (height > heightMax) {
                heightMax = height;
            }

            if (mPainterPosX + layoutParams.leftMargin + width + layoutParams.rightMargin > mViewGroupWidth) {
                mPainterPosX = l;
                mPainterPosY += heightMax;
                heightMax = 0;
            }

            childView.layout(
                    mPainterPosX + layoutParams.leftMargin,
                    mPainterPosY + layoutParams.topMargin,
                    mPainterPosX + width + layoutParams.rightMargin,
                    mPainterPosY + height + layoutParams.bottomMargin
            );

            mPainterPosX += width + layoutParams.leftMargin + layoutParams.rightMargin;
        }
    }

    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new ViewGroup.LayoutParams(getContext(), attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        measureChildren(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    public static class LayoutParams extends MarginLayoutParams {

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(MarginLayoutParams source) {
            super(source);
        }
    }
}

@@ -355,62 +355,8 @@
 R;%0A%0A
-import org.w3c.dom.Text;%0A%0Aimport java.util.ArrayList;%0A
 impo
@@ -374,16 +374,16 @@
 l.List;%0A
+
 %0A/**%0A * 
@@ -529,16 +529,147 @@
 E = 100;
+%0A    private OnLabelClickListener mCallback;%0A%0A    public interface OnLabelClickListener %7B%0A        void onClick(String label);%0A    %7D
 %0A%0A    pu
@@ -953,32 +953,142 @@
 leAttr);%0A    %7D%0A%0A
+    public void setOnLabelClickListener(OnLabelClickListener callback) %7B%0A        mCallback = callback;%0A    %7D%0A%0A
     public void 
@@ -1886,32 +1886,38 @@
 ;%0A%0A        for (
+final 
 Pair%3CString, Int
@@ -2679,16 +2679,16 @@
  5, 5);%0A
-
         
@@ -2726,24 +2726,316 @@
 outParams);%0A
+            tagView.setOnClickListener(new OnClickListener() %7B%0A                @Override%0A                public void onClick(View v) %7B%0A                    if (mCallback != null) %7B%0A                        mCallback.onClick(label.first);%0A                    %7D%0A                %7D%0A            %7D);%0A
             

add a callback function.