package com.adealink.weparty.playmate.list.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.viewModels
import com.adealink.frame.util.onClick
import com.adealink.weparty.playmate.data.PlaymateListPriceSort
import com.adealink.weparty.playmate.data.PlaymateListStarSort
import com.adealink.weparty.playmate.databinding.LayoutPlaymateHomeListFilterBinding
import com.adealink.weparty.playmate.list.viewmodel.PlaymateListViewModel
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModelFactory
import com.adealink.weparty.R as APP_R

class PlaymateListFilterComp(
    lifecycleOwner: LifecycleOwner,
    val vFilter: LayoutPlaymateHomeListFilterBinding,
    val initStarSort: PlaymateListStarSort,
    val initPriceSort: PlaymateListPriceSort,
    val onStarSortClick: () -> Unit,
    val onPriceSortClick: () -> Unit,
) : ViewComponent(lifecycleOwner) {

    private val playmateListViewModel by viewModels<PlaymateListViewModel> { PlaymateViewModelFactory() }
    override fun onCreate() {
        super.onCreate()
        initView()
        observeViewModel()
        updateStarFilter(initStarSort)
        updatePriceFilter(initPriceSort)
    }

    private fun initView() {
        vFilter.clStarFilter.onClick {
            onStarSortClick.invoke()
        }
        vFilter.clPriceFilter.onClick {
            onPriceSortClick.invoke()
        }
    }

    private fun observeViewModel() {
        playmateListViewModel.starRankLD.observe(viewLifecycleOwner) {
            updateStarFilter(it)
        }
        playmateListViewModel.priceRankLD.observe(viewLifecycleOwner) {
            updatePriceFilter(it)
        }
    }

    private fun updateStarFilter(sort: PlaymateListStarSort) {
        when (sort) {
            PlaymateListStarSort.ASC -> {
                vFilter.tvStarRank.text = getCompatString(APP_R.string.common_star_rank_filter_to_high)
                vFilter.ivStarRank.setImageResource(APP_R.drawable.common_filter_asc_ic)
            }

            PlaymateListStarSort.DESC -> {
                vFilter.tvStarRank.text = getCompatString(APP_R.string.common_star_rank_filter_to_low)
                vFilter.ivStarRank.setImageResource(APP_R.drawable.common_filter_desc_ic)
            }

            PlaymateListStarSort.DEFAULT -> {
                vFilter.tvStarRank.text = getCompatString(APP_R.string.common_star_rank_filter)
                vFilter.ivStarRank.setImageResource(APP_R.drawable.common_filter_ic)
            }
        }
    }

    private fun updatePriceFilter(sort: PlaymateListPriceSort) {
        when (sort) {
            PlaymateListPriceSort.ASC -> {
                vFilter.tvPriceRank.text = getCompatString(APP_R.string.common_price_rank_filter_to_high)
                vFilter.ivPriceRank.setImageResource(APP_R.drawable.common_filter_asc_ic)
            }

            PlaymateListPriceSort.DESC -> {
                vFilter.tvPriceRank.text = getCompatString(APP_R.string.common_price_rank_filter_to_low)
                vFilter.ivPriceRank.setImageResource(APP_R.drawable.common_filter_desc_ic)
            }

            PlaymateListPriceSort.DEFAULT -> {
                vFilter.tvPriceRank.text = getCompatString(APP_R.string.common_price_rank_filter)
                vFilter.ivPriceRank.setImageResource(APP_R.drawable.common_filter_ic)
            }
        }
    }

}