package com.sixin.module_p.ui.enter.home

import android.app.DatePickerDialog
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.observe
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.ktx.launchCollect
import com.lukouguoji.module_base.ktx.toInt
import com.lukouguoji.module_base.ktx.toRequestBody
import com.sixin.module_p.ui.gnc.shouyun.dialog.PdaDaiShouYunFilterDialogModel
import dev.DevUtils
import dev.utils.common.DateUtils

class HomeViewModel : BasePageViewModel() {


    // 日期
    val date = MutableLiveData(DateUtils.formatDate(DateUtils.getCurrentTime(), "yyyy-MM-dd"))

    // 类型
    val type = MutableLiveData(TYPE_LEAVE)

    // 航班类型- 0:国内, 1:国际
    val countryType = MutableLiveData(false)

    private val datePicker by lazy {
        DatePickerDialog(getTopActivity()).apply {
            setOnDateSetListener { _, year, month, dayOfMonth ->
                date.value = "${year}-${month + 1}-${dayOfMonth}"
                refresh()
            }
        }
    }

    private val configModel = HomeConfigDialogModel {
        getData()
    }
    ///////////////////////////////////////////////////////////////////////////
    // 方法区
    ///////////////////////////////////////////////////////////////////////////

    fun onInit() {
        countryType.observe(DevUtils.getTopActivity() as LifecycleOwner) {
            refresh()
        }
    }

    override fun getData() {
        launchCollect({
            NetApply.api.getFlightList(
                mapOf(
                    "page" to pageModel.page,
                    "limit" to pageModel.limit,
                    "fdate" to date.value,
                    "status" to type.value,
                    "countryType" to countryType.value.toInt(),
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
            }
        }
    }

    fun onConfigClick() {
        configModel.show()
    }

    /**
     * 日期点击
     */
    fun onDateClick() {
        datePicker.show()
    }

    /**
     * 类型 改变
     */
    fun onTypeChange(type: Int) {
        this.type.value = type
        refresh()
    }

    companion object {
        // 出发
        const val TYPE_LEAVE = 0

        // 到达
        const val TYPE_ARRIVAL = 1

        // 国内
        const val TYPE_GN = 0

        // 国际
        const val TYPE_GJ = 1
    }

}