package com.example.bpa.view.fragment.setting;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;

import com.example.bpa.Model.IWriteCallBack;
import com.example.bpa.R;
import com.example.bpa.app.DeviceData;
import com.example.bpa.helper.T;

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

public class Fragment_lcparameter extends Fragment  implements CompoundButton.OnCheckedChangeListener {

    private View view;
    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;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_lcparameter, container,false);
        Init();
        InitEvent();
        InitData();
        return view;

    }

    private void Init(){
        mylayout = view.findViewById(R.id.lcparameter_layout);
        mylayout.setBackgroundColor(Color.TRANSPARENT);
        //校准数值

        passwayText_1 = view.findViewById(R.id.passwayText_1);
        passwayText_2 = view.findViewById(R.id.passwayText_2);
        passwayText_3 = view.findViewById(R.id.passwayText_3);
        passwayText_4 = view.findViewById(R.id.passwayText_4);
        passwayText_5 = view.findViewById(R.id.passwayText_5);
        passwayText_6 = view.findViewById(R.id.passwayText_6);
        passwayText_7 = view.findViewById(R.id.passwayText_7);
        passwayText_8 = view.findViewById(R.id.passwayText_8);
        passwayText_9 = view.findViewById(R.id.passwayText_9);
        passwayText_10 = view.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 = view.findViewById(R.id.Switch_1);
        Switch_2 = view.findViewById(R.id.Switch_2);
        Switch_3 = view.findViewById(R.id.Switch_3);
        Switch_4 = view.findViewById(R.id.Switch_4);
        Switch_5 = view.findViewById(R.id.Switch_5);
        Switch_6 = view.findViewById(R.id.Switch_6);
        Switch_7 = view.findViewById(R.id.Switch_7);
        Switch_8 = view.findViewById(R.id.Switch_8);
        Switch_9 = view.findViewById(R.id.Switch_9);
        Switch_10 = view.findViewById(R.id.Switch_10);

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

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

    //点击事件
    private void InitEvent(){
        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(view.getContext(),"通道开始校准");
                    }

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

        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(view.getContext(),"校准时间写入成功");
                                }

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


            }
        });

    }

    //初始化数据
    private void InitData(){
        //初始化标准值数据
        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);



    }

    //switch 事件
    @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;
        }
    }

    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(view.getContext(), sb.toString());
    }

}