package rallyedto.finishedto.complianceedto.minuteedto

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.addCallback
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.RecyclerView
import com.base.datarecovery.R
import com.base.datarecovery.databinding.SingleedtoBinding
import rallyedto.battleedto.acresedto.Lemonedto
import rallyedto.policeedto.huntedto.Eauxedto
import rallyedto.policeedto.huntedto.ListBean
import rallyedto.sunedto.tumouredto.majorityedto.loseedto.Togetheredto
import rallyedto.talledto.blewedto.Islandedto
import kotlin.random.Random

@SuppressLint("SimpleDateFormat")
class Incomeedto : Lemonedto<SingleedtoBinding>() {
    override val binding: SingleedtoBinding by lazy {
        SingleedtoBinding.inflate(layoutInflater)
    }
    var wBean: Eauxedto? = null
    override fun initView() {
        Islandedto.setStatusBarColor(this, Color.TRANSPARENT)
        binding.candidateedto.updatePadding(top = Islandedto.getStatusBarHeight())
        getData()
        Togetheredto.showNativeAd(this,binding.jeweledto, isShowAdNative = Togetheredto.isShowAdNativeBottom())

    }

    override fun initListener() {
        binding.candidateedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
                admobMaxShowInterstitialAd(isShow = Togetheredto.isShowAdBackInter()) {
                    finishToMain()
                }
            }

    }

    private fun getData() {

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


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

    @SuppressLint("SetTextI18n")
    private fun setViews() {
        binding.whomedto.text = wBean?.city
        binding.safetyedto.text = wBean?.list?.get(0)?.fxDate
        binding.excavateedto.text = "${wBean?.list?.get(0)?.tempMax}°"
        binding.integrationedto.text = "${wBean?.list?.get(0)?.humidity}%"
        binding.cabinetedto.text = "${wBean?.list?.get(0)?.windScaleDay}"
        binding.quietlyedto.text = "${wBean?.list?.get(0)?.pressure}hPa"
        when ((wBean?.list?.get(0)?.iconDay)?.toInt()?.let { Poundedto.getWeatherType(it) }) {
            "Sunny day" -> {
                binding.townedto.setImageResource(R.mipmap.bondedto)
            }

            "Cloudy day" -> {
                binding.townedto.setImageResource(R.mipmap.turkeyedto)
            }

            "Rainy day" -> {
                binding.townedto.setImageResource(R.mipmap.driveredto)
            }

            "Snowy day" -> {
                binding.townedto.setImageResource(R.mipmap.argumentedto)
            }

            "Greasy day" -> {
                binding.townedto.setImageResource(R.mipmap.jailedto)
            }

            "Unknown" -> {
                binding.townedto.setImageResource(R.mipmap.bondedto)
            }
        }

        wertherList.clear()
        wBean?.list?.let { wertherList.addAll(it) }
        binding.lededto.run {
            adapter = object : RecyclerView.Adapter<ViewHolder>() {
                override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
                    val view = LayoutInflater.from(this@Incomeedto)
                        .inflate(R.layout.whisperedto, 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 (Poundedto.getWeatherType(item.iconDay.toInt())) {
                        "Sunny day" -> {
                            holder.ivIcon.setImageResource(R.mipmap.neighboredto)
                        }

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

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

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

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

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

                    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.mustedto)
            tvTime = view.findViewById(R.id.flockedto)
            tvMaxTmp = view.findViewById(R.id.hardlyedto)
            tvMinTmp = view.findViewById(R.id.protectedto)
        }
    }

}
