package com.adealink.weparty.playmate.setting.dialog

import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.adealink.frame.base.Rlt
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.clearItemDecoration
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.GridSpacingItemDecoration
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.databinding.DialogPlaymateOrderWeekDayBinding
import com.adealink.weparty.playmate.setting.adapter.WeekDayItemViewBinder
import com.adealink.weparty.playmate.setting.viewmodel.OrderSettingViewModel
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModelFactory
import com.adealink.weparty.util.WeekDay

data class WeekDayChooseItem(
    val weekDay: WeekDay,
    var selected: Boolean = false
)

class WeekDayDialog : BottomDialogFragment(R.layout.dialog_playmate_order_week_day) {

    private val binding by viewBinding(DialogPlaymateOrderWeekDayBinding::bind)

    private val viewModel by activityViewModels<OrderSettingViewModel> { PlaymateViewModelFactory() }

    private var chooseItemList = listOf<WeekDayChooseItem>()
    private val listAdapter by fastLazy { MultiTypeListAdapter<WeekDayChooseItem>() }

    override fun initViews() {
        super.initViews()

        listAdapter.register(WeekDayItemViewBinder { item, position ->
            onSelect(item, position)
        })
        binding.rvItems.layoutManager = GridLayoutManager(context, 3)
        binding.rvItems.clearItemDecoration()
        binding.rvItems.addItemDecoration(
            GridSpacingItemDecoration(
                3,
                spacingH = 16.dp(),
                spacingV = 20.dp(),
                false
            )
        )
        binding.rvItems.adapter = listAdapter
        binding.btnConfirm.onClick {
            confirm()
        }
    }

    override fun observeViewModel() {
        viewModel.weekDayLD.observe(viewLifecycleOwner) {
            setChooseItems(it)
        }
    }

    private fun setChooseItems(chooseItems: List<WeekDay>?) {
        this.chooseItemList = WeekDay.entries.map { day ->
            WeekDayChooseItem(day, chooseItems?.contains(day) ?: false)
        }
        listAdapter.submitList(chooseItemList)
    }

    private fun onSelect(item: WeekDayChooseItem, position: Int) {
        item.selected = !item.selected
        listAdapter.notifyItemChanged(position)
    }

    private fun confirm() {
        val chooseList = chooseItemList.filter { it.selected }
        if (chooseList.isEmpty()) {
            showToast(R.string.playmate_order_setting_week_day_choose_empty)
            return
        }
        showLoading()
        viewModel.updateWeekDay(chooseList.map { it.weekDay }).observe(viewLifecycleOwner) { rlt ->
            dismissLoading()
            showToast(rlt)
            when (rlt) {
                is Rlt.Failed -> {
                    //Ntd.
                }

                is Rlt.Success -> {
                    dismiss()
                }
            }
        }
    }
}