package com.dahe.mylibrary.weight;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.dahe.mylibrary.R;
import com.dahe.mylibrary.utils.ToastUtils;

public class MySlideBar extends RelativeLayout implements View.OnClickListener {

    private boolean canSelect;
    private TextView tvT1;
    private TextView tvT2;
    private TextView tvT3;
    private TextView tvT4;
    private TextView tvC1;
    private TextView tvC2;
    private TextView tvC3;
    private TextView tvC4;
    private View view1;
    private View view2;

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

    public MySlideBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MySlideBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, 0, 0);
    }

    public MySlideBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);


    }

    private void init(Context context, AttributeSet attrs) {
        View inflate = View.inflate(context, R.layout.view_slide_bar, this);
        tvT1 = (TextView) findViewById(R.id.tv_title1);
        tvT2 = (TextView) findViewById(R.id.tv_title2);
        tvT3 = (TextView) findViewById(R.id.tv_title3);
        tvT4 = (TextView) findViewById(R.id.tv_title4);
        tvC1 = (TextView) findViewById(R.id.tv_conten1);
        tvC2 = (TextView) findViewById(R.id.tv_content2);
        tvC3 = (TextView) findViewById(R.id.tv_content3);
        tvC4 = (TextView) findViewById(R.id.tv_content4);
        view1 = (View) findViewById(R.id.view1);
        view2 = (View) findViewById(R.id.view2);
        LinearLayout ll1 = (LinearLayout) findViewById(R.id.ll1);
        LinearLayout ll2 = (LinearLayout) findViewById(R.id.ll2);
        LinearLayout ll3 = (LinearLayout) findViewById(R.id.ll3);
        LinearLayout ll4 = (LinearLayout) findViewById(R.id.ll4);
        ll1.setOnClickListener(this);
        ll2.setOnClickListener(this);
        ll3.setOnClickListener(this);
        ll4.setOnClickListener(this);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MySlideBar);
        int integer = a.getInteger(R.styleable.MySlideBar_proress, -1);
        canSelect = a.getBoolean(R.styleable.MySlideBar_canSelect, false);
        changeProress(integer,true);

        a.recycle();
    }

    @Override
    public void onClick(View v) {
        if (canSelect) {
            int id = v.getId();
            if (id == R.id.ll1) {
                changeProress(0,canSelect);
            } else if (id == R.id.ll2) {
                changeProress(1,canSelect);
            } else if (id == R.id.ll3) {
                changeProress(2,canSelect);
            } else if (id == R.id.ll4) {
                changeProress(3,canSelect);
            }
        }

    }

    public void setProress(int proress){
        changeProress(proress,canSelect);
    }

    private void changeProress(int integer,boolean canSelect){
        if (listener!=null&&canSelect){
            listener.onChange(integer);
        }
        if (0 == integer) {
            tvT1.setBackgroundResource(R.drawable.select_slide_title);
            tvT2.setBackgroundResource(R.drawable.select_slide_title);
            tvT3.setBackgroundResource(R.drawable.select_slide_title);
            tvT4.setBackgroundResource(R.drawable.select_slide_title);
            tvT1.setText("1");
            tvT2.setText("2");
            tvT3.setText("3");
            tvT4.setText("4");
            tvT1.setSelected(true);
            tvT2.setSelected(false);
            tvT3.setSelected(false);
            tvT4.setSelected(false);

            view1.setBackgroundResource(R.color.color_d5);
            view2.setBackgroundResource(R.color.color_d5);

            tvC1.setTextColor(getResources().getColor(R.color.mine_top_bg));
            tvC2.setTextColor(getResources().getColor(R.color.color_9));
            tvC3.setTextColor(getResources().getColor(R.color.color_9));
            tvC4.setTextColor(getResources().getColor(R.color.color_9));
        } else if (1 == integer) {
            tvT1.setBackgroundResource(R.drawable.ok);
            tvT2.setBackgroundResource(R.drawable.select_slide_title);
            tvT3.setBackgroundResource(R.drawable.select_slide_title);
            tvT4.setBackgroundResource(R.drawable.select_slide_title);
            tvT1.setText("");
            tvT2.setText("2");
            tvT3.setText("3");
            tvT4.setText("4");
            tvT2.setSelected(true);
            tvT3.setSelected(false);
            tvT4.setSelected(false);

            view1.setBackgroundResource(R.color.mine_top_bg);
            view2.setBackgroundResource(R.color.color_d5);

            tvC1.setTextColor(getResources().getColor(R.color.black));
            tvC2.setTextColor(getResources().getColor(R.color.mine_top_bg));
            tvC3.setTextColor(getResources().getColor(R.color.color_9));
            tvC4.setTextColor(getResources().getColor(R.color.color_9));
        } else if (2 == integer) {
            tvT1.setBackgroundResource(R.drawable.ok);
            tvT2.setBackgroundResource(R.drawable.ok);
            tvT3.setBackgroundResource(R.drawable.select_slide_title);
            tvT4.setBackgroundResource(R.drawable.select_slide_title);
            tvT1.setText("");
            tvT2.setText("");
            tvT3.setText("3");
            tvT4.setText("4");
            tvT3.setSelected(true);
            tvT4.setSelected(false);

            view1.setBackgroundResource(R.color.mine_top_bg);
            view2.setBackgroundResource(R.color.mine_top_bg);

            tvC1.setTextColor(getResources().getColor(R.color.black));
            tvC2.setTextColor(getResources().getColor(R.color.black));
            tvC3.setTextColor(getResources().getColor(R.color.mine_top_bg));
            tvC4.setTextColor(getResources().getColor(R.color.color_9));
        } else if (3 == integer) {
            tvT1.setBackgroundResource(R.drawable.ok);
            tvT2.setBackgroundResource(R.drawable.ok);
            tvT3.setBackgroundResource(R.drawable.ok);
            tvT4.setBackgroundResource(R.drawable.select_slide_title);
            tvT1.setText("");
            tvT2.setText("");
            tvT3.setText("");
            tvT4.setText("4");
            tvT4.setSelected(true);

            tvC1.setTextColor(getResources().getColor(R.color.black));
            tvC2.setTextColor(getResources().getColor(R.color.black));
            tvC3.setTextColor(getResources().getColor(R.color.black));
            tvC4.setTextColor(getResources().getColor(R.color.mine_top_bg));
        }
    }


    public interface OnChangeListener{
        void onChange(int progress);
    }

    private OnChangeListener listener;
    public void setOnChangeListener(OnChangeListener listener){
        this.listener = listener;
    }
}
