package com.example.bpa.view.from;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;

import com.example.bpa.Model.IWriteCallBack;
import com.example.bpa.R;
import com.example.bpa.app.DeviceData;
import com.example.bpa.db.QueryDB;
import com.example.bpa.db.mode.BPA_MATERIAL;
import com.example.bpa.db.mode.BPA_SILOS;
import com.example.bpa.helper.T;
import com.example.bpa.view.adapter.lsjy_adapter;
import com.example.bpa.view.adapter.wl_adapter;
import com.example.bpa.view.inteface.MyClickListener;
import com.example.bpa.view.mode.ResSilosMode;

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

/**
 * 流速校验
 */
public class lsjy_activity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    //region 变量
    /**
     * 返回按钮
     */
    ImageView gongneng_fanhui;
    /**
     * 标题设置
     */
    TextView gongneng_title;

//    LinearLayout mylayout;

    //通道校准值

    EditText passwayText_1,passwayText_2,passwayText_3,passwayText_4,passwayText_5,passwayText_6,passwayText_7,passwayText_8,passwayText_9,passwayText_10;

    EditText timeText;
    List<EditText> editTextLists  = new ArrayList<EditText>();
    //通道校准开关
    Switch Switch_1,Switch_2,Switch_3,Switch_4,Switch_5,Switch_6,Switch_7,Switch_8,Switch_9,Switch_10;
    List<Switch> switchLists = new ArrayList<Switch>();
    //开始校准
    Button StartButton;
    //保存
    Button SaveButton;


    /**
     * 表格显示
     */
//    ListView datatab;
    /**
     * 料仓数据
     */
