package com.adealink.weparty.playmate.setting.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.mvvm.viewmodel.viewModels
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseViewComponent
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.databinding.LayoutPlaymateOrderSettingTimeRangeBinding
import com.adealink.weparty.playmate.setting.dialog.TimeRangeDialog
import com.adealink.weparty.playmate.setting.viewmodel.OrderSettingViewModel
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModelFactory

class OrderTimeRangeComp(
    lifecycleOwner: LifecycleOwner,
    val binding: LayoutPlaymateOrderSettingTimeRangeBinding
) : BaseViewComponent(lifecycleOwner) {
    private val viewModel by viewModels<OrderSettingViewModel> { PlaymateViewModelFactory() }

    override fun initViews() {
        super.initViews()
        binding.root.onClick {
            selectTimeRange()
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        viewModel.timeRangeLD.observe(viewLifecycleOwner) {
            val fromTime = it.first
            val toTime = it.second
            binding.tvDesc.text = if (fromTime.ordinal == toTime.ordinal) {
                //岂止时间相同,显示"全天"
                getCompatString(R.string.playmate_order_setting_time_all_day_desc)
            } else if (fromTime.ordinal < toTime.ordinal) {
                getCompatString(
                    R.string.playmate_order_setting_time_today_desc,
                    fromTime.time(),
                    toTime.time()
                )
            } else {
                getCompatString(
                    R.string.playmate_order_setting_time_tomorrow_desc,
                    fromTime.time(),
                    toTime.time()
                )
            }
        }
    }

    private fun selectTimeRange() {
        TimeRangeDialog().show(fragmentManager, "TimeRangeDialog")
    }
}