package com.base.scanqrclear.ui.main

import android.annotation.SuppressLint
import android.content.Intent
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.base.scanqrclear.R
import com.base.scanqrclear.ads.AdsMgr
import com.base.scanqrclear.ads.AdsShowCallBack
import com.base.scanqrclear.base.BaseFragment
import com.base.scanqrclear.base.goToAc
import com.base.scanqrclear.bean.FunctionUIBean
import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_CONTACT
import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_EMAIL
import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_EVENT
import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_MESSAGE
import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_TELEPHONE
import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_TEXT
import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_WEBSITE
import com.base.scanqrclear.bean.FunctionUIBean.Companion.KEY_WIFI
import com.base.scanqrclear.bean.config.AdConfigBean.Companion.adsConfigBean
import com.base.scanqrclear.databinding.FragmentCreateBinding
import com.base.scanqrclear.luma.WeatherInterface
import com.base.scanqrclear.luma.WeatherUtils
import com.base.scanqrclear.ui.contact.ContractActivity
import com.base.scanqrclear.ui.email.EmailActivity
import com.base.scanqrclear.ui.event.EventActivity
import com.base.scanqrclear.ui.message.MessageActivity
import com.base.scanqrclear.ui.telephone.TelephoneActivity
import com.base.scanqrclear.ui.text.TextActivity
import com.base.scanqrclear.ui.website.WebsiteActivity
import com.base.scanqrclear.ui.webview.WebBrowserActivity
import com.base.scanqrclear.ui.wifi.WifiActivity
import com.base.scanqrclear.utils.AppPreferences
import com.base.scanqrclear.utils.BarUtils
import com.gyf.immersionbar.ktx.immersionBar
import kotlin.random.Random

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

    private var adapter: FunctionAdapter? = null

    private val functions by lazy(LazyThreadSafetyMode.NONE) {
        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)),
        )
    }

    override fun initView() {
        super.initView()
        binding.space.layoutParams.height = BarUtils.getStatusBarHeight()
        // binding.ivVip.updatePadding(top = BarUtils.getStatusBarHeight())
        adapter = FunctionAdapter()
        binding.rv.layoutManager = GridLayoutManager(requireContext(), 3)
        binding.rv.adapter = adapter
        adapter?.submitList(functions)
        adapter?.itemClick = { key ->
            val activity = requireActivity() as MainActivity?
            activity?.let {
                if (adsConfigBean.functionInShowAd) {
                    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)
                        }
                    })
                } else {
                    itemKeyClick(key)
                }
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun setWeather(){
        val weatherEntity = WeatherUtils.getWeatherEntity()
        var icon:Int
        var value = 22
        weatherEntity?.let {
            val subBean = weatherEntity.list[0]
            val min = subBean.tempMin.toInt()
            val max = subBean.tempMax.toInt()
            if (min == max) {
                value = max
            }
            if (min < max) {
                value = Random.nextInt(min, max)
            }
            icon = when (WeatherUtils.getWeatherType(subBean.iconDay.toInt())) {
                "Sunny day" -> R.mipmap.x_qing
                "Cloudy day" -> R.mipmap.x_yin
                "Rainy day" -> R.mipmap.x_dayu
                "Snowy day" -> R.mipmap.x_xiaxue
                "Greasy day" -> R.mipmap.x_wumaishachengbao
                "Unknown" -> R.mipmap.x_qing
                else -> R.mipmap.x_qing
            }
            binding.tvWeather.setCompoundDrawablesWithIntrinsicBounds(
                ContextCompat.getDrawable(requireContext(), icon),
                null,
                null,
                null
            )
            binding.tvWeather.text = "${value}℃"
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun itemKeyClick(key: String) {
        val bean = functions.find { it.key == key }
        if (bean?.isPlaying == true) {
            bean.showGuide = false
            AppPreferences.getInstance().put("${key}_guide", false)
        }
        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))
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onResumeOneShoot() {
        super.onResumeOneShoot()
        adapter?.changeGuide()
        setWeather()
    }

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

    override fun initListener() {
        super.initListener()
        binding.tvLogo.setOnClickListener {
            goToAc(ScanAc::class.java)
        }
        binding.tvZodiac.setOnClickListener {
            startActivity(Intent(context, WebBrowserActivity::class.java).apply {
                putExtra("url", "https://www.horoscope.com/us/index.aspx")
            })
        }
        binding.tvWeather.setOnClickListener {
            startActivity(Intent(context, WeatherInterface::class.java))
        }
//        binding.rv.setOnClickListener {
//            if (isContinuousTimesClick()) {
//                val sb = StringBuilder()
//                val gson = Gson()
//                sb.append("isInBlackList:").append(ConfigBean.configBean.isInBlackList).append("\n")
//                sb.append("ut:${ConfigBean.configBean.ut}").append("\n")
//                val adLog = gson.toJson(ConfigBean.configBean.adConfigBean).replace(",", ",\n")
//                sb.append(adLog).append("\n")
//                val popupLog =
//                    gson.toJson(ConfigBean.configBean.popupConfigBean).replace(",", ",\n")
//                sb.append(popupLog).append("\n")
//                sb.append("uuid:").append(AppPreferences.getInstance().getString("uuid", "null"))
//                requireContext().showLogDialog(sb.toString())
//            }
//        }
    }

}