package com.unibuy.smartdevice.ui;

import android.content.Context;
import android.os.Bundle;

import com.unibuy.smartdevice.AppCompatActivityAbstract;
import com.unibuy.smartdevice.MyApp;
import com.unibuy.smartdevice.controller.DevController;
import com.unibuy.smartdevice.controller.DevXinYuanController;
import com.unibuy.smartdevice.databinding.ActivitySystemSetBinding;
import com.unibuy.smartdevice.databinding.ActivityXyshjTestBinding;
import com.unibuy.smartdevice.devices.DevXinYuan;
import com.unibuy.smartdevice.devices.PortTools;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.tools.HandlerMainCountdown;
import com.unibuy.smartdevice.tools.ToastHandlerMain;


public class XyshjTestActivity extends AppCompatActivityAbstract {
    private ActivityXyshjTestBinding binding;
    private SetHandlerMainCountdown setHandlerMainCountdown;

    @Override
    protected Context setCtx() {
        return this;
    }

    @Override
    protected Class<?> setCls() {
        return getClass();
    }

    @Override
    protected HandlerMain setHandlerMain() {
        return new ToastHandlerMain(getCtx(), getLogs());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityXyshjTestBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        updateCheckBoxes();

        binding.buttonBack.setOnClickListener(v -> {
            MyApp.updateLog(getCtx());
            finish();
        });

        setHandlerMainCountdown = new SetHandlerMainCountdown(getHandlerMain());
    }

    public static class SetHandlerMainCountdown extends HandlerMainCountdown {
        public SetHandlerMainCountdown(HandlerMain handlerMain) {
            super(handlerMain);
        }

        @Override
        protected HandlerMain setHandlerMain() {
            return getSrcHandlerMain();
        }

        @Override
        protected void close(HandlerMain handlerMain) {
            handlerMain.start("SetHandlerMainCountdown", "可以測試");
        }

        @Override
        protected void execute(long countdown, HandlerMain handlerMain) {
        }

        @Override
        protected Class<?> setCls() {
            return getClass();
        }
    }

