package com.unibuy.smartdevice.devices;

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

/*
    filepath:/dev/ttymxc1
    baudRate:9600
    filepath:/dev/ttymxc2
    baudRate:9600
    filepath:/dev/ttymxc3
    baudRate:9600
*/

public enum ComPort {
    //57600, N, 8, 1
    COM_PORT_1("/dev/ttymxc1",  57600, DeviceType.XINYUAN),
    COM_PORT_S1("/dev/ttyS7",  57600,  DeviceType.XINYUAN),
    COM_PORT_2("/dev/ttymxc2",  9600,  DeviceType.NFC_SCANNER),
    COM_PORT_S2("/dev/ttyS6",  9600,  DeviceType.NFC_SCANNER),
    COM_PORT_3("/dev/ttymxc3",  38400,  DeviceType.ELECTRIC),
    COM_PORT_4("/dev/ttymxc4",  38400,  DeviceType.UNKNOWN),
    COM_PORT_5("/dev/ttymxc5",  38400,  DeviceType.UNKNOWN),
    COM_PORT_6("/dev/ttymxc6",  38400,  DeviceType.DIGITAL_DISPLAY),
    COM_PORT_7("/dev/ttymxc7",  38400,  DeviceType.UNKNOWN);

    private final String filepath;
    private final int baudRate;
    private final DeviceType deviceType;

    // 靜態篩選方法
    public static List<ComPort> getPortsByDeviceType(DeviceType type) {
        List<ComPort> result = new ArrayList<>();
        for (ComPort port : ComPort.values()) {
            if (port.getDeviceType() == type) {
                result.add(port);
            }
        }
        return result;
    }

    ComPort(String filepath, int baudRate, DeviceType deviceType) {
        this.filepath = filepath;
        this.baudRate = baudRate;
        this.deviceType = deviceType;
    }

    public String getFilepath() {
        if (this == COM_PORT_S1) {
            if ("Cmuh".equalsIgnoreCase(com.unibuy.smartdevice.BuildConfig.FLAVOR_project)) {
                return "/dev/ttyS1";
            }
            return "/dev/ttyS7";
        }
        if (this == COM_PORT_S2) {
            if ("Cmuh".equalsIgnoreCase(com.unibuy.smartdevice.BuildConfig.FLAVOR_project)) {
                return "/dev/ttyS2";
            }
            return "/dev/ttyS6";
        }
        return filepath;
    }

    public int getBaudRate() {
        return baudRate;
    }

    public DeviceType getDeviceType() {
        return deviceType;
    }

    @Override
    public String toString() {
        return "ComPort{" +
                "filepath='" + filepath +
                ", baudRate=" + baudRate +
                ", deviceType=" + deviceType.getDescription() +
                '}';
    }
}
