package com.desfate.chart;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.desfate.chart.ui.old.ParamConfigData;

public class MTPTabHost extends LinearLayout {

    private ColumnHorizontalScrollView mColumnHorizontalScrollView;
    private LinearLayout mRadioGroup_content;
    private Context context;
    private LinearLayout linear_lay;

    private MTPTabHostListener mtpTabHostListener;

    /**
     * 屏幕宽度
     */
    private int mScreenWidth = 0;
    /**
     * Item宽度
     */
    private int mItemWidth = 0;
    private String[] titles;
    /**
     * 当前选中的栏目
     */
    private int columnSelectIndex = 0;

    public int getColumnSelectIndex() {
        return columnSelectIndex;
    }

    public void setColumnSelectIndex(int columnSelectIndex) {
        this.columnSelectIndex = columnSelectIndex;
    }

    public MTPTabHost(Context context) {
        super(context);
        this.context = context;
        initViews();
    }

    public MTPTabHost(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initViews();
    }

    public MTPTabHost(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        initViews();
    }

    /**
     * 显示的tab名称
     *
     * @param titles 显示tab的名称
     * @param screem 是否横向全屏显示 默认横向显示4个item
     */
    public void show(String[] titles, boolean screem) {
        this.titles = titles;
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) this.context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
        mItemWidth = mScreenWidth / (screem ? titles.length : 4);// 一个Item宽度为屏幕的1/6

        mRadioGroup_content.removeAllViews();
        mColumnHorizontalScrollView.setParam((Activity) this.context, mRadioGroup_content);
        for (int i = 0; i < titles.length; i++) {
            LayoutParams params = new LayoutParams(mItemWidth, LayoutParams.WRAP_CONTENT);
            params.leftMargin = 5;
            params.rightMargin = 5;
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflater.inflate(R.layout.order_add, null);

            final TextView line = v.findViewById(R.id.line);
            line.setWidth(mItemWidth);

            TextView ordername = v.findViewById(R.id.order_names);
            ordername.setWidth(mItemWidth);
            ordername.setGravity(Gravity.CENTER);
            ordername.setPadding(5, 5, 5, 5);
            ordername.setText(titles[i]);
            ordername.setTag(i);
            ordername.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int tab_postion = (Integer) v.getTag();
                    int _tem = columnSelectIndex;
                    columnSelectIndex = tab_postion;
                    selectTab(tab_postion);
                    if (mtpTabHostListener != null) {
                        mtpTabHostListener.onTabChanged(tab_postion, _tem, v);
                    }
                }
            });
            mRadioGroup_content.addView(v, i, params);
        }
    }

    /**
     * 选择的Column里面的Tab
     */
    public void selectTab(int tab_postion) {
        for (int i = 0; i < this.titles.length; i++) {
            View childAt = mRadioGroup_content.getChildAt(i);
            TextView line = childAt.findViewById(R.id.line);
            TextView name = childAt.findViewById(R.id.order_names);
            line.setVisibility(View.INVISIBLE);
            name.setTextColor(this.context.getResources().getColor(R.color.p_global_write_color));
            name.setTextSize(13);
        }
        View checkView = mRadioGroup_content.getChildAt(tab_postion);
        if (checkView != null) {
            int k = checkView.getMeasuredWidth();
            int l = checkView.getLeft();
            int i2 = l + k / 2 - mScreenWidth / 2;
            mColumnHorizontalScrollView.smoothScrollTo(i2, 0);
            View vw = mRadioGroup_content.getChildAt(tab_postion);
            if(Configs.Chart_Color) {
                vw.findViewById(R.id.line).setBackgroundColor(this.context.getResources().getColor(R.color.manager_yellow_bg));
            }else{
                vw.findViewById(R.id.line).setBackgroundColor(this.context.getResources().getColor(R.color.p_bottom_g_color));
            }
            vw.findViewById(R.id.line).setVisibility(View.VISIBLE);
            if(Configs.Chart_Color) {
                ((TextView) vw.findViewById(R.id.order_names)).setTextColor(this.context.getResources().getColor(R.color.manager_yellow_bg));
            }else{
                ((TextView) vw.findViewById(R.id.order_names)).setTextColor(this.context.getResources().getColor(R.color.p_bottom_g_color));
            }
            if (ParamConfigData.DisplayMode == 2) {
                ((TextView) vw.findViewById(R.id.order_names)).setTextSize(13);
            } else {
                ((TextView) vw.findViewById(R.id.order_names)).setTextSize(15);
            }
        }
    }

    /**
     * mtp_tab_host.xml
     */
    private void initViews() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.chart_tab_host, null);
        // android.view.ViewGroup.LayoutParams layoutParams = getLayoutParams();
        // layoutParams.height =
        // android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
        // layoutParams.width =
        // android.view.ViewGroup.LayoutParams.MATCH_PARENT;
        // this.setLayoutParams(layoutParams);
        // this.setOrientation(LinearLayout.HORIZONTAL);
        linear_lay = view.findViewById(R.id.linear_lay);
        if(Configs.Chart_Color){
            linear_lay.setBackground(getResources().getDrawable(R.color.manager_body_bg));
        }
        addView(view);
        mColumnHorizontalScrollView = view.findViewById(R.id.mColumnHorizontalScrollView);
        mRadioGroup_content = view.findViewById(R.id.mRadioGroup_content);
    }

    public void setOnMtpTabHostListener(MTPTabHostListener mtpTabHostListener) {
        this.mtpTabHostListener = mtpTabHostListener;
    }

    /**
     * 获取当前View
     *
     * @return
     */
    public View getCurrentView() {
        if (mRadioGroup_content == null) {
            return null;
        }
        return mRadioGroup_content.getChildAt(columnSelectIndex);
    }

    public int getCurrentIndxe() {
        return columnSelectIndex;
    }

    /**
     * 当前对应的值
     *
     * @return
     */
    public String getCurrentValue() {
        if (titles == null) {
            return "";
        }
        return this.titles[columnSelectIndex];
    }

}
