package com.casic.br.utils

import android.util.Log
import com.tuya.smart.android.device.builder.TuyaTimerBuilder
import com.tuya.smart.android.device.enums.TimerDeviceTypeEnum
import com.tuya.smart.home.sdk.TuyaHomeSdk
import com.tuya.smart.home.sdk.constant.TimerUpdateEnum
import com.tuya.smart.sdk.api.IResultCallback
import com.tuya.smart.sdk.api.ITuyaDevice


object CommandManager {
    private const val kTag = "CommandManager"

    fun getTuyaDp(deviceInstance: ITuyaDevice, dpId: String) {
        deviceInstance.getDp(dpId, object : IResultCallback {
            override fun onSuccess() {

            }

            override fun onError(code: String?, error: String?) {
                Log.e(kTag, "onError: $error")
            }
        })
    }

    fun sendTuyaDp(deviceInstance: ITuyaDevice, cmd: String) {
        deviceInstance.publishDps(cmd, object : IResultCallback {
            override fun onSuccess() {

            }

            override fun onError(code: String?, error: String?) {
                Log.e(kTag, "onError: $error")
            }
        })
    }

    fun addDeviceTimer(
        taskName: String, deviceId: String, dps: String, loops: String, aliasName: String
    ) {
        val builder = TuyaTimerBuilder.Builder()
            .taskName(taskName)
            .devId(deviceId)
            .deviceType(TimerDeviceTypeEnum.DEVICE)
            .actions(dps)
            .loops(loops)
            .aliasName(aliasName)
            .status(1)
            .appPush(true)
            .build()
        TuyaHomeSdk.getTimerInstance().addTimer(builder, object : IResultCallback {
            override fun onError(code: String?, error: String?) {
                Log.e(kTag, "code: $code --- onError: $error")
            }

            override fun onSuccess() {

            }
        })
    }

    fun deleteDeviceTimer(taskName: String, deviceId: String) {
        TuyaHomeSdk.getTimerInstance().updateCategoryTimerStatus(
            taskName,
            deviceId,
            TimerDeviceTypeEnum.DEVICE,
            TimerUpdateEnum.DELETE,
            object : IResultCallback {
                override fun onError(code: String?, error: String?) {
                    Log.e(kTag, "code: $code --- onError: $error")
                }

                override fun onSuccess() {

                }
            })
    }
}