package com.base.filerecoveryrecyclebin.view

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.RecyclerView
import com.base.filerecoveryrecyclebin.R
import com.base.filerecoveryrecyclebin.ads.AdmobMaxHelper
import com.base.filerecoveryrecyclebin.bean.ListBean
import com.base.filerecoveryrecyclebin.bean.WeatherBean
import com.base.filerecoveryrecyclebin.databinding.ActivityWeatherBinding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.help.WeatherUtils
import com.base.filerecoveryrecyclebin.utils.BarUtils
import kotlin.random.Random

@SuppressLint("SimpleDateFormat")
class WeatherInterface : BaseActivity<ActivityWeatherBinding>() {
    override val binding: ActivityWeatherBinding by lazy {
        ActivityWeatherBinding.inflate(layoutInflater)
    }
    var wBean: WeatherBean? = null
    override fun initView() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.idBack.updatePadding(top = BarUtils.getStatusBarHeight())
        getData()
        AdmobMaxHelper.admobMaxShowNativeAd(this, binding.idFlNative)
        AdmobMaxHelper.admobMaxShowNativeAd(this, binding.idFlAd)
    }

    override fun initListener() {
        binding.idBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {

                if (AdmobMaxHelper.showBackInterAdSp()) {
                    AdmobMaxHelper.admobMaxShowInterstitialAd(this@WeatherInterface) {
                        finishToMain()
                    }
                } else {
                    finishToMain()
                }
            }
        })
    }

    private fun getData() {

        wBean = WeatherUtils.getWeatherEntity()
        if (wBean != null) {
            setViews()
        } else {
            WeatherUtils.requestWeatherData()
        }
        binding.root.postDelayed({
            AdmobMaxHelper.admobMaxShowInterstitialAd(this) {
                binding.idFlLoading.isVisible = false
            }
        }, Random.nextLong(2000, 4000))
        ValueAnimator.ofFloat(0f, 360f).apply {
            duration = 1100
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener { binding.idZhuan.rotation = it.animatedValue as Float }
            start()
        }
    }


    @SuppressLint("SetTextI18n")
    private var wertherList = mutableListOf<ListBean>()

    @SuppressLint("SetTextI18n")
    private fun setViews() {
        binding.idTvCity.text = wBean?.city
        binding.idTvDate.text = wBean?.list?.get(0)?.fxDate
        binding.idTvTmp.text = "${wBean?.list?.get(0)?.tempMax}°"
        binding.idTvHumidity.text = "${wBean?.list?.get(0)?.humidity}%"
        binding.idTvWind.text = "${wBean?.list?.get(0)?.windScaleDay}"
        binding.idTvPressure.text = "${wBean?.list?.get(0)?.pressure}hPa"
        when ((wBean?.list?.get(0)?.iconDay)?.toInt()?.let { WeatherUtils.getWeatherType(it) }) {
            "Sunny day" -> {
                binding.idImgW.setImageResource(R.mipmap.d_qing)
            }

            "Cloudy day" -> {
                binding.idImgW.setImageResource(R.mipmap.d_yin)
            }

            "Rainy day" -> {
                binding.idImgW.setImageResource(R.mipmap.d_dayu)
            }

            "Snowy day" -> {
                binding.idImgW.setImageResource(R.mipmap.d_xiaxue)
            }

            "Greasy day" -> {
                binding.idImgW.setImageResource(R.mipmap.d_wumaishachengbao)
            }

            "Unknown" -> {
                binding.idImgW.setImageResource(R.mipmap.d_qing)
            }
        }

        wertherList.clear()
        wBean?.list?.let { wertherList.addAll(it) }
        binding.idRlWeather.run {
            adapter = object : RecyclerView.Adapter<ViewHolder>() {
                override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
                    val view = LayoutInflater.from(this@WeatherInterface)
                        .inflate(R.layout.item_layout_wheather, parent, false)
                    return ViewHolder(view)
                }

                override fun getItemCount(): Int {
                    return wertherList.size
                }

                @SuppressLint("SetTextI18n")
                override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                    val item = wertherList[position]
                    when (WeatherUtils.getWeatherType(item.iconDay.toInt())) {
                        "Sunny day" -> {
                            holder.ivIcon.setImageResource(R.mipmap.x_qing)
                        }

                        "Cloudy day" -> {
                            holder.ivIcon.setImageResource(R.mipmap.x_yin)
                        }

                        "Rainy day" -> {
                            holder.ivIcon.setImageResource(R.mipmap.x_dayu)
                        }

                        "Snowy day" -> {
                            holder.ivIcon.setImageResource(R.mipmap.x_xiaxue)
                        }

                        "Greasy day" -> {
                            holder.ivIcon.setImageResource(R.mipmap.x_wumaishachengbao)
                        }

                        "Unknown" -> {
                            holder.ivIcon.setImageResource(R.mipmap.x_qing)
                        }
                    }

                    holder.tvTime.text = item.fxDate
                    holder.tvMaxTmp.text = "${item.tempMax}°/"
                    holder.tvMinTmp.text = "${item.tempMin}°"
                }

            }
        }
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val ivIcon: ImageView
        val tvTime: TextView
        val tvMaxTmp: TextView
        val tvMinTmp: TextView

        init {
            ivIcon = view.findViewById(R.id.id_img_icon)
            tvTime = view.findViewById(R.id.id_tv_time)
            tvMaxTmp = view.findViewById(R.id.id_tv_max_tmp)
            tvMinTmp = view.findViewById(R.id.id_tv_min_tmp)
        }
    }

}
