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.TextView;

import androidx.annotation.Nullable;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

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

/**
 * Image 两个状态空间
 */
public class imagebuttom extends LinearLayout {

    @BindView(R.id.image_u)
    ImageView image_u;
    private int ks_ys = R.mipmap.zhizuo_ks;
    private int tz_ys = R.mipmap.zhizuo_js;
    /**
     * 当前状态
     */
    public boolean Status=false;
    /**
     * 点击事件
     */
    public MyClickListener mListener=null;

    private View root;

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

        }
    }

    public void Init()
    {
        SetStatus(false);
    }

    @OnClick({R.id.image_u})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_u:
                if(ks_ys==R.mipmap.zhizuo_ks)
                {
                    if(Status)
                    {
                        String title = "停止操作提示！";
                        String message = "请问客官确定要停止制作吗,小菠萝会生气的,啊啊啊啊啊啊啊...我的饭饭？";
                        AlertDialogUtils.showDialog(getContext(), title, message, new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                //强制结束
                                ExecuteTheRecipe.SetForcedEnd();
                                if(mListener!=null)
                                {
                                    mListener.clickListener(root,false);
                                }
                                dialog.dismiss();
                            }
                        });

                    }else
                    {
                        if (!ConfigName.getInstance().PlcIsConnect) {
                            ToastUtils.warning("PLC未准备就绪!!!");
                            return;
                        }

                        Object chushiover = ExecuteTheRecipe.getListingValue("初始化完成");
                        if (chushiover == null || !(boolean) chushiover) {
                            ToastUtils.warning("设备未初始化,请先手动初始化设备!!!");
                            return;
                        }

                        String title = "开始操作提示！";
                        String message = "请问客官确定要开始制作吗,小菠萝好开心呀,马上就有好吃的耶？";
                        AlertDialogUtils.showDialog(getContext(), title, message, new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                ExecuteTheRecipe.StopForcedEnd();
                                Status=!Status;
                                SetStatus(Status);
                                if(mListener!=null)
                                {
                                    mListener.clickListener(root,Status);
                                }
                                dialog.dismiss();
                            }
                        });
                    }
                }else
                {
                    Status=!Status;
                    SetStatus(Status);
                    if(mListener!=null)
                    {
                        mListener.clickListener(root,Status);
                    }
                }
                break;
        }


    }

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