package com.zswc.creativity.widget

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.DrawableRes
import androidx.databinding.DataBindingUtil
import com.qmuiteam.qmui.kotlin.onClick
import com.zswc.creativity.R
import com.zswc.creativity.activity.MainActivity
import com.zswc.creativity.config.ConfigMMKV
import com.zswc.creativity.databinding.WidgetGuideBinding
import com.ysnows.base.utils.MMKVManager
import com.ysnows.base.utils.UiSwitch

open class WidgetGuide constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) {
    lateinit var binding: WidgetGuideBinding

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    private fun initView(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.widget_guide, this, true)

    }

    private fun listeners() {

    }

    fun setImageResource(@DrawableRes drawableId: Int) {
        binding.imgBg.setImageResource(drawableId)
        if (drawableId == R.drawable.img_bg_n) {
            binding.btnGo.visibility = View.VISIBLE
            binding.btnGo.onClick {

                MMKVManager.instance().encode(ConfigMMKV.FIRST_OPEN, false)
                UiSwitch.single(context, MainActivity::class.java)

                (context as Activity).finish()
            }
        } else {
            binding.btnGo.visibility = View.GONE
        }
    }

    init {
        initView(context, attrs, defStyleAttr)
        listeners()
    }
}
