package com.sunzee.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import com.orhanobut.hawk.Hawk;
import com.sunzee.R;
import com.sunzee.utils.UiUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
 * 写代码看视频，看完一个视频，先写代码，不要拖，而不是全部看完再写
 */

/**
 * 1.继承viewGroup
 * 2.声明属性,,,getDimension使用float接收
 */
public class CustomViewGrounp extends ViewGroup implements CustomView.InformationLocation, View.OnClickListener {
    //存储数据
    private List<String> mData = new ArrayList<>();
    private Map<String, List<Float>> mIntegerMap = new LinkedHashMap<>();
    private Map<String, List<Float>> mIntegerMap1 = Hawk.get("location", mIntegerMap);
    private boolean ismove;
    private onClickCleaning onClickCleaning;

    public Map<String, List<Float>> getIntegerMap1() {
        return mIntegerMap1;
    }

    public void setTextList(List<String> data) {
        this.mData.clear();
        this.mData.addAll(data);
        setUpChildren();
    }

    private static final String TAG = "CustomViewGrounp";

    private void setUpChildren() {
        //先清空布局的子元素
        removeAllViews();
        //添加子view进来
        Log.d(TAG, "setUpChildren: " + mData.size());
        for (int i = 0; i < mData.size(); i++) {
            CustomView customView = new CustomView(getContext());
            customView.setId(UiUtil.getResId("cv_" + i, R.id.class));
            customView.setWidth(170);
            customView.setHeight(170);
            customView.setBackgroundResource(R.drawable.selector_language_bg);
            customView.setGravity(Gravity.CENTER);
            customView.setTextSize(20);
            customView.setText(mData.get(i));
            customView.setOnClickListener(this);
            //设置TextView的相关属性，编剧，位置，颜色等等。
            addView(customView);
            Log.d(TAG, "setUpChildren: ");
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            int groupWidth = getMaxWidth();
            int groupHeight = getTotalHeight();
            setMeasuredDimension(groupWidth, groupHeight);
        } else if (widthMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(getMaxWidth(), height);
        } else if (heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(width, getTotalHeight());
        }

    }

    public CustomViewGrounp(Context context) {
        this(context, null);
    }

    public CustomViewGrounp(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            CustomView childAt = (CustomView) getChildAt(i);
            childAt.setInformationLocation(this);
            childAt.layout(i, i, childAt.getMeasuredWidth(), childAt.getMeasuredHeight());
        }
    }

    private int getMaxWidth() {
        int count = getChildCount();
        int maxWidth = 0;
        for (int i = 0; i < count; i++) {
            int currentWidth = getChildAt(i).getMeasuredWidth();
            if (maxWidth < currentWidth) {
                maxWidth = currentWidth;
            }
        }
        return maxWidth;
    }

    private int getTotalHeight() {
        int count = getChildCount();
        int totalHeight = 0;
        for (int i = 0; i < count; i++) {
            totalHeight += getChildAt(i).getMeasuredHeight();
        }
        return totalHeight;
    }


    @Override
    public void setInformationLocationListener(View view, List<Float> list, boolean ismove) {
        this.ismove = ismove;
        Log.d(TAG, "setInformationLocationListener: "+view.getId());
        switch (view.getId()) {
            case R.id.cv_0:
                Log.d(TAG, "setInformationLocationListener: cv_0");
                mIntegerMap1.put("0", list);
                break;
            case R.id.cv_1:
                Log.d(TAG, "setInformationLocationListener: cv_1");
                mIntegerMap1.put("1", list);
                break;
            case R.id.cv_2:
                Log.d(TAG, "setInformationLocationListener: cv_2");
                mIntegerMap1.put("2", list);
                break;
            case R.id.cv_3:
                mIntegerMap1.put("3", list);
                break;
            case R.id.cv_4:
                mIntegerMap1.put("4", list);
                break;
            case R.id.cv_5:
                mIntegerMap1.put("5", list);
                break;
            case R.id.cv_6:
                mIntegerMap1.put("6", list);
                break;
            case R.id.cv_7:
                mIntegerMap1.put("7", list);
                break;
            case R.id.cv_8:
                mIntegerMap1.put("8", list);
                break;
            case R.id.cv_9:
                mIntegerMap1.put("9", list);
                break;
            case R.id.cv_10:
                mIntegerMap1.put("10", list);
                break;
            case R.id.cv_11:
                mIntegerMap1.put("11", list);
                break;
            case R.id.cv_12:
                mIntegerMap1.put("12", list);
                break;
            case R.id.cv_13:
                mIntegerMap1.put("13", list);
                break;
            case R.id.cv_14:
                mIntegerMap1.put("14", list);
                break;
            case R.id.cv_15:
                mIntegerMap1.put("15", list);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        Log.d(TAG, "onClick: "+ismove);
        if (!ismove) {
            switch (v.getId()) {
                case R.id.cv_0:
                    onClickCleaning.setOnCLickCleaning(1);
                    break;
                case R.id.cv_1:
                    onClickCleaning.setOnCLickCleaning(2);
                    break;
                case R.id.cv_2:
                    onClickCleaning.setOnCLickCleaning(3);
                    break;
                case R.id.cv_3:
                    onClickCleaning.setOnCLickCleaning(4);
                    break;
                case R.id.cv_4:
                    onClickCleaning.setOnCLickCleaning(5);
                    break;
                case R.id.cv_5:
                    onClickCleaning.setOnCLickCleaning(6);
                    break;
                case R.id.cv_6:
                    onClickCleaning.setOnCLickCleaning(7);
                    break;
                case R.id.cv_7:
                    onClickCleaning.setOnCLickCleaning(8);
                    break;
                case R.id.cv_8:
                    onClickCleaning.setOnCLickCleaning(9);
                    break;
                case R.id.cv_9:
                    onClickCleaning.setOnCLickCleaning(10);
                    break;
                case R.id.cv_10:
                    onClickCleaning.setOnCLickCleaning(11);
                    break;
                case R.id.cv_11:
                    onClickCleaning.setOnCLickCleaning(12);
                    break;
                case R.id.cv_12:
                    onClickCleaning.setOnCLickCleaning(13);
                    break;
                case R.id.cv_13:
                    onClickCleaning.setOnCLickCleaning(14);
                    break;
                case R.id.cv_14:
                    onClickCleaning.setOnCLickCleaning(15);
                    break;
                case R.id.cv_15:
                    onClickCleaning.setOnCLickCleaning(16);
                    break;
            }
        }
    }

    public void setOnClickCleaning(onClickCleaning onClickCleaning) {
        this.onClickCleaning = onClickCleaning;
    }

    //客户点击了，那么我们需要进行清洗
    public interface onClickCleaning {
        void setOnCLickCleaning(int i);
    }
}