    private void updateCheckBoxes() {
        binding.checkbox1FFFF.setOnClickListener(v -> {
            binding.checkbox1FXXX.setChecked(true);
            binding.checkbox1XFXX.setChecked(true);
            binding.checkbox1XXFX.setChecked(true);
            binding.checkbox1XXXF.setChecked(true);

            MyApp.getInstance().getDevXinYuanController().addSendBuffer(MyApp.getInstance().getDevXinYuanController().getDevXinYuan().setMoneyType((byte) 0x00, (byte)0xFF, (byte)0xFF));
            setHandlerMainCountdown.start(5);
            getLogs().info("---- 1FFFF ----");
        });

        binding.checkbox1XXXX.setOnClickListener(v -> {
            binding.checkbox1FXXX.setChecked(false);
            binding.checkbox1XFXX.setChecked(false);
            binding.checkbox1XXFX.setChecked(false);
            binding.checkbox1XXXF.setChecked(false);

            MyApp.getInstance().getDevXinYuanController().addSendBuffer(MyApp.getInstance().getDevXinYuanController().getDevXinYuan().setMoneyType((byte) 0x00, (byte)0x00, (byte)0x00));
            setHandlerMainCountdown.start(5);
            getLogs().info("---- 1XXXX ----");
        });

        binding.checkbox1Set.setOnClickListener(v -> {
            int u = 0;

            if (binding.checkbox1FXXX.isChecked()) {
                u |= 8;
            }
            if (binding.checkbox1XFXX.isChecked()) {
                u |= 4;
            }
            if (binding.checkbox1XXFX.isChecked()) {
                u |= 2;
            }
            if (binding.checkbox1XXXF.isChecked()) {
                u |= 1;
            }

            MyApp.getInstance().getDevXinYuanController().addSendBuffer(MyApp.getInstance().getDevXinYuanController().getDevXinYuan().setMoneyType((byte) 0x00, (byte)0x00, (byte)u));
            setHandlerMainCountdown.start(5);
            getLogs().info("---- 1???? ----");
        });

        binding.checkbox2FFFF.setOnClickListener(v -> {
            binding.checkbox2FXXX.setChecked(true);
            binding.checkbox2XFXX.setChecked(true);
            binding.checkbox2XXFX.setChecked(true);
            binding.checkbox2XXXF.setChecked(true);

            MyApp.getInstance().getDevXinYuanController().addSendBuffer(MyApp.getInstance().getDevXinYuanController().getDevXinYuan().setMoneyType((byte) 0x01, (byte)0xFF, (byte)0xFF));
            setHandlerMainCountdown.start(5);
            getLogs().info("---- 2FFFF ----");
        });

        binding.checkbox2XXXX.setOnClickListener(v -> {
            binding.checkbox2FXXX.setChecked(false);
            binding.checkbox2XFXX.setChecked(false);
            binding.checkbox2XXFX.setChecked(false);
            binding.checkbox2XXXF.setChecked(false);

            MyApp.getInstance().getDevXinYuanController().addSendBuffer(MyApp.getInstance().getDevXinYuanController().getDevXinYuan().setMoneyType((byte) 0x01, (byte)0x00, (byte)0x00));
            setHandlerMainCountdown.start(5);
            getLogs().info("---- 2XXXX ----");
        });

        binding.checkbox2Set.setOnClickListener(v -> {
            int u = 0;

            if (binding.checkbox2FXXX.isChecked()) {
                u |= 8;
            }
            if (binding.checkbox2XFXX.isChecked()) {
                u |= 4;
            }
            if (binding.checkbox2XXFX.isChecked()) {
                u |= 2;
            }
            if (binding.checkbox2XXXF.isChecked()) {
                u |= 1;
            }

            MyApp.getInstance().getDevXinYuanController().addSendBuffer(MyApp.getInstance().getDevXinYuanController().getDevXinYuan().setMoneyType((byte) 0x01, (byte)0x00, (byte)u));
            setHandlerMainCountdown.start(5);
            getLogs().info("---- 2???? ----");
        });

        binding.checkboxGet1000.setOnClickListener(v -> {
            MyApp.getInstance().getDevXinYuanController().addSendBuffer(MyApp.getInstance().getDevXinYuanController().getDevXinYuan().giveChange(1000));
        });

        binding.checkboxGet500.setOnClickListener(v -> {
            MyApp.getInstance().getDevXinYuanController().addSendBuffer(MyApp.getInstance().getDevXinYuanController().getDevXinYuan().giveChange(500));
        });

        binding.checkboxGet100.setOnClickListener(v -> {
            MyApp.getInstance().getDevXinYuanController().addSendBuffer(MyApp.getInstance().getDevXinYuanController().getDevXinYuan().giveChange(100));
        });

        binding.checkboxGet50.setOnClickListener(v -> {
            MyApp.getInstance().getDevXinYuanController().addSendBuffer(MyApp.getInstance().getDevXinYuanController().getDevXinYuan().giveChange(50));
        });

        binding.checkboxGet10.setOnClickListener(v -> {
            MyApp.getInstance().getDevXinYuanController().addSendBuffer(MyApp.getInstance().getDevXinYuanController().getDevXinYuan().giveChange(10));
        });

        binding.checkboxGet5.setOnClickListener(v -> {
            MyApp.getInstance().getDevXinYuanController().addSendBuffer(MyApp.getInstance().getDevXinYuanController().getDevXinYuan().giveChange(5));
        });

        binding.checkboxGet1.setOnClickListener(v -> {
            MyApp.getInstance().getDevXinYuanController().addSendBuffer(MyApp.getInstance().getDevXinYuanController().getDevXinYuan().giveChange(1));
        });
    }
}