package com.adealink.weparty.commonui.widget.wheel

import android.os.Bundle
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.R
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.commonui.widget.wheel.WheelDatePickerFragment.Companion.EXTRA_CURRENT_DATE
import com.adealink.weparty.commonui.widget.wheel.WheelDatePickerFragment.Companion.EXTRA_INIT_DATE
import com.adealink.weparty.databinding.DialogWheelDatePickerBinding

/**
 * 日期选择控件；
 */
class WheelDatePickerDialog : BottomDialogFragment(R.layout.dialog_wheel_date_picker) {

    companion object {
        const val EXTRA_TIPS = "extra_tips"
        const val EXTRA_CONFIRM_BUTTON_TEXT = "extra_confirm_button_text"

        fun newInstance(
            initTimestamp: Long? = null,
            currentTimestamp: Long? = null,

            tips: String? = null,
            confirmButtonText: String? = null
        ): WheelDatePickerDialog {
            val fragment = WheelDatePickerDialog()
            val bundle = Bundle()
            bundle.putLong(EXTRA_INIT_DATE, initTimestamp ?: System.currentTimeMillis())
            bundle.putLong(EXTRA_CURRENT_DATE, currentTimestamp ?: System.currentTimeMillis())
            bundle.putString(EXTRA_TIPS, tips)
            confirmButtonText?.let {
                bundle.putString(EXTRA_CONFIRM_BUTTON_TEXT, confirmButtonText)
            }
            fragment.arguments = bundle
            return fragment
        }
    }

    private val binding by viewBinding(DialogWheelDatePickerBinding::bind)

    private var title: String = getCompatString(R.string.common_choose_date)
    private var confirmButtonText: String = getCompatString(R.string.commonui_confirm)

    var selectBirthdayCallback: ISelectDateCallback? = null

    private lateinit var datePickerFragment: WheelDatePickerFragment

    private fun parseIntent() {
        arguments?.getString(EXTRA_CONFIRM_BUTTON_TEXT)?.let {
            confirmButtonText = it
        }
        arguments?.getString(EXTRA_TIPS)?.let {
            title = it
        }
    }


    override fun initViews() {
        parseIntent()
        binding.tvTitle.text = title
        binding.btnConfirm.text = confirmButtonText

        datePickerFragment = WheelDatePickerFragment().apply {
            arguments = this@WheelDatePickerDialog.arguments
        }
        childFragmentManager.beginTransaction()
            .replace(binding.flDatePicker.id, datePickerFragment, "WheelDatePickerFragment")
            .commitAllowingStateLoss()

        binding.btnConfirm.onClick {
            datePickerFragment.getSelectedDate({ timestamp: Long, timeStr: String ->
                selectBirthdayCallback?.onSelectComplete(timestamp, timeStr)
            })
            dismiss()
        }
    }

    interface ISelectDateCallback {
        /**
         * @param timeStr "yyyy-MM-dd" 格式 或者“yyyy-MM-DD-HH-MM-SS”
         */
        fun onSelectComplete(timestamp: Long, timeStr: String)
    }
}