package com.bonait.bnframework.modules.home.fragment.mode;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.helper.I.MyClickListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class imagebuttom3 extends LinearLayout {

    @BindView(R.id.image_u)
    ImageView image_u;
    @BindView(R.id.image_layout)
    LinearLayout image_layout;
    @BindView(R.id.text_u)
    TextView text_u;
    private int ys0 = R.mipmap.sb2;
    private int ys1 = R.mipmap.sb1;
    private int ys2 = R.mipmap.sb3;

    /**
     * 0 1 2
     */
    public boolean Status = false;
    /**
     * 点击事件
     */
    public MyClickListener mListener = null;


    private View root;

    public imagebuttom3(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        root = LayoutInflater.from(context).inflate(R.layout.item_imagebuttomnew1, this);
        ButterKnife.bind(this, root);
        initParams(root.getContext(), attrs);
        Init();
    }

    private void initParams(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.imagebutton_control);
        if (typedArray != null) {
            try {
                ys0 = typedArray.getResourceId(R.styleable.imagebutton_control_imagesrc_qt, R.mipmap.sb2);
                ys1 = typedArray.getResourceId(R.styleable.imagebutton_control_imagesrc_ks, R.mipmap.sb1);
                ys2 = typedArray.getResourceId(R.styleable.imagebutton_control_imagesrc_tz, R.mipmap.sb3);
                typedArray.recycle();
            } catch (Exception exception) {
                Log.i("错误", exception.getMessage());
            }

        }
    }

    boolean IsEnable = true;
    boolean IsButtom = false;

    public void Init() {
        String str = this.getTag().toString();
        if (!str.isEmpty()) {
            if (str.contains("-按钮")) {
                IsButtom = true;
                text_u.setText(str.replace("-按钮", ""));
            } else {
                text_u.setText(str);
            }
        } else {
            text_u.setText("");
        }

        String str11 = this.getContentDescription().toString();
        if (!str11.isEmpty() && str11.contains("false")) {
            IsEnable = false;
        } else {
            IsEnable = true;
        }
        SetStatus(false);


    }

    /**
     * 设置
     * @param listener
     */
    public void SetOnTounch(OnTouchListener listener)
    {
        image_layout.setOnTouchListener(listener);
    }

    @OnClick({R.id.image_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_layout:
                if (IsEnable) {
                    Status = !Status;
                    SetStatus(Status);
                    if (mListener != null) {
                        mListener.clickListener(root, Status);
                    }
                }
                break;
        }
    }

    /**
     * 设置级别
     *
     * @param
     */
    public void SetStatus(boolean status) {
        Status = status;
        if (IsButtom) {
            if (IsEnable) {
                image_u.setImageResource(ys1);
            } else {
                image_u.setImageResource(ys0);
            }
        } else {
            if (IsEnable) {
                if (status) {
                    image_u.setImageResource(ys2);
                } else {
                    image_u.setImageResource(ys1);
                }
            } else {
                if (status) {
                    image_u.setImageResource(ys2);
                } else {
                    image_u.setImageResource(ys0);
                }
            }
        }

    }


    public void SetStatusKK(boolean status)
    {
        if (IsEnable) {
            if (status) {
                image_u.setImageResource(ys2);
            } else {
                image_u.setImageResource(ys1);
            }
        } else {
            if (status) {
                image_u.setImageResource(ys2);
            } else {
                image_u.setImageResource(ys0);
            }
        }
    }
}
