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

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
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.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 imagebutton_control extends LinearLayout {

    @BindView(R.id.image_u)
    ImageView image_u;

    @BindView(R.id.text_u)
    TextView text_u;
    private int ks_ys = R.mipmap.start;
    private int tz_ys = R.mipmap.stop;

    /**
     * 当前状态
     */
    public boolean Status=false;
    /**
     * 点击事件
     */
    public MyClickListener mListener=null;

    private View root;

    public imagebutton_control(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        root= LayoutInflater.from(context).inflate(R.layout.item_imagebutton, 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 {
                ks_ys = typedArray.getResourceId(R.styleable.imagebutton_control_imagesrc_ks, R.mipmap.start);
                tz_ys = typedArray.getResourceId(R.styleable.imagebutton_control_imagesrc_tz,R.mipmap.stop);
                typedArray.recycle();
            }catch (Exception exception)
            {
                Log.i("错误", exception.getMessage());
            }

        }
    }

    public void Init()
    {
        String str= this.getTag().toString();
        if(!str.isEmpty())
        {
            text_u.setText(str);
        }else
        {
            text_u.setText("");
        }

        SetStatus(false);
    }

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

    }

    /**
     * 设置级别
     * @param
     */
    public void SetStatus(boolean status)
    {
        Status=status;
        if(status)
        {
            image_u.setImageResource(ks_ys);
        }else
        {
            image_u.setImageResource(tz_ys);
        }
    }
}
