package rowedto.jockeyedto.sheetedto.atticedto.equipmentedto.exactedto.costumeedto

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.datarecovery.R
import com.base.datarecovery.databinding.DeclarationedtoBinding
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Happyedto
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.ListBean
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Crossingedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Spareedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto
import kotlin.random.Random

@SuppressLint("SimpleDateFormat")
class Geneticedto : Crossingedto<DeclarationedtoBinding>() {
    override val binding: DeclarationedtoBinding by lazy {
        DeclarationedtoBinding.inflate(layoutInflater)
    }
    var wBean: Happyedto? = null
    override fun initView() {
        Spareedto.setStatusBarColor(this, Color.TRANSPARENT)
        binding.groundedto.updatePadding(top = Spareedto.getStatusBarHeight())
        getData()
        Scandaledto.admobMaxShowNativeAd(this, binding.tooledto,1)
        Scandaledto.admobMaxShowNativeAd(this, binding.dirtyedto,1)
    }

    override fun initListener() {
        binding.groundedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                Scandaledto.admobMaxShowInterstitialAd(this@Geneticedto){
                    finishToMain()
                }
            }
        })
    }

    private fun getData() {

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


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

    @SuppressLint("SetTextI18n")
    private fun setViews() {
        binding.snapedto.text = wBean?.city
        binding.secretionedto.text = wBean?.list?.get(0)?.fxDate
        binding.genuineedto.text = "${wBean?.list?.get(0)?.tempMax}°"
        binding.lastedto.text = "${wBean?.list?.get(0)?.humidity}%"
        binding.thinkedto.text = "${wBean?.list?.get(0)?.windScaleDay}"
        binding.publishedto.text = "${wBean?.list?.get(0)?.pressure}hPa"
        when ((wBean?.list?.get(0)?.iconDay)?.toInt()?.let { Mythedto.getWeatherType(it) }) {
            "Sunny day" -> {
                binding.islandedto.setImageResource(R.mipmap.reinforceedto)
            }

            "Cloudy day" -> {
                binding.islandedto.setImageResource(R.mipmap.printedto)
            }

            "Rainy day" -> {
                binding.islandedto.setImageResource(R.mipmap.halfwayedto)
            }

            "Snowy day" -> {
                binding.islandedto.setImageResource(R.mipmap.honoredto)
            }

            "Greasy day" -> {
                binding.islandedto.setImageResource(R.mipmap.thoseedto)
            }

            "Unknown" -> {
                binding.islandedto.setImageResource(R.mipmap.reinforceedto)
            }
        }

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

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

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

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

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

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

                    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.lampedto)
            tvTime = view.findViewById(R.id.seminaredto)
            tvMaxTmp = view.findViewById(R.id.woundedto)
            tvMinTmp = view.findViewById(R.id.motoristedto)
        }
    }

}
