package com.unibuy.smartdevice.ui;

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.unibuy.smartdevice.AppCompatActivityAbstract;
import com.unibuy.smartdevice.MyApp;
import com.unibuy.smartdevice.databinding.ActivitySettingTestBinding;
import com.unibuy.smartdevice.devices.SerialPort;
import com.unibuy.smartdevice.devices.UsbDev;
import com.unibuy.smartdevice.exception.Logs;
import com.unibuy.smartdevice.exception.LogsUnsupportedOperationException;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.tools.ToastHandlerMain;
import com.unibuy.smartdevice.tools.Tools;
import com.unibuy.smartdevice.ui.recycler.RecyclerSettingListAdpter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SettingTestActivity extends AppCompatActivityAbstract {
    private ActivitySettingTestBinding binding;
    private List<String> statusList = new ArrayList<>();
    private RecyclerSettingListAdpter recyclerSettingListAdpter;

    @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);
//        EdgeToEdge.enable(this);
        binding = ActivitySettingTestBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        recyclerSettingListAdpter = new RecyclerSettingListAdpter(statusList);
        binding.recyclerSettingList.setAdapter(recyclerSettingListAdpter);
        binding.recyclerSettingList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        binding.buttonBack.setOnClickListener(v -> finish());
        binding.buttonTest.setOnClickListener(v -> checkSetting());
    }

    private void checkSetting() {
        statusList.add("ByXinYuan comPort:" + MyApp.getInstance().getComPort1ByXinYuan());
        statusList.add("NFCPay comPort:" + MyApp.getInstance().getComPort2ByNFCPay());

        HashMap<String, UsbDevice> deviceList = UsbDev.getUsbManager().getDeviceList();

        if (deviceList.isEmpty()) {
            statusList.add("No USB devices connected");
        } else {
            for (UsbDevice device : deviceList.values()) {
                int vendorId = device.getVendorId();
                int productId = device.getProductId();

                String usbStatus = "Usb DN: " + device.getDeviceName();
                usbStatus += " PN: " + device.getProductName();
                usbStatus += " MN: " + device.getManufacturerName();
                usbStatus += " VID: " + vendorId;
                usbStatus += " PID: " + productId;
                statusList.add(usbStatus);

                String usbStatusPort =  SerialPort.getPortsByVendorInfo(vendorId, productId).toString();
                statusList.add(usbStatusPort);
            }
        }

        if (!MyApp.getInstance().getMachine().getMachineID().isEmpty()) {
            statusList.add("download maching data:"
                + MyApp.getInstance().getMachine().getMachineID()
                +"/"
                +Tools.randomReplace(MyApp.getInstance().getMachine().getApiKey(), 0.3)
            );
        }

        if (MyApp.getInstance().getGreenInvoice().getGreenMerchantID() != null) {
            statusList.add("download greenpay Invoice data:"
                    +Tools.randomReplace(String.valueOf(MyApp.getInstance().getGreenInvoice().getGreenMerchantID()), 0.3)
                    +"/"
                    +Tools.randomReplace(String.valueOf(MyApp.getInstance().getGreenInvoice().getGreenHashKey()), 0.3)
                    +"/"
                    +Tools.randomReplace(String.valueOf(MyApp.getInstance().getGreenInvoice().getGreenHashIV()), 0.3)
                    +"/"
                    +Tools.randomReplace(String.valueOf(MyApp.getInstance().getGreenInvoice().getGreenEmail()), 0.3)
            );
        }

        if (!MyApp.getInstance().getEsunpay().getStoreID().isEmpty()) {
            statusList.add("download esunpay data:"
                +Tools.randomReplace(MyApp.getInstance().getEsunpay().getStoreID(), 0.3)
                +"/"
                +Tools.randomReplace(MyApp.getInstance().getEsunpay().getTermID(), 0.3)
                +"/"
                +Tools.randomReplace(MyApp.getInstance().getEsunpay().getHash(), 0.3)
            );
        }

        if (MyApp.getInstance().getGreenpaySetting().getAppKey() != null && !MyApp.getInstance().getGreenpaySetting().getAppKey().isEmpty()) {
            statusList.add("download greenpay data:"
                    +Tools.randomReplace(String.valueOf(MyApp.getInstance().getGreenpaySetting().getAppId()), 0.3)
                    +"/"
                    +Tools.randomReplace(MyApp.getInstance().getGreenpaySetting().getAppKey(), 0.3)
                    +"/"
                    +Tools.randomReplace(MyApp.getInstance().getGreenpaySetting().getPartnerKey(), 0.3)
            );

            statusList.add("download greenpay merchant data:"
                    +Tools.randomReplace(String.valueOf(MyApp.getInstance().getGreenpayMerchant().getLineMerchantId()), 0.3)
                    +"/"
                    +Tools.randomReplace(MyApp.getInstance().getGreenpayMerchant().getPsMerchantId(), 0.3)
                    +"/"
                    +Tools.randomReplace(MyApp.getInstance().getGreenpayMerchant().getEasyMerchantId(), 0.3)
                    +"/"
                    +Tools.randomReplace(MyApp.getInstance().getGreenpayMerchant().getPiMerchantId(), 0.3)
                    +"/"
                    +Tools.randomReplace(MyApp.getInstance().getGreenpayMerchant().getJkoMerchantId(), 0.3)
            );
        }

        if (!MyApp.getInstance().getProductList().isEmpty()) {
            int size = MyApp.getInstance().getProductList().size();
            statusList.add("download product data:" + size);
        }

        if (!MyApp.getInstance().getFontendAdUrlString().isEmpty()) {
            int size = MyApp.getInstance().getFontendAdUrlString().size();
            statusList.add("download ad url data:" + size);
        }

        recyclerSettingListAdpter.notifyItemInserted(statusList.size() -1);
    }
}