package com.base.scanqrclear.qr

import android.content.Intent
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import com.base.scanqrclear.base.BaseActivity
import com.base.scanqrclear.databinding.ActivityQrStyleBinding
import com.base.scanqrclear.qr.QrStyleUtils.getQrStyleBean
import com.base.scanqrclear.utils.BarUtils
import com.google.gson.Gson
import com.gyf.immersionbar.ktx.immersionBar

class QrStyleActivity : BaseActivity<ActivityQrStyleBinding>(ActivityQrStyleBinding::inflate) {

    private var qrString = ""
    private val adapter = QrStyleAdapter()
    override fun initView() {
        super.initView()
        binding.clTop.updatePadding(top = BarUtils.getStatusBarHeight())
        qrString = intent.extras?.getString("qrString", "") ?: ""
        initAdapter()
    }

    private fun initAdapter() {
        binding.rv.adapter = adapter
        val list = getQrStyleBean()

        list?.let {
            it.first().isSelect = true
            adapter.submitList(it)
        }
    }

    override fun initListener() {
        super.initListener()

        onBackPressedDispatcher.addCallback {
            finishToMainTop()
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }

        binding.tvConfirm.setOnClickListener {
            startActivity(Intent(this, QrStyleResultActivity::class.java).apply {
                val data = adapter.items.find { it.isSelect }
                putExtra("data", Gson().toJson(data))
                putExtra("qrString", qrString)
            })
        }

    }

    override fun configSystemBar() {
        immersionBar {
            statusBarColor("#FFFFFFFF")
        }
    }
}