package com.develop.setting.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.develop.base.mvvm.BaseBindingActivity
import com.develop.common.router.Screens
import com.develop.common.ui.CommonBindingActivity
import com.develop.setting.R
import com.develop.setting.adapter.ScreenBannerAdapter
import com.develop.setting.databinding.ActivityScreensaverBinding
import com.drake.brv.utils.bindingAdapter
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.youth.banner.listener.OnPageChangeListener

@Route(path = Screens.Setting.SCREEN_SAVER)
class ScreensaverActivity : CommonBindingActivity<ActivityScreensaverBinding>() {

    override fun createViewBinding(inflater: LayoutInflater): ActivityScreensaverBinding {
        return ActivityScreensaverBinding.inflate(layoutInflater)
    }

    private var selectedPosition = -1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        hasShowScreenSaver = true
        val dataList = mutableListOf(
            com.develop.common.R.drawable.ic_screensaver
        )
        val bannerAdapter = ScreenBannerAdapter(dataList)
        binding.bannerView.adapter = bannerAdapter

        binding.circleRecycler.apply {
            linear(RecyclerView.HORIZONTAL)
            setup {
                addType<Int>(R.layout.item_screen_circle)
                onBind {
                    val ivCircle = findView<View>(R.id.iv_circle)
                    if (selectedPosition == absoluteAdapterPosition) {
                        ivCircle.setBackgroundResource(com.develop.common.R.drawable.f4a12e_circle)
                    } else {
                        ivCircle.setBackgroundResource(com.develop.common.R.drawable.e5e5e5_circle)
                    }
                }
            }
        }.models = dataList

        binding.bannerView.apply {
            isAutoLoop(false)
            setOnBannerListener { data, position ->
                finish()
            }
            addOnPageChangeListener(object : OnPageChangeListener {
                override fun onPageScrolled(
                    position: Int, positionOffset: Float, positionOffsetPixels: Int
                ) {

                }

                override fun onPageSelected(position: Int) {
                    updateSelectionPos(position)
                }

                override fun onPageScrollStateChanged(state: Int) {
                }
            })
        }
    }

    private fun updateSelectionPos(pos: Int) {
        val tempPosition = selectedPosition
        selectedPosition = pos
        binding.circleRecycler.bindingAdapter.apply {
            notifyItemChanged(tempPosition)
            notifyItemChanged(selectedPosition)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        hasShowScreenSaver = false
//        binding.showTimeView.stopTime()
    }
}