package com.example.bpa.view.from;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;

import com.example.bpa.Model.IWriteCallBack;
import com.example.bpa.R;
import com.example.bpa.service.DeviceData;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 电子秤校验
 */
public class dzcjy_activity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    //region 变量
    /**
     * 返回按钮
     */
    ImageView gongneng_fanhui;
    /**
     * 标题设置
     */
    TextView gongneng_title;
    /**
     * out_switch:称重模块校准
     * marialmode_switch:出口称重模块校准
     */
    Switch out_switch, marialmode_switch;
    /**
     * mwSave_button:物料砝码值保存
     * owSave_button:出口砝码值保存
     * materilwrite_button:物料砝码值写入
     * calibration_button:物料校准
     * zeroweight_button:重量清零
     * outWrite_button:出口砝码值写入
     * zerocalibration_button:零点校准
     * allcalibration_button:一键去皮
     */
    Button mwSave_button, owSave_button, materilwrite_button, calibration_button, zeroweight_button, outWrite_button, zerocalibration_button, allcalibration_button;

    EditText materialweight_edit, outweight_edit;

    Spinner values_spinner;

    EditText mode_1_realvalue, mode_2_realvalue, mode_3_realvalue, mode_4_realvalue, mode_5_realvalue, mode_6_realvalue, mode_7_realvalue, mode_8_realvalue, mode_out_realvalue;

    Map<String, Integer> datamaps = new LinkedHashMap<String, Integer>();

    //endregion

    //region 私有函数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dzcjy);
        Init();
        initEvents();
        Initdata();
        startListening();
    }
    //endregion

    //region 公共函数

    /**
     * 初始化
     */
    private void Init() {
        gongneng_fanhui = this.findViewById(R.id.gongneng_fanhui);
        gongneng_title = this.findViewById(R.id.gongneng_title);
        //通过Activity.getIntent()获取当前页面接收到的Intent。 getXxxExtra方法获取Intent传递过来的数据
        String msg = getIntent().getStringExtra("data");
        gongneng_title.setText(msg);
        //edittext
        materialweight_edit = this.findViewById(R.id.materialweight_edit);
        outweight_edit = this.findViewById(R.id.outweight_edit);
        //switch
        out_switch = this.findViewById(R.id.out_switch);
        marialmode_switch = this.findViewById(R.id.marialmode_switch);
        //button
        mwSave_button = this.findViewById(R.id.mwSave_button);
        owSave_button = this.findViewById(R.id.owSave_button);
        materilwrite_button = this.findViewById(R.id.materilwrite_button);
        calibration_button = this.findViewById(R.id.calibration_button);
        zeroweight_button = this.findViewById(R.id.zeroweight_button);
        outWrite_button = this.findViewById(R.id.outWrite_button);
        zerocalibration_button = this.findViewById(R.id.zerocalibration_button);

        values_spinner = this.findViewById(R.id.values_spinner);
        allcalibration_button = this.findViewById(R.id.allcalibration_button);
        //edit
        mode_1_realvalue = this.findViewById(R.id.mode_1_realvalue);
        mode_2_realvalue = this.findViewById(R.id.mode_2_realvalue);
        mode_3_realvalue = this.findViewById(R.id.mode_3_realvalue);
        mode_4_realvalue = this.findViewById(R.id.mode_4_realvalue);
        mode_5_realvalue = this.findViewById(R.id.mode_5_realvalue);
        mode_6_realvalue = this.findViewById(R.id.mode_6_realvalue);
        mode_7_realvalue = this.findViewById(R.id.mode_7_realvalue);
        mode_8_realvalue = this.findViewById(R.id.mode_8_realvalue);
        mode_out_realvalue = this.findViewById(R.id.mode_out_realvalue);


        datamaps.put("称重_1", 1);
        datamaps.put("称重_2", 2);
        datamaps.put("称重_3", 3);
        datamaps.put("称重_4", 4);
        datamaps.put("称重_5", 5);
        datamaps.put("称重_6", 6);
        datamaps.put("称重_7", 7);
        datamaps.put("称重_8", 8);


    }

    /**
     * 初始化按钮事件
     */
    private void initEvents() {
        gongneng_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dzcjy_activity.this.finish();
            }
        });
        mwSave_button.setOnClickListener(this);
        owSave_button.setOnClickListener(this);
        materilwrite_button.setOnClickListener(this);
        calibration_button.setOnClickListener(this);
        zeroweight_button.setOnClickListener(this);
        outWrite_button.setOnClickListener(this);
        zerocalibration_button.setOnClickListener(this);
        allcalibration_button.setOnClickListener(this);
        out_switch.setOnCheckedChangeListener(this);
        marialmode_switch.setOnCheckedChangeListener(this);


    }

    /**
     * 初始化数据
     */
    private void Initdata() {
        marialmode_switch.setChecked(DeviceData.Get().getCalibrationMode());
        out_switch.setChecked(DeviceData.Get().getWeightCalibrationMode());
        materialweight_edit.setText("0");
        outweight_edit.setText("0");
        //spinner绑定map数据
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                R.layout.spinner_text_item,
                new ArrayList<String>(datamaps.keySet()));
        adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
        values_spinner.setAdapter(adapter);

    }

    /**
     * 监听外部数据变化
     */
    private void startListening() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mode_1_realvalue.setText(String.valueOf(DeviceData.Get().getCallCurrentWeight(1)));
                                mode_2_realvalue.setText(String.valueOf(DeviceData.Get().getCallCurrentWeight(2)));
                                mode_3_realvalue.setText(String.valueOf(DeviceData.Get().getCallCurrentWeight(3)));
                                mode_4_realvalue.setText(String.valueOf(DeviceData.Get().getCallCurrentWeight(4)));
                                mode_5_realvalue.setText(String.valueOf(DeviceData.Get().getCallCurrentWeight(5)));
                                mode_6_realvalue.setText(String.valueOf(DeviceData.Get().getCallCurrentWeight(6)));
                                mode_7_realvalue.setText(String.valueOf(DeviceData.Get().getCallCurrentWeight(7)));
                                mode_8_realvalue.setText(String.valueOf(DeviceData.Get().getCallCurrentWeight(8)));
                                mode_out_realvalue.setText(String.valueOf(DeviceData.Get().getCallCurrentWeight(9)/10.0));
                            }
                        });

                        Thread.sleep(500);

                    } catch (Exception e) {

                    }
                }

            }
        }).start();
    }
    //endregion

    //region 点击事件

    /**
     * 本页面需要写入plc的点击事件监听
     * 开启新线程，防止阻塞UI线程
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        new Thread(() -> {
            switch (v.getId()) {
                case R.id.mwSave_button://物料砝码值保存
                    String mwres = materialweight_edit.getText().toString().trim();
                    if (!mwres.equals("")) {
                        DeviceData.Get().setCalibrationWeight(Short.parseShort(mwres), null);
                    }
                    break;
                case R.id.owSave_button://出口砝码值保存
                    String outres = outweight_edit.getText().toString().trim();
                    if (!outres.equals("")) {
                        DeviceData.Get().setCalibrationWeight9(Short.parseShort(outres), null);
                    }
                    break;
                case R.id.materilwrite_button://物料砝码值写入
                    String s = String.valueOf(values_spinner.getSelectedItem());
                    Integer value = datamaps.get(s);
                    DeviceData.Get().setWeightValue(true, value, null);
                    break;
                case R.id.calibration_button://物料校准
                    String s2 = String.valueOf(values_spinner.getSelectedItem());
                    Integer value2 = datamaps.get(s2);
                    DeviceData.Get().setRemovePeelCalibration(true, value2, null);
                    break;
                case R.id.zeroweight_button://重量清零
                    DeviceData.Get().setWeightClear(null);
                    break;
                case R.id.outWrite_button://出口砝码值写入
                    DeviceData.Get().setWeightValueWrite(null);
                    break;
                case R.id.zerocalibration_button://零点校准
                    DeviceData.Get().setZeroPointCalibration(null);
                    break;
                case R.id.allcalibration_button://一件去皮
                    DeviceData.Get().setOneClickPeeling(null);
                    break;

            }
        }).start();


    }

    /**
     * 本页面switch点击事件监听
     *
     * @param
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.marialmode_switch://称重模块校准
                DeviceData.Get().setCalibrationMode(isChecked, new IWriteCallBack() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onFailure(String ErrorMsg) {

                    }
                });

                break;
            case R.id.out_switch://出口称重模块校准
                DeviceData.Get().setWeightCalibrationMode(isChecked, new IWriteCallBack() {

                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onFailure(String ErrorMsg) {

                    }
                });
                break;
        }
    }
    //endregion
}