//    ArrayList<ResSilosMode> resSilosModes=new ArrayList<>();




    //endregion

    //region 私有函数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lsjy);
        Init();
        initEvents();
        Initdata();
    }
    //endregion

    //region 公共函数

    /**
     * 初始化
     */
    private void Init(){
        gongneng_fanhui = this.findViewById(R.id.gongneng_fanhui);
        gongneng_title = this.findViewById(R.id.gongneng_title);
//        datatab= this.findViewById(R.id.datatab);
        //通过Activity.getIntent()获取当前页面接收到的Intent。 getXxxExtra方法获取Intent传递过来的数据
        String msg=getIntent().getStringExtra("data");
        gongneng_title.setText(msg);

//        mylayout = findViewById(R.id.lcparameter_layout);
//        mylayout.setBackgroundColor(Color.TRANSPARENT);
        //校准数值

        passwayText_1 = findViewById(R.id.passwayText_1);
        passwayText_2 = findViewById(R.id.passwayText_2);
        passwayText_3 = findViewById(R.id.passwayText_3);
        passwayText_4 = findViewById(R.id.passwayText_4);
        passwayText_5 = findViewById(R.id.passwayText_5);
        passwayText_6 = findViewById(R.id.passwayText_6);
        passwayText_7 = findViewById(R.id.passwayText_7);
        passwayText_8 = findViewById(R.id.passwayText_8);
        passwayText_9 = findViewById(R.id.passwayText_9);
        passwayText_10 = findViewById(R.id.passwayText_10);
        editTextLists = Arrays.asList(passwayText_1,passwayText_2,passwayText_3,passwayText_4,passwayText_5,passwayText_6,passwayText_7,passwayText_8,passwayText_9,passwayText_10);

        //校准开关
        Switch_1 = findViewById(R.id.Switch_1);
        Switch_2 = findViewById(R.id.Switch_2);
        Switch_3 = findViewById(R.id.Switch_3);
        Switch_4 = findViewById(R.id.Switch_4);
        Switch_5 = findViewById(R.id.Switch_5);
        Switch_6 = findViewById(R.id.Switch_6);
        Switch_7 = findViewById(R.id.Switch_7);
        Switch_8 = findViewById(R.id.Switch_8);
        Switch_9 = findViewById(R.id.Switch_9);
        Switch_10 = findViewById(R.id.Switch_10);

        //校准按钮
        StartButton = findViewById(R.id.StartButton);

        timeText = findViewById(R.id.edi_time);
        SaveButton = findViewById(R.id.savetime);

    }

    /**
     * 初始化按钮事件
     */
    private void initEvents() {
        gongneng_fanhui.setOnClickListener(this);
        //switch
        Switch_1.setOnCheckedChangeListener(this);
        Switch_2.setOnCheckedChangeListener(this);
        Switch_3.setOnCheckedChangeListener(this);
        Switch_4.setOnCheckedChangeListener(this);
        Switch_5.setOnCheckedChangeListener(this);
        Switch_6.setOnCheckedChangeListener(this);
        Switch_7.setOnCheckedChangeListener(this);
        Switch_8.setOnCheckedChangeListener(this);
        Switch_9.setOnCheckedChangeListener(this);
        Switch_10.setOnCheckedChangeListener(this);

        StartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DeviceData.Get().setChStartCalibration(new IWriteCallBack() {
                    @Override
                    public void onSuccess() {
                        T.show(lsjy_activity.this,"通道开始校准");
                    }

                    @Override
                    public void onFailure(String ErrorMsg) {
                        T.show(lsjy_activity.this,"通道开始校准失败");
                    }
                });
            }
        });

        SaveButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String stime = timeText.getText().toString();
                            if(stime.equals("")){return;}
                            Float ftime = Float.parseFloat(stime);
                            DeviceData.Get().setCalibrationReferenceTime(ftime,new IWriteCallBack() {
                                @Override
                                public void onSuccess() {
                                    T.show(lsjy_activity.this,"校准时间写入成功");
                                }

                                @Override
                                public void onFailure(String ErrorMsg) {
                                    T.show(lsjy_activity.this,"校准时间写入失败");
                                }
                            });
                        }catch (ArithmeticException e) {
                            e.printStackTrace();
                        }
                    }
                });


            }
        });
    }

    /**
     * 初始化数据加载
     */
    public void Initdata()
    {

        //初始化标准值数据
        try{
        for (int i = 0; i <10;i++){
            String s = String.valueOf(DeviceData.Get().getCalibrationValue(i+1));
            editTextLists.get(i).setText(s);
        }
        String time = String.valueOf(DeviceData.Get().getCalibrationReferenceTime());
        //校准时间
        timeText.setText(time);
        }catch(Exception e){

        }
    }
    //endregion

    //region 点击事件
    /**
     * 本页面点击事件监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gongneng_fanhui://返回按钮
                this.finish();
                break;
        }
    }
    int k=404;

    /**
     * 本页面switch点击事件监听
     *
     * @param
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.Switch_1:
                SwitchClick(1,isChecked);
                break;
            case R.id.Switch_2:
                SwitchClick(2,isChecked);
                break;
            case R.id.Switch_3:
                SwitchClick(3,isChecked);
                break;
            case R.id.Switch_4:
                SwitchClick(4,isChecked);
                break;
            case R.id.Switch_5:
                SwitchClick(5,isChecked);
                break;
            case R.id.Switch_6:
                SwitchClick(6,isChecked);
                break;
            case R.id.Switch_7:
                SwitchClick(7,isChecked);
                break;
            case R.id.Switch_8:
                SwitchClick(8,isChecked);
                break;
            case R.id.Switch_9:
                SwitchClick(9,isChecked);
                break;
            case R.id.Switch_10:
                SwitchClick(10,isChecked);
                break;
        }

    }

    //switch切换

    private void SwitchClick(int index, boolean isChecked){
        StringBuffer sb = new StringBuffer("PLC写入：");
        new Thread (new Runnable() {
            public void run() {
                if (isChecked){  //校准开关开启状态才会写入值


                    String value = editTextLists.get(index-1).getText().toString();
                    DeviceData.Get().setCalibrationValue(Short.parseShort(value),index, new IWriteCallBack() {
                        @Override
                        public void onSuccess() {
                            sb.append("通道");
                            sb.append(index);
                            sb.append("校准值写入成功；");
                        }

                        @Override
                        public void onFailure(String ErrorMsg) {
                            sb.append("通道");
                            sb.append(index);
                            sb.append("校准值写入失败；");
                        }
                    });
                }
                DeviceData.Get().setChCalibrationSwitch(isChecked, index, new IWriteCallBack() {
                    @Override
                    public void onSuccess() {
                        sb.append("通道");
                        sb.append(index);
                        sb.append("校准开关成功；");

                    }

                    @Override
                    public void onFailure(String ErrorMsg) {
                        sb.append("通道");
                        sb.append(index);
                        sb.append("校准开关失败；");

                    }
                });
            }
        });


        T.show(lsjy_activity.this, sb.toString());
    }


    /**
     * 接口方法，响应ListView按钮点击事件
     */
//    @Override
//    public void clickListener(View v,Object data) {
//        switch (v.getId())
//        {
//            case R.id.button_item://校验
//                k=k+1;
//                T.show(this,((ResSilosMode)data).id);
//                QueryDB.UpdateJYZ(((ResSilosMode)data).id,k);
//                Initdata();
//                break;
//        }
//    }
    //endregion
}