package com.bjzc.bhzhgd.activity.sbxj;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bjzc.bhzhgd.DBModel.XSJJHDataBean;
import com.bjzc.bhzhgd.R;
import com.bjzc.bhzhgd.base.BaseFragment;
import com.bjzc.bhzhgd.weight.ListDialog;

import java.util.Arrays;
import java.util.List;

public class XJCJFragment extends BaseFragment {

    TextView tvBjmc;
    TextView tvSbmc;
    TextView tvZczt;
    TextView tvXcnr;
    TextView textView2;
    EditText etJg;
    TextView tvYjzj;
    TextView tvZt;
    Button xjetButton;
    EditText etYcFxnr;
    LinearLayout ycmsLl;


    private boolean isEdit;
    private XSJJHDataBean info;
    private int zj;
    private int index;
    private String writebleinfo;
    private String MRNR;

    public static XJCJFragment newInstance(boolean b, XSJJHDataBean info, int zj, int index) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("edit", b);
        bundle.putParcelable("info", info);
        bundle.putInt("zj", zj);
        bundle.putInt("index", index);
        XJCJFragment fragment = new XJCJFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_xjcj;
    }

    @Override
    protected void initView() {
        tvBjmc = view.findViewById(R.id.tv_bjmc);
        tvSbmc = view.findViewById(R.id.tv_sbmc);
        tvZczt = view.findViewById(R.id.tv_zczt);
        tvXcnr = view.findViewById(R.id.tv_xcnr);
        textView2 = view.findViewById(R.id.textView2);
        etJg = view.findViewById(R.id.et_jg);
        tvYjzj = view.findViewById(R.id.tv_yjzj);
        tvZt = view.findViewById(R.id.tv_zt);
        xjetButton = view.findViewById(R.id.xjet_button);
        etYcFxnr = view.findViewById(R.id.et_yc_fxnr);
        ycmsLl = view.findViewById(R.id.ycms_ll);

        Bundle bundle = getArguments();
        if (bundle != null) {
            isEdit = bundle.getBoolean("edit");
            info = bundle.getParcelable("info");
            zj = bundle.getInt("zj");
            index = bundle.getInt("index");
        }

        etJg.setEnabled(isEdit);
        xjetButton.setEnabled(isEdit);
        if(!isEdit){
            etYcFxnr.setEnabled(false);
        }
        updata(info, index, zj);
        tvBjmc.setMovementMethod(ScrollingMovementMethod.getInstance());
        tvZczt.setMovementMethod(ScrollingMovementMethod.getInstance());
        tvXcnr.setMovementMethod(ScrollingMovementMethod.getInstance());
        tvSbmc.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    @Override
    public void initData() {
        xjetButton.setOnClickListener(v -> {
            ListDialog dialog = new ListDialog(context, stringToList(MRNR), (text, position) -> {
                xjetButton.setText(text);
                etJg.setText(text);
                if (text.equals("正常")){
                    ycmsLl.setVisibility(View.GONE);
                }else if (text.equals("异常")){
                    ycmsLl.setVisibility(View.VISIBLE);
                }
            });
            dialog.show();
        });
    }


    public void updateState(String value) {
        etJg.setText(value);
        xjetButton.setText(value);
    }

    public void updatecheck(Boolean value) {
        if (value) {
            tvZt.setText("已检");
        } else {
            tvZt.setText("未检");
        }
    }
    public void updateFXNR(String value) { etYcFxnr.setText(value); }

    @SuppressLint("SetTextI18n")
    public void updata(XSJJHDataBean info, int item, int size) {
        tvBjmc.setText(info.getBJMC());
        tvSbmc.setText(info.getSb());

        tvZczt.setText(info.getZczt());
        tvXcnr.setText(info.getXcnr());
        MRNR = info.getMRNR();
        tvZt.setText(info.isChecked() ? "已检" : "未检");
        tvYjzj.setText(item + "/" + size);
        writebleinfo = info.getLRFS();
        if (info.getLRFS().equals("0")) {      //当时编辑状态的是 不需要按钮
            xjetButton.setVisibility(View.GONE);
            etJg.setVisibility(View.VISIBLE);
            ycmsLl.setVisibility(View.GONE);
            etYcFxnr.setText("");
            if (info.getCJJG() == null) {
                etJg.setText("");
            } else {
                switch (info.getCJJG()) {
                    case "已运行":
                        etJg.setText("");
                        break;
                    case "大小修":
                        etJg.setText("大小修");
                        break;
                    case "已停用":
                        etJg.setText("已停用");
                        break;
                    case "":
                        etJg.setText("");
                        break;
                    default:
                        etJg.setText(info.getCJJG());
                        break;
                }
            }
        } else if (info.getLRFS().equals("1")) {       //不是编辑状态的时候 需要按钮点击弹出菜单
            etJg.setVisibility(View.GONE);
            xjetButton.setVisibility(View.VISIBLE);
            etYcFxnr.setText("");
            if (info.getCJJG() == null) {
                xjetButton.setText("正常");
                etJg.setText("正常");
                ycmsLl.setVisibility(View.GONE);
            } else {
                switch (info.getCJJG()) {
                    case "已运行":
                        xjetButton.setText("正常");
                        etJg.setText("正常");
                        ycmsLl.setVisibility(View.GONE);
                        break;
                    case "大小修":
                        xjetButton.setText("大小修");
                        etJg.setText("大小修");
                        ycmsLl.setVisibility(View.GONE);
                        break;
                    case "已停用":
                        xjetButton.setText("已停用");
                        etJg.setText("已停用");
                        ycmsLl.setVisibility(View.GONE);
                        break;
                    case "异常":
                        ycmsLl.setVisibility(View.VISIBLE);
                        xjetButton.setText("异常");
                        etJg.setText("异常");
                        etYcFxnr.setText(info.getFxnr());
                        break;
                    case "":
                        xjetButton.setText("正常");
                        etJg.setText("正常");
                        ycmsLl.setVisibility(View.GONE);
                        break;
                    default:
                        xjetButton.setText(info.getCJJG());
                        etJg.setText(info.getCJJG());
                        ycmsLl.setVisibility(View.GONE);
                        break;
                }
            }
        }
    }

    public String getCJJG() { return etJg.getText().toString().trim(); }
    public String getYc_Fxnr() { return etYcFxnr.getText().toString().trim(); }

    public void getBlueToothData(String bluetoothdata) {
        if (writebleinfo.equals("0")) {
            etJg.setText(bluetoothdata);
        } else if (writebleinfo.equals("1")) {
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private List<String> stringToList(String strs) {
        String[] str = strs.split(";");
        return Arrays.asList(str);
    }
}
