package com.develop.step.ui.cook_step.model

import com.develop.common.data_repo.db.entity.DevRecipeCookingStep
import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus
import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK

class CookStepUiData {
    var doingModify = false
    var runningStatus = DevStatus.STOP.toInt()

    // 设置目标温度
    var targetTemp = 0
    // 设置目标时间
    var targetTime = 0

    // 当前温度
    var currentTemp = 0
    // 当前时间
    var currentTime = 0
    // 当前剩余时间
    var remainTime = 0
    // targetTimeBuffer
    var targetTimeBuffer = 0
    // 称重数
    var weightNum = 0
    // 当前方向
    var direction = 0
    // 当前转速
    var currentSpeed = 0

    var currentWaterGear = 0


    // 限制时间调整范围
    var limitMinTime = 0
    var limitMaxTime = 0

    // 限制温度调整范围
    var limitMinTemp = 0
    var limitMaxTemp = 0

    // 限制转速调整范围
    var limitMinSpeed = 0
    var limitMaxSpeed = 0

    // 限制转速调整范围
    var limitMinWaterGear = 0
    var limitMaxWaterGear = 0

    //是否可以修改参数
    var isTempChange = false
    var isTimeChange = false
    var isMotorGearChange = false
    var isWaterGearChange = false

    var isMotorDirectionChange = false

    fun applyRecipeSetting(data: DevRecipeCookingStep) {
        targetTime = (data.minute ?: 0) * 60 + (data.second ?: 0)
        targetTemp = data.temperature ?: 0
        direction = if (data.rotateDirection == "0") 0 else 1


        currentSpeed = data.rotateSpeed ?: 0
//        remainTime = targetTime
        CofarSDK.devMode(data.workMode).let {
            limitMinTime = it.minTime
            limitMaxTime = it.maxTime
            limitMinTemp = it.minTemp
            limitMaxTemp = it.maxTemp
            limitMinSpeed = it.minMotorGear
            limitMaxSpeed = it.maxMotorGear
            isTempChange = it.isTempChange
            isTimeChange = it.isTimeChange
            isMotorGearChange = it.isMotorGearChange
            isWaterGearChange = it.isWaterGearChange
            limitMinWaterGear = it.minWaterGear
            limitMaxWaterGear = it.maxWaterGear
            isMotorDirectionChange = it.isMotorDirectionChange
        }
        targetTime = getFixedValue(targetTime, limitMinTime, limitMaxTime)
        targetTemp = getFixedValue(targetTemp, limitMinTemp, limitMaxTemp)
        currentSpeed = getFixedValue(currentSpeed, limitMinSpeed, limitMaxSpeed)



    }

    private fun getFixedValue(source: Int, min: Int, max: Int): Int {
        if (source < min) {
            return min
        }
        if (source > max) {
            return source
        }
        return source
    }

    fun newCopy(): CookStepUiData {
        val uiData = CookStepUiData()
        uiData.let {
            it.targetTemp = targetTemp
            it.targetTime = targetTime
            it.currentTemp = currentTemp
            it.currentTime = currentTime
            it.remainTime = remainTime
            it.weightNum = weightNum
            it.direction = direction
            it.currentSpeed = currentSpeed
            it.limitMinTime = limitMinTime
            it.limitMaxTime = limitMaxTime
            it.limitMinTemp = limitMinTemp
            it.limitMaxTemp = limitMaxTemp
            it.limitMinSpeed = limitMinSpeed
            it.limitMaxSpeed = limitMaxSpeed
            it.isTempChange = isTempChange
            it.isTimeChange = isTimeChange
            it.isMotorGearChange = isMotorGearChange
            it.isMotorDirectionChange = isMotorDirectionChange
        }
        return uiData
    }
    
    fun copyFrom(uiData: CookStepUiData): CookStepUiData {
        uiData.let {
            targetTemp = it.targetTemp
            targetTime = it.targetTime
            currentTemp = it.currentTemp
            currentTime = it.currentTime
            remainTime = it.remainTime
            weightNum = it.weightNum
            direction = it.direction
            currentSpeed = it.currentSpeed
            limitMinTime = it.limitMinTime
            limitMaxTime = it.limitMaxTime
            limitMinTemp = it.limitMinTemp
            limitMaxTemp = it.limitMaxTemp
            limitMinSpeed = it.limitMinSpeed
            limitMaxSpeed = it.limitMaxSpeed
            isTempChange = it.isTempChange
            isTimeChange = it.isTimeChange
            isMotorGearChange = it.isMotorGearChange
            isMotorDirectionChange = it.isMotorDirectionChange
        }
        return uiData
    }
}