package com.develop.airfryer.ui

import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.blankj.utilcode.util.ActivityUtils.startActivity
import com.blankj.utilcode.util.AppUtils
import com.develop.airfryer.databinding.DevModeViewBinding
import com.kuyuntech.cofarcooking.device.sdk.constant.core.CommonEventTypes
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()
       if(!AppUtils.getAppVersionName().split(".")[0].startsWith("5")){
           this.visibility = GONE
       }
   }

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

    @Subscribe
    fun onDevModeChange(event: DevCommonEvent){
            if(CommonEventTypes.DEV_MODE_CHANGE == event.type){
                mode = CofarSDK.devInfo().devMode;
                updateUI();
                val intent = Intent(this.context, Class.forName("com.develop.main.ui.ModeEntranceActivity"))
                intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
                this.context.startActivity(intent)
            }

    }

    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)

      }

   }





}