package com.bonait.bnframework.modules.home.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ConfigData;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_SUGAR;
import com.bonait.bnframework.common.db.res.wdszMode;
import com.bonait.bnframework.common.helper.I.IWriteCallBack;
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 com.suke.widget.SwitchButton;

import java.util.List;

public class zdqxchid_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater mLayoutInflater;

    private Context context;

    public List<BPA_SUGAR> zdqxModes = DataBus.getInstance().zdqx;
    public Activity activity;
    public zdqxchid_adapter(Context _context, Activity _activity) {
        this.context = _context;
        activity=_activity;
        mLayoutInflater = LayoutInflater.from(context);

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.zdqxchid_item, parent, false);
        return new ZDQXViewHolder(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try {
            if (holder instanceof ZDQXViewHolder) {
                ZDQXViewHolder myViewHolder = (ZDQXViewHolder) holder;
                BPA_SUGAR goodsMake= zdqxModes.get(position);
                String pssj="0.0";
                String qxsj="0.0";
                String temp="0";

                if(goodsMake.plcvar.contains("|"))
                {
                    String[] d= goodsMake.plcvar.split("[|]");
                    if(d.length>=3)
                    {
                        pssj=d[0];
                        qxsj=d[1];
                        temp=d[2];
                    }
                }
                myViewHolder.name.setText("步骤-"+(position+1));
                myViewHolder.edittext_pssj.setText(pssj+"");
                myViewHolder.edittext_xhsj.setText(qxsj+"");
                myViewHolder.edittext_jrsj.setText(temp+"");

                if(goodsMake.exp==null || goodsMake.exp.isEmpty())
                {
                    myViewHolder.status.setText("未开始");
                    myViewHolder.status.setTextColor(Color.parseColor("#99a0aa"));
                    myViewHolder.qxlc_layout.setBackgroundResource(R.color.activity_background1);
                }else
                {
                    myViewHolder.status.setText(goodsMake.exp);

                    if(goodsMake.exp.equals("执行中"))
                    {
                        myViewHolder.status.setTextColor(Color.parseColor("#00A8E1"));
                        myViewHolder.qxlc_layout.setBackgroundResource(R.color.selebottom);

                           if (!myViewHolder.edittext_pssj.getText().toString().equals("")) {
                                int k= (int)Math.round((Double.parseDouble(myViewHolder.edittext_pssj.getText().toString()) * 10));
                                ExecuteTheRecipe.WritePLC("排水阀排水时间",(short)k,null);
                                Thread.sleep(50);
                            }

                            if (!myViewHolder.edittext_xhsj.getText().toString().equals("")) {
                                int k= (int)Math.round((Double.parseDouble(myViewHolder.edittext_xhsj.getText().toString()) * 10));
                                ExecuteTheRecipe.WritePLC("循环清洗时间",(short)k,null);
                                Thread.sleep(50);
                            }

                            if (!myViewHolder.edittext_jrsj.getText().toString().equals("")) {
                                int k= (int)Math.round((Double.parseDouble(myViewHolder.edittext_jrsj.getText().toString()) * 10));
                                ExecuteTheRecipe.WritePLC("加热时间",(short)k,null);
                                Thread.sleep(50);
                            }

                            ExecuteTheRecipe.WritePLC("自动清洗开始", true,null);

                    }else //执行完成
                    {
                        myViewHolder.status.setTextColor(Color.parseColor("#FF4081"));
                        myViewHolder.qxlc_layout.setBackgroundResource(R.color.activity_background1);
                    }
                }

                myViewHolder.save_time.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(DataIs("排水阀排水时间",myViewHolder.edittext_pssj.getText().toString())
                        && DataIs("循环清洗时间",myViewHolder.edittext_xhsj.getText().toString()) && DataIs("加热水池时间",myViewHolder.edittext_jrsj.getText().toString()))
                        {
                            goodsMake.plcvar=myViewHolder.edittext_pssj.getText().toString()+"|"
                                    +myViewHolder.edittext_xhsj.getText().toString()+"|"
                                    +myViewHolder.edittext_jrsj.getText().toString();
                            QueryDB.UpdateSugar(goodsMake);
                            ToastUtils.info("保存成功！");
                        }
                    }
                });

                myViewHolder.delete_time.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String title = "温馨提示！";
                        String message = "客官确定要删除【"+"步骤-"+(position+1)+"】吗？";
                        AlertDialogUtils.showDialog(context, title, message, new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                QueryDB.DeleteSugar(goodsMake);
                                zdqxModes.remove(position);
                                notifyDataSetChanged();
                                dialog.dismiss();
                            }
                        });

                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean DataIs(String name,String data)
    {
        try
        {
            if(data==null || data.isEmpty())
            {
                ToastUtils.error(name+"不能为空！");
                return false;
            }

            Double.parseDouble(data);
            return true;
        }catch (Exception ex)
        {
            ToastUtils.error(name+"数据不合法！");
            return false;
        }
    }

    /**
     * 刷新
     * @param
     */
    public void refresh(){
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    notifyDataSetChanged();
                } catch (Exception e) {

                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return zdqxModes.size();
    }

    public static class ZDQXViewHolder extends RecyclerView.ViewHolder {
        EditText edittext_pssj;//排水阀排水时间
        EditText edittext_xhsj;//循环清洗时间
        EditText edittext_jrsj;//加热时间
        Button save_time;//保存时间

        Button delete_time;
        TextView name;//保存时间
        TextView status;//保存时间

        RelativeLayout qxlc_layout;//李总

        public ZDQXViewHolder(View view) {
            super(view);
            edittext_pssj =view.findViewById(R.id.edittext_pssj);
            edittext_xhsj =view.findViewById(R.id.edittext_xhsj);
            edittext_jrsj =view.findViewById(R.id.edittext_jrsj);
            save_time =view.findViewById(R.id.save_time);
            delete_time=view.findViewById(R.id.delete_time);
            name =view.findViewById(R.id.name);
            status =view.findViewById(R.id.status);

            qxlc_layout=view.findViewById(R.id.qxlc_layout);
        }
    }
}
