package com.swago.home.innerhome

import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.constant.UrlConstant
import com.swago.baseswago.cusview.SwagoRecyclerView
import com.swago.baseswago.fragment.BaseListFragment
import com.swago.baseswago.model.MomentModel
import com.swago.baseswago.model.home.BannerModel
import com.swago.baseswago.util.SwagoInfo
import com.swago.home.databinding.FragmentHotBinding
import com.swago.home.databinding.LayoutViewHeaderBinding
import com.youth.banner.indicator.CircleIndicator

/**
 *@date 2021/11/23 21:43
 *description:
 */
class HotFragment : BaseListFragment<FragmentHotBinding, MomentModel>() {

    private val homeVm by viewModels<HomeVm>()

    private var isFirstLoadData = true

    override val smartRecyclerView: SwagoRecyclerView<MomentModel> by lazy {
        binding.rv as SwagoRecyclerView<MomentModel>
    }
    override val adapter: BaseQuickAdapter<MomentModel, *> by lazy {
        HomeAdapter()
    }
    override val layoutManager: RecyclerView.LayoutManager by lazy {
        GridLayoutManager(context, 2)
    }

    private var labelValue = 0
    private var selectedPosition = 0
    //印尼，马来默认印尼；阿拉伯语默认阿拉伯语；其他语默认打开是展示全球
    private var matchLanguageId = 0

    override val loadData: (page: Int) -> Unit = {
        homeVm.getHotList(1, it + 1,labelValue)
    }

    private val bannerList by lazy {
        ArrayList<BannerModel>()
    }

    private val bannerAdapter by lazy {
        ImageAdapter(bannerList)
    }


    private var headerView: LayoutViewHeaderBinding? = null


    override fun initViewData() {
        UrlConstant.getConfigModel()?.let {
            if (!it.language_lables.isNullOrEmpty()){
                matchLanguageId = when(SwagoInfo.languageId){
                    3,4 -> {
                        3
                    }
                    5 -> {
                        5
                    }
                    else -> {
                        0
                    }
                }

                context?.let { context ->
                    val labelAdapter = LabelAdapter()
                    binding.rvLabel.layoutManager = LinearLayoutManager(context,RecyclerView.HORIZONTAL,false)
                    binding.rvLabel.adapter = labelAdapter
                    it.language_lables?.forEachIndexed { index, label ->
                        if (matchLanguageId==label.id){
                            label.isSelected = true
                            labelValue = label.id
                            selectedPosition = index
                        }
                    }
                    labelAdapter.setNewData(it.language_lables)

                    labelAdapter.setOnItemClickListener { _, _, position ->
                        labelAdapter.data[selectedPosition].isSelected = false
                        labelAdapter.data[position].isSelected = true
                        labelAdapter.notifyItemChanged(selectedPosition)
                        labelAdapter.notifyItemChanged(position)
                        selectedPosition = position
                        labelValue = labelAdapter.data[position].id
                        smartRecyclerView.startRefresh()
                    }
                }
            }
        }

        headerView =
            LayoutViewHeaderBinding.inflate(layoutInflater, smartRecyclerView.recyclerView, false)
        adapter.addHeaderView(headerView?.root)

        headerView?.banner?.addBannerLifecycleObserver(this)
            ?.setAdapter(bannerAdapter)
            ?.setIndicator(CircleIndicator(context))
            ?.setOnBannerListener { data, _ ->
                if (data is BannerModel){
                    ARouter.getInstance().build(ARouteConstant.Base.webView)
                        .withString("url", data.rotation_url)
                        .navigation()
                }
            }

        smartRecyclerView.recyclerView?.addItemDecoration(HomeGridItemDecoration())
        adapter.setOnItemClickListener { _, _, position ->
            ARouter.getInstance()
                .build(ARouteConstant.Room.user)
                .withInt("position", position)
                .withParcelableArrayList("data", adapter.data as ArrayList<MomentModel>)
                .navigation()
        }
    }

    override fun initLiveData() {
        homeVm.hotListLiveData.observe(this) {
            if (it == null) {
                smartRecyclerView.onFetchDataError()
            } else {
                smartRecyclerView.onFetchDataFinish(it, true,true)
            }
        }

        homeVm.bannerListLiveData.observe(this) {
            if (it != null) {
                if (it.isEmpty()){
                    headerView?.banner?.visibility = View.GONE
                }else{
                    bannerList.clear()
                    bannerList.addAll(it)
                    headerView?.banner?.visibility = View.VISIBLE
                    bannerAdapter.notifyDataSetChanged()
                }

            }else{
                headerView?.banner?.visibility = View.GONE
            }
        }
    }

    fun loadData(){
        if (!isFirstLoadData){
            smartRecyclerView.startRefresh()
        }
    }

    override fun onResume() {
        super.onResume()
        if (!isFirstLoadData){
            smartRecyclerView.startRefresh()
        }
        isFirstLoadData = false
    }

    override fun loadMoreNeed(): Boolean {
        return false
    }
}