package bums.lunatic.launcher.home.adapters

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import bums.lunatic.launcher.databinding.ItemRecHourlyDressBinding
import bums.lunatic.launcher.model.Hour
import bums.lunatic.launcher.model.WeatherInfoManager
import bums.lunatic.launcher.utils.BLog
import java.util.Calendar

class WeatherDressAdatper (private val dataSet: ArrayList<Hour>) : RecyclerView.Adapter<WeatherDressAdatper.ViewHolder>(){
        var isChangedAmOrPm: Boolean = true

        class ViewHolder(val viewItem: ItemRecHourlyDressBinding): RecyclerView.ViewHolder(viewItem.root)

        //    @SuppressLint("NotifyDataSetChanged")
        fun update(li: Collection<Hour>) {
            li.toList()
            this.dataSet.clear()
            this.dataSet.addAll(li)
//        notifyDataSetChanged()
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WeatherDressAdatper.ViewHolder {
//        val view = LayoutInflater.from(parent.context)
//            .inflate(R.layout.item_rec_hourly_dress, parent, false)
//        val itemBinding: ItemRecHourlyDressBinding = DataBindingUtil.
            val binding = ItemRecHourlyDressBinding.inflate(LayoutInflater.from(parent.context), parent, false)
            return WeatherDressAdatper.ViewHolder(binding)
        }

        fun getToday() = Calendar.getInstance().get(Calendar.DAY_OF_YEAR)

        @SuppressLint("SetTextI18n")
        override fun onBindViewHolder(holder: WeatherDressAdatper.ViewHolder, position: Int) {
            val data = dataSet[position] as? Hour
//            BLog.LOGE("saved weatherForcast >>> asFlow ${dataSet.size}")
//        val today = Calendar.getInstance()
//        today.time = Date(data?.time_epoch?.toLong()?.times(1000L) ?: 0L)
//        val dayOfItem = today.get(Calendar.DAY_OF_YEAR)
            data?.let {
//            total.loc = WorkersDb.getRealm().query<Location>().also {
//                BLog.LOGE("re >>> ${it.description()}") // 쿼리 로그
//            }.find().first()
                WeatherInfoManager.getShowingInfo(it).apply {
//                    total.setInfo(this)
                    holder.viewItem.setInfo(this)
                }
//                BLog.LOGE("reeeeeeeeeee >>> ${holder.viewItem.hour.text}")
                holder.viewItem.amOrPm.visibility =
                    if (arrayListOf(12, 0).contains(WeatherInfoManager.toZonedDateTime(it.time_epoch).hour) || position == 0) {
                        View.VISIBLE
                    } else View.INVISIBLE
                holder.viewItem.hour.apply {
                    if (WeatherInfoManager.toZonedDateTime(it.time_epoch).hour == 0) {
                        this@apply.setTextColor(Color.BLACK)
                        this@apply.isSelected = true
                    } else {
                        this@apply.setTextColor(Color.WHITE)
                        this@apply.isSelected = false
                    }
                }
                holder.viewItem.imgDress.setImageLevel(it.temp_c.toInt())
            }
        }

        override fun getItemCount(): Int = dataSet.size
}