package com.base.datarecovery.activity.guide

import android.content.Intent
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.base.datarecovery.R
import com.base.datarecovery.ads.admob.AdmobManager
import com.base.datarecovery.bean.SelectBean
import com.base.datarecovery.databinding.ActivityLayoutFirstSelectBinding
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.utils.BarUtils
import com.base.datarecovery.view.DialogViews.showFeature

class FirstSelectActivity : BaseActivity<ActivityLayoutFirstSelectBinding>() {

    override val binding: ActivityLayoutFirstSelectBinding by lazy {
        ActivityLayoutFirstSelectBinding.inflate(layoutInflater)
    }
    private val mList by lazy {
        mutableListOf(
            SelectBean(R.mipmap.icon_image, "Photo"),
            SelectBean(R.mipmap.icon_video, "Video"),
            SelectBean(R.mipmap.icon_voice, "Audio"),
            SelectBean(R.mipmap.icon_document, "Document")
        )
    }

    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        binding.idRl.run {
            layoutManager = LinearLayoutManager(this@FirstSelectActivity)
            adapter = mAdapter
        }
        AdmobManager.showNativeAd(this, binding.idFlAd)
    }

    override fun initListener() {
        binding.idContinue.setOnClickListener {
            val s = mList.filter { it.isSelect }
            if (s.isNotEmpty()) {
                startActivity(Intent(this, FirstGuestActivity::class.java))
                finish()
            } else {
                showFeature()
            }
        }
    }

    var isshow = 0

    private val mAdapter by lazy {
        class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            val ivImage: ImageView
            val tvName: TextView
            val ivSelect: ImageView

            init {
                ivImage = view.findViewById(R.id.id_icon)
                tvName = view.findViewById(R.id.id_tv_name)
                ivSelect = view.findViewById(R.id.id_img_select)
            }
        }

        object : RecyclerView.Adapter<ViewHolder>() {
            override fun onCreateViewHolder(
                parent: ViewGroup,
                viewType: Int
            ): ViewHolder {
                val view = LayoutInflater.from(this@FirstSelectActivity)
                    .inflate(R.layout.item_layout_select_type, parent, false)
                return ViewHolder(view)
            }

            override fun getItemCount() = mList.size

            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                val item = mList[position]
                holder.ivImage.setImageResource(item.imageRes)
                holder.tvName.text = item.name
                holder.itemView.setOnClickListener {
                    item.isSelect = !item.isSelect
                    holder.itemView.isSelected = item.isSelect
                    holder.ivSelect.isSelected = item.isSelect
                    isshow++
                    if (isshow < 2) {
                        AdmobManager.showNativeAd(this@FirstSelectActivity, binding.idFlAd)
                    }

                }
            }
        }
    }
}