package com.example.iot_controlhost.utils.control.infrared

import com.example.iot_controlhost.model.controller.AirConditionInfrared
import com.example.iot_controlhost.utils.global.RoomController

class MyInfraredUtils : AirConditionInfrared("空调红外总控") {

    companion object {
        val infraredList = listOf(
            RoomController.airConditionInfrared,
            RoomController.airConditionInfrared2,
        )

        val instance: MyInfraredUtils by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            MyInfraredUtils()
        }

        @JvmStatic
        fun getInstances(): MyInfraredUtils {
            return instance
        }
    }

    override fun getState(): String {
        return if (infraredList[1].isEnable) {
            "空调1：${infraredList[0].getState()}\n" + "空调2：${infraredList[1].getState()}"
        } else {
            infraredList[0].getState()
        }
    }

    override fun isPowerSupply(): Boolean {
        val result = mutableListOf<Boolean>()
        for (infrared in infraredList) {
            if (infrared.isEnable) {
                result.add(infrared.isPowerSupply())
            }
        }
        // 如果所有的空调都是开机状态，返回true
        return result.all { it }
    }

    override fun getMode(): Int {
        // 因为一起控制，所以只需要返回第一个的模式即可
        return infraredList[0].getMode()
    }

    override fun getModel(): String {
        val result = mutableListOf<String>()
        for (infrared in infraredList) {
            if (infrared.isEnable) {
                result.add("${infrared.name}：${infrared.getModel()}\n")
            }
        }
        return result.toString()
    }

    override fun setAirCondition(newMode: Int, temperature: Int): Boolean {
        // 记录当前的模式和温度 用于回退操作
        val curMode = infraredList[0].getMode()
        val curTemperature = infraredList[0].getTemperature()

        val result = mutableListOf<Boolean>()
        for (infrared in infraredList) {
            if (infrared.isEnable) {
                result.add(infrared.setAirCondition(newMode, temperature))
            }
        }
        // 如果有一个操作失败，回退操作
        if (result.any { !it }) {
            for (infrared in infraredList) {
                infrared.setAirCondition(curMode, curTemperature)
            }
            return false
        }
        return true
    }

    override fun setPowerSupply(state: Boolean): Boolean {
        val curState = infraredList[0].isPowerSupply()
        val result = mutableListOf<Boolean>()
        for (infrared in infraredList) {
            if (infrared.isEnable) {
                result.add(infrared.setPowerSupply(state))
            }
        }
        // 如果有一个操作失败，回退操作
        if (result.any { !it }) {
            for (infrared in infraredList) {
                infrared.setPowerSupply(curState)
            }
            return false
        }
        return true
    }

    override fun isEnable(): Boolean {
        return infraredList.any { it.isEnable }
    }

    override fun setTemperature(temperature: Int) {
        // 更新时，更新所有开启的空调
        for (infrared in infraredList) {
            if (infrared.isEnable) {
                infrared.setTemperature(temperature)
            }
        }
    }

    override fun setMode(mode: Int) {
        for (infrared in infraredList) {
            if (infrared.isEnable) {
                infrared.setMode(mode)
            }
        }
    }

    override fun setAirConditionMode(newMode: Int): Boolean {
        val result = mutableListOf<Boolean>()
        for (infrared in infraredList) {
            if (infrared.isEnable) {
                result.add(infrared.setAirConditionMode(newMode))
            }
        }
        return result.all { it }
    }

    override fun setAirConditionTemperature(temperature: Int): Boolean {
        // 记录当前的温度 用于回退操作
        val curTemperature = infraredList[0].getTemperature()

        val result = mutableListOf<Boolean>()
        for (infrared in infraredList) {
            if (infrared.isEnable) {
                result.add(infrared.setAirConditionTemperature(temperature))
            }
        }
        if (result.any { !it }) {
            for (infrared in infraredList) {
                infrared.setAirConditionTemperature(curTemperature)
            }
            return false
        }
        return true
    }

    override fun isAvailable(): Boolean {
        return infraredList.any { it.isAvailable() }
    }

    // 只返回一个空调的内容
    override fun getTemperature(): Int {
        return infraredList[0].getTemperature()
    }

    override fun getModeName(): String {
        return infraredList[0].getModeName()
    }

}