package com.develop.airfryer.ui

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.develop.airfryer.databinding.DevModeViewBinding
import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevModes
import com.kuyuntech.cofarcooking.device.sdk.eventbus.event.DevCommonEvent
import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK
import org.greenrobot.eventbus.Subscribe

class DevModeView(context: Context, attrs: AttributeSet) : ConstraintLayout(context, attrs){

   private var mode = CofarSDK.devInfo().devMode;

   private val binding: DevModeViewBinding

   init {
      binding = DevModeViewBinding.inflate(LayoutInflater.from(context), this, true)
       setupClickListener()
       CofarSDK.register(this)
       updateUI()
   }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        CofarSDK.unregister(this);
    }

    @Subscribe
    fun onDevModeChange(event: DevCommonEvent){
            mode = CofarSDK.devInfo().devMode;
            updateUI();
    }

    private fun updateUI(){
        //判断模式
        if(mode == DevModes.AIR_FRYER){
            binding.dfbAtive.visibility = View.GONE;
            binding.dfbUnactive.visibility = View.VISIBLE;
            binding.kqzgActive.visibility = View.VISIBLE;
            binding.kqzgUnactive.visibility = View.GONE;
        }else{
            binding.dfbAtive.visibility = View.VISIBLE;
            binding.dfbUnactive.visibility = View.GONE;
            binding.kqzgActive.visibility = View.GONE;
            binding.kqzgUnactive.visibility = View.VISIBLE;
        }
    }

   private fun setupClickListener(){


      setOnClickListener {
          CofarSDK.changeDevMode(if (mode ==DevModes.SOUP) DevModes.AIR_FRYER else DevModes.SOUP)

      }

   }





}