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;

/**
 * @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;
        if (target) {
            if ((SpinnerList.VENTILATION_MODES[0].equals(AutoModelSet.getVentilatorMode())
                    || SpinnerList.VENTILATION_MODES[1].equals(AutoModelSet.getVentilatorMode()))
                    && fans[0].isAvailable()) {
                result = MyUtil.autoControlOperateThird(fans[0], true,false);
            }
            if ((SpinnerList.VENTILATION_MODES[0].equals(AutoModelSet.getVentilatorMode())
                    || SpinnerList.VENTILATION_MODES[2].equals(AutoModelSet.getVentilatorMode()))
                    && fans[1].isAvailable()) {
                result = MyUtil.autoControlOperateThird(fans[1], true,false) || result;
            }
        } else {
            //防止出现“仅开进气扇-开启-设置为仅开排气扇-关闭”此时进气扇无法关闭的情况 所以每次都得全部关闭（各种换气扇）
            if (fans[0].isAvailable()) {
                result = MyUtil.autoControlOperateThird(fans[0], false,false);
            }
            if (fans[1].isAvailable()) {
                result = MyUtil.autoControlOperateThird(fans[1], false,false) || result;
            }
        }
        return result;
    }

    @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() {
        int total = 0;
        int number = 0;
        if (fans[0].isAvailable()) {
            total += fans[0].getVoltage();
            number++;
        }
        if (fans[1].isAvailable()) {
            total += fans[1].getVoltage();
            number++;
        }
        return total / number;
    }

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

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

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