package com.example.bpa.view.fragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.bpa.Model.IMessageLogNotify;
import com.example.bpa.Model.IReadCallBack;
import com.example.bpa.Model.IThread;
import com.example.bpa.R;
import com.example.bpa.app.ModbusTcpServer;
import com.example.bpa.helper.MessageLog;
import com.example.bpa.helper.ThreadManager;

/**
 * 系统设置界面
 */
public class SystemSetFragment extends Fragment {

    private Button ConBut;
    private Button ReadBut;
    private Button WriteBut;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_system_set, container, false);
        Init(view);
        return view;
    }

    private void Init(View tempView) {
        ConBut = tempView.findViewById(R.id.ConnectDevice);
        ReadBut = tempView.findViewById(R.id.ReadData);
        WriteBut = tempView.findViewById(R.id.WriteData);

        MessageLog.MsgNotify = new IMessageLogNotify() {
            @Override
            public void ErrorMsg(String msg) {
                Log.e("Error", msg);
            }

            @Override
            public void InfoMsg(String msg) {
                Log.i("Info", msg);
            }

            @Override
            public void WarnMsg(String msg) {
                Log.w("Warn", msg);
            }
        };

        ConBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ModbusTcpServer.get().Connect("192.168.1.9", 502);
                    }
                }).start();
            }
        });

        ReadBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ThreadManager.Get().StartLong("数据读取监听", true, new IThread() {
                    @Override
                    public void Run() throws InterruptedException {
                        ModbusTcpServer.get().ReadShort("0", 3, new IReadCallBack<short[]>() {
                            @Override
                            public void onSuccess(short[] shorts) {
                                for (int i = 0; i < shorts.length; i++) {
                                    MessageLog.ShowInfo("Shorts[" + i + "]=" + shorts[i]);
                                }
                            }
                        });

                        ModbusTcpServer.get().ReadFloat("10", 3, new IReadCallBack<float[]>() {
                            @Override
                            public void onSuccess(float[] floats) {
                                for (int i = 0; i < floats.length; i++) {
                                    MessageLog.ShowInfo("Floats[" + i + "]=" + floats[i]);
                                }
                            }
                        });

                        ModbusTcpServer.get().ReadBool("0", 3, new IReadCallBack<boolean[]>() {
                            @Override
                            public void onSuccess(boolean[] booleans) {
                                for (int i = 0; i < booleans.length; i++) {
                                    MessageLog.ShowInfo("Booleans[" + i + "]=" + booleans[i]);
                                }
                            }
                        });
                        Thread.sleep(3000);
                    }

                    @Override
                    public void RunComplete() throws InterruptedException {
                        MessageLog.ShowInfo("线程执行完成");
                    }
                });


            }
        });

        WriteBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ThreadManager.Get().Stop("数据读取监听");
                ModbusTcpServer.get().WriteBool("0", true);
                ModbusTcpServer.get().WriteBool("1", true);
                ModbusTcpServer.get().WriteBool("2", true);

                ModbusTcpServer.get().WriteShort("0", (short) 10);
                ModbusTcpServer.get().WriteShort("1", (short) 20);
                ModbusTcpServer.get().WriteShort("2", (short) 30);

                ModbusTcpServer.get().WriteFloat("10", 10.10f);
                ModbusTcpServer.get().WriteFloat("12", 20.20f);
                ModbusTcpServer.get().WriteFloat("14", 30.30f);
            }
        });

    }


}