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;
    /**
     * mwSave_button:物料砝码值保存
     * owSave_button:出口砝码值保存
     * materilwrite_button:物料砝码值写入
     * calibration_button:物料校准
     * zeroweight_button:重量清零
     * outWrite_button:出口砝码值写入
     * zerocalibration_button:零点校准
     * allcalibration_button:一键去皮
     */
    Button  owSave_button, zeroweight_button, outWrite_button, zerocalibration_button;

    EditText  outweight_edit;



    EditText mode_out_realvalue;


    //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();
    }

    @Override
    protected void onDestroy() {
        DeviceData.Get().setWeightCalibrationMode(false, new IWriteCallBack() {

            @Override
            public void onSuccess() {

            }

            @Override
            public void onFailure(String ErrorMsg) {

            }
        });
        super.onDestroy();
    }

    //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
        outweight_edit = this.findViewById(R.id.outweight_edit);
        //switch
        out_switch = this.findViewById(R.id.out_switch);
        //button
        owSave_button = this.findViewById(R.id.owSave_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);
        //edit
        mode_out_realvalue = this.findViewById(R.id.mode_out_realvalue);
    }

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

    /**
     * 初始化数据
     */
    private void Initdata() {
        out_switch.setChecked(DeviceData.Get().getWeightCalibrationMode());
        outweight_edit.setText("0");
    }

    /**
     * 监听外部数据变化
     */
    private void startListening() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mode_out_realvalue.setText(String.valueOf(DeviceData.Get().getCallCurrentWeight(1)/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.owSave_button://出口砝码值保存
                    String outres = outweight_edit.getText().toString().trim();
                    if (!outres.equals("")) {
                        DeviceData.Get().setCalibrationWeight9(Short.parseShort(outres), 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;

            }
        }).start();


    }

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

                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onFailure(String ErrorMsg) {

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