package com.develop.step.viewmodel

import android.animation.ObjectAnimator
import android.content.res.Resources
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import androidx.lifecycle.MutableLiveData
import com.develop.base.ext.globalApp
import com.develop.base.mvvm.BaseViewModel
import com.develop.common.data_repo.db.ModesType
import com.develop.step.CookSettingType
import com.develop.common.R
import com.kuyuntech.cofarcooking.device.sdk.constant.core.MotorDirections
import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK
import java.util.*

class ModesViewMode : BaseViewModel() {

    private var rotateAnimator: ObjectAnimator? = null
    private var rotateAnimDir = -1
    val currentStep = MutableLiveData<CookSettingType>()
    private var turboTimer: Timer? = null
    var modeType = ModesType.CHOP.name
    var lastModeType = ModesType.CHOP.name


    fun changeStep(step: CookSettingType) {
        currentStep.value = step
    }

    /**启动搅碎*/
    fun startTurbo() {
        stopTurbo()
        turboTimer = Timer()
        turboTimer?.schedule(object : TimerTask() {
            override fun run() {
                CofarSDK.start(modeType)
            }

        }, 0, 300)
    }

    /**关闭搅碎*/
    fun stopTurbo() {
        turboTimer?.cancel()
        turboTimer = null
    }


    /**根据模式获取显示的banner图*/
    fun getBanner(): Int {
        return when (modeType) {
            ModesType.CHOP.name -> {
                R.drawable.ic_mode_chop
            }
            ModesType.STEAM.name -> {
                R.drawable.ic_mode_steam
            }
            ModesType.FOOD_PROCESSOR.name -> {
                R.drawable.ic_mode_food_processor
            }
            ModesType.BOIL_WATER.name -> {
                R.drawable.ic_mode_boil_water
            }
            ModesType.ADAPTED_COOKING.name -> {
                R.drawable.ic_mode_diy
            }
            ModesType.WIGHT.name -> {
                R.drawable.ic_mode_weight
            }
            ModesType.SLOW_COOK.name -> {
                R.drawable.ic_mode_slow_cook
            }
            ModesType.KNEAD_TOUGH.name -> {
                R.drawable.ic_mode_knead_dough
            }
            ModesType.TURBO.name -> {
                R.drawable.ic_mode_turbo
            }
            else -> {
                R.drawable.ic_mode_chop
            }
        }
    }

    /**根据模式获取显示的模式标题*/
    fun getModeTitle(resources: Resources): String {
        return when (modeType) {
            ModesType.CHOP.name -> {
                resources.getString(R.string.chop)
            }
            ModesType.STEAM.name -> {
                resources.getString(R.string.steam)
            }
            ModesType.FOOD_PROCESSOR.name -> {
                resources.getString(R.string.food_processor)
            }
            ModesType.BOIL_WATER.name -> {
                resources.getString(R.string.boil_water)
            }
            ModesType.ADAPTED_COOKING.name -> {
                resources.getString(R.string.adapted_cooking)
            }
            ModesType.WIGHT.name -> {
                resources.getString(R.string.wight)
            }
            ModesType.SLOW_COOK.name -> {
                resources.getString(R.string.slow_cook)
            }
            ModesType.KNEAD_TOUGH.name -> {
                resources.getString(R.string.knead_dough)
            }
            ModesType.TURBO.name -> {
                resources.getString(R.string.turbo)
            }
            else -> {
                resources.getString(R.string.adapted_cooking)
            }
        }
    }


    fun getDirectionStr(isLeft: Boolean, resources: Resources): String {
        return if (isLeft) resources.getString(R.string.turn_left)
        else resources.getString(R.string.turn_right)
    }

    fun playRotateAnimator(direction: Int, viewIcon: ImageView) {
        if (rotateAnimator == null) {
            rotateAnimator = ObjectAnimator.ofFloat(
                viewIcon, View.ROTATION, 0f, 360f
            ).apply {
                duration = 2000
                repeatCount = ObjectAnimator.INFINITE
                interpolator = LinearInterpolator()
            }
        }
        if (rotateAnimator!!.isRunning) {
            if (direction == rotateAnimDir) {
                return
            } else {
                rotateAnimator?.cancel()
            }
        }
        if (direction == MotorDirections.REVERSE.toInt()) {
            rotateAnimator?.reverse()
        } else {
            rotateAnimator?.start()
        }
        rotateAnimDir = direction
    }

    fun pauseRotateAnimator(viewIcon: ImageView) {
        rotateAnimator?.cancel()
        viewIcon.rotation = 0f
    }

    fun cancelAnimator(){
        rotateAnimator?.cancel()
    }

}