package bums.lunatic.launcher.home.adapters

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper
import androidx.recyclerview.widget.RecyclerView
import bums.lunatic.launcher.databinding.WeatherBookBinding
import bums.lunatic.launcher.utils.BLog

class WeatherAdapter(
    private val pages: List<Int>,
    private val adatpers: List<RecyclerView.Adapter<out RecyclerView.ViewHolder>?>,
    private val weatherBook: WeatherBookBinding? = null)
    : RecyclerView.Adapter<WeatherAdapter.PageViewHolder>() {

    class PageViewHolder(val view: View): RecyclerView.ViewHolder(view)


    var childs : ArrayList<RecyclerView> = arrayListOf<RecyclerView>()


    @SuppressLint("ResourceType")
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PageViewHolder {


        val layoutInflater = LayoutInflater.from(parent.context).inflate(pages[viewType], parent, false)
//        var first = layoutInflater.findViewById<RecyclerView>(R.id.recycler_hourly_weather)
//        var second = layoutInflater.findViewById<RecyclerView>(R.id.weather_dress_recycller)

        return PageViewHolder(layoutInflater)
    }


    fun syncScroll(newPosition: Int) {
        childs[newPosition].let {recyclerView ->
            BLog.LOGE("recyclerView >>> ${recyclerView} 1 ")
            if (!isSyncingScroll) {
                isSyncingScroll = true
                BLog.LOGE("recyclerView >>> ${recyclerView} 2 ")
                childs.forEach { c ->
//                    val visibleItemCount = (layoutManager?.findLastVisibleItemPosition() ?: 0) - (layoutManager?.findFirstVisibleItemPosition() ?: 0)
                    if (c != recyclerView) {
                        (c.layoutManager as LinearLayoutManager)?.let {
                            it.findFirstVisibleItemPosition()?.let {
                                (recyclerView?.layoutManager as? LinearLayoutManager)?.let { target ->
                                    target.scrollToPositionWithOffset(it, 0)
                                }
//                                    .scrollToPosition(it)
                            }
                        }
                        BLog.LOGE("recyclerView >>> ${recyclerView} 3 ")
                    }
                }
                isSyncingScroll = false
                BLog.LOGE("recyclerView >>> ${recyclerView} 4 ")
            }
        }

    }


    val mOnScrollListener = object : RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)
//           recyclerView.postDelayed({syncScroll(recyclerView)},10L)
//            recyclerView.post{syncScroll(recyclerView)}
        }

        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
        }
    }
    var isSyncingScroll = false
    override fun onBindViewHolder(holder: PageViewHolder, position: Int) {
        if (adatpers[position] != null) {
            (holder.view as RecyclerView).apply {
                this.adapter = adatpers[position]
                this.layoutManager = LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false)
                if (!childs.contains(this)) {
                    childs.add(this)
                    this.addOnScrollListener(mOnScrollListener)
                    LinearSnapHelper().attachToRecyclerView(this)
                }
            }
//            when (position) {
//                0 -> holder.view.findViewById<RecyclerView>(R.id.recycler_hourly_weather).apply {
//                    this.adapter = adatpers[position]
//                    this.layoutManager =
//
//                }
//
//                else -> holder.view.findViewById<RecyclerView>(R.id.weather_dress_recycller).apply {
//                    this.adapter = adatpers[position]
//                    this.layoutManager =
//                        LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false)
//                }
//            }
        }
    }

    override fun getItemCount(): Int = pages.size

    override fun getItemViewType(position: Int): Int = position
}