package com.example.iot_controlhost.model.controller.relay;

import com.example.iot_controlhost.utils.MyUtil;
import com.example.iot_controlhost.utils.global.AutoModelSet;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.global.SpinnerList;
import com.example.iot_controlhost.utils.log.MyLog;

/**
 * @Description 换气扇（总控所有换气扇）
 * @Author DuanKaiji
 * @CreateTime 2024年03月05日 15:33
 */
public class Fan extends Relay {

    Relay[] fans = new Relay[]{RoomController.intakeFan, RoomController.exhaustFan};

    public Fan(String name, int iconOpen, int iconClose) {
        super(name, iconOpen, iconClose);
    }

    @Override
    public String[] getModels() {
        return new String[]{"换气扇"};
    }


    @Override
    public boolean setPowerSupply(boolean target) {
        boolean result = false;
        String mode = AutoModelSet.getVentilatorMode();
        boolean intakeAvailable = fans[0].isAvailable();
        boolean exhaustAvailable = fans[1].isAvailable();
        MyLog.auto("换气扇分路解析：目标=" + (target ? "开启" : "关闭") + "，模式=" + mode
                + "，进气扇可用=" + intakeAvailable + "，端口=" + fans[0].getPorts()
                + "；排气扇可用=" + exhaustAvailable + "，端口=" + fans[1].getPorts());
        if (target) {
            if ((SpinnerList.VENTILATION_MODES[0].equals(mode)
                    || SpinnerList.VENTILATION_MODES[1].equals(mode)) && intakeAvailable) {
                boolean intakeResult = MyUtil.autoControlOperateThird(fans[0], true,false);
                MyLog.auto("换气扇分路执行：设备=进气扇，目标=开启，端口=" + fans[0].getPorts()
                        + "，结果=" + (intakeResult ? "成功" : "失败"));
                result = intakeResult;
            }
            if ((SpinnerList.VENTILATION_MODES[0].equals(mode)
                    || SpinnerList.VENTILATION_MODES[2].equals(mode)) && exhaustAvailable) {
                boolean exhaustResult = MyUtil.autoControlOperateThird(fans[1], true,false);
                MyLog.auto("换气扇分路执行：设备=排气扇，目标=开启，端口=" + fans[1].getPorts()
                        + "，结果=" + (exhaustResult ? "成功" : "失败"));
                result = exhaustResult || result;
            }
        } else {
            //防止出现“仅开进气扇-开启-设置为仅开排气扇-关闭”此时进气扇无法关闭的情况 所以每次都得全部关闭（各种换气扇）
            if (fans[0].isAvailable()) {
                boolean intakeResult = MyUtil.autoControlOperateThird(fans[0], false,false);
                MyLog.auto("换气扇分路执行：设备=进气扇，目标=关闭，端口=" + fans[0].getPorts()
                        + "，结果=" + (intakeResult ? "成功" : "失败"));
                result = intakeResult;
            }
            if (fans[1].isAvailable()) {
                boolean exhaustResult = MyUtil.autoControlOperateThird(fans[1], false,false);
                MyLog.auto("换气扇分路执行：设备=排气扇，目标=关闭，端口=" + fans[1].getPorts()
                        + "，结果=" + (exhaustResult ? "成功" : "失败"));
                result = exhaustResult || result;
            }
        }
        return result;
    }

    /**
     * 当前换气模式下是否至少存在一个可开启的分路。
     */
    public boolean hasAvailableFanForCurrentMode() {
        String mode = AutoModelSet.getVentilatorMode();
        return (SpinnerList.VENTILATION_MODES[0].equals(mode) && isAvailable())
                || (SpinnerList.VENTILATION_MODES[1].equals(mode) && fans[0].isAvailable())
                || (SpinnerList.VENTILATION_MODES[2].equals(mode) && fans[1].isAvailable());
    }

    @Override
    public boolean isPowerSupply() {
        return fans[0].isPowerSupply() || fans[1].isPowerSupply();
    }

    @Override
    public boolean isAvailable() {
        return fans[0].isAvailable() || fans[1].isAvailable();
    }

    @Override
    public double getVoltage() {
        double total = 0;
        int number = 0;
        if (fans[0].isAvailable()) {
            total += fans[0].getVoltage();
            number++;
        }
        if (fans[1].isAvailable()) {
            total += fans[1].getVoltage();
            number++;
        }
        return number == 0 ? 0 : total / number;
    }

    public double getCurrent() {
        double total = 0;
        int number = 0;
        if (fans[0].isAvailable()) {
            total += fans[0].getCurrent();
            number++;
        }
        if (fans[1].isAvailable()) {
            total += fans[1].getCurrent();
            number++;
        }
        return number == 0 ? 0 : total / number;
    }

    public double getPower() {
        double total = 0;
        if (fans[0].isAvailable()) {
            total += fans[0].getPower();
        }
        if (fans[1].isAvailable()) {
            total += fans[1].getPower();
        }
        return total;
    }

    public double getEnergyConsumption() {
        double total = 0;
        if (fans[0].isAvailable()) {
            total += fans[0].getEnergyConsumption();
        }
        if (fans[1].isAvailable()) {
            total += fans[1].getEnergyConsumption();
        }
        return total;
    }
}
