package com.base.scanqr.ui.main

import android.content.Intent
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.GridLayoutManager
import com.base.scanqr.R
import com.base.scanqr.ads.AdsMgr
import com.base.scanqr.ads.AdsShowCallBack
import com.base.scanqr.base.BaseFragment
import com.base.scanqr.bean.FunctionUIBean
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_CONTACT
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_EMAIL
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_EVENT
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_MESSAGE
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_TELEPHONE
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_TEXT
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_WEBSITE
import com.base.scanqr.bean.FunctionUIBean.Companion.KEY_WIFI
import com.base.scanqr.databinding.FragmentCreateBinding
import com.base.scanqr.ui.contact.ContractActivity
import com.base.scanqr.ui.email.EmailActivity
import com.base.scanqr.ui.event.EventActivity
import com.base.scanqr.ui.message.MessageActivity
import com.base.scanqr.ui.telephone.TelephoneActivity
import com.base.scanqr.ui.text.TextActivity
import com.base.scanqr.ui.website.WebsiteActivity
import com.base.scanqr.ui.wifi.WifiActivity
import com.base.scanqr.utils.BarUtils
import com.gyf.immersionbar.ktx.immersionBar

class CreateFragment : BaseFragment<FragmentCreateBinding>(FragmentCreateBinding::inflate) {

    private var adapter: FunctionAdapter? = null

    override fun initView() {
        super.initView()
        binding.tvTitle.updatePadding(top = BarUtils.getStatusBarHeight())
        binding.ivVip.updatePadding(top = BarUtils.getStatusBarHeight())
        adapter = FunctionAdapter()
        binding.rv.layoutManager = GridLayoutManager(requireContext(), 3)
        binding.rv.adapter = adapter
        adapter?.submitList(
            listOf(
                FunctionUIBean(KEY_WIFI, R.mipmap.h_wifi, resources.getString(R.string.wifi)),
                FunctionUIBean(KEY_TEXT, R.mipmap.h_text, resources.getString(R.string.text)),
                FunctionUIBean(KEY_EMAIL, R.mipmap.h_email, resources.getString(R.string.email)),
                FunctionUIBean(KEY_WEBSITE, R.mipmap.h_website, resources.getString(R.string.website)),
                FunctionUIBean(KEY_EVENT, R.mipmap.h_event, resources.getString(R.string.event)),
                FunctionUIBean(KEY_CONTACT, R.mipmap.h_contact, resources.getString(R.string.contact)),
//                FunctionUIBean(KEY_LOCATION, R.mipmap.h_loction, resources.getString(R.string.location)),
                FunctionUIBean(KEY_TELEPHONE, R.mipmap.h_telephone, resources.getString(R.string.telephone)),
                FunctionUIBean(KEY_MESSAGE, R.mipmap.h_message, resources.getString(R.string.message)),
            )
        )
        adapter?.itemClick = { key ->
            val activity = requireActivity() as MainActivity?
            activity?.let {
                AdsMgr.showInsert(it, false, object : AdsShowCallBack() {
                    override fun close(where: Int) {
                        itemKeyClick(key)
                    }

                    override fun failed(where: Int) {
                        itemKeyClick(key)

                    }

                    override fun googleFailed(where: Int) {
                        itemKeyClick(key)
                    }
                })
            }
        }
    }

    private fun itemKeyClick(key: String) {
        when (key) {
            KEY_WIFI -> {
                startActivity(Intent(requireContext(), WifiActivity::class.java))
            }

            KEY_TEXT -> {
                startActivity(Intent(requireContext(), TextActivity::class.java))
            }

            KEY_EMAIL -> {
                startActivity(Intent(requireContext(), EmailActivity::class.java))
            }

            KEY_WEBSITE -> {
                startActivity(Intent(requireContext(), WebsiteActivity::class.java))
            }

            KEY_EVENT -> {
                startActivity(Intent(requireContext(), EventActivity::class.java))
            }

            KEY_TELEPHONE -> {
                startActivity(Intent(requireContext(), TelephoneActivity::class.java))
            }

            KEY_CONTACT -> {
                startActivity(Intent(requireContext(), ContractActivity::class.java))
            }

            KEY_MESSAGE -> {
                startActivity(Intent(requireContext(), MessageActivity::class.java))
            }
        }
    }

    override fun onResume() {
        super.onResume()
        immersionBar {
            statusBarColor("#FFFFFFFF")
            statusBarDarkFont(true)
        }
    }

}