package com.hongyegroup.cpt_pensonal.ui.activity

import android.content.Intent
import android.os.Bundle
import com.guadou.cs_cptservices.base.activity.YYBaseVDBActivity
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.ext.vertical
import com.guadou.lib_baselib.utils.CommUtils
import com.guadou.lib_baselib.utils.DateAndTimeUtil
import com.hongyegroup.cpt_pensonal.BR
import com.hongyegroup.cpt_pensonal.R
import com.hongyegroup.cpt_pensonal.bean.MakeAppointmentTime
import com.hongyegroup.cpt_pensonal.databinding.ActivityPensonalAppointmentMakeBinding
import com.hongyegroup.cpt_pensonal.mvvm.PensonalAppointmentMakeViewModel

/**
 * 个人用户 开始预约时间
 */
class PensonalAppointmentMakeActivity : YYBaseVDBActivity<PensonalAppointmentMakeViewModel, ActivityPensonalAppointmentMakeBinding>() {

    companion object {
        fun startInstance() {
            val context = CommUtils.getContext()
            val intent = Intent(context, PensonalAppointmentMakeActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.activity_pensonal_appointment_make)
            .addBindingParams(BR.click, ClickProxy())
    }

    override fun startObserve() {

    }

    override fun init(savedInstanceState: Bundle?) {

        initRV()
        initData()
        initListener()
    }

    private fun initData() {
        mViewModel.mDatas.add(MakeAppointmentTime().apply {
            time = "08:30"
            isSelect = true
        })

        for (i in 1..10) {
            mViewModel.mDatas.add(MakeAppointmentTime().apply {
                time = "18:30"
                isSelect = false
            })
        }

        mViewModel.mAdapter.notifyDataSetChanged()
    }


    private fun initListener() {

        //Item点击选中指定的Item
        mViewModel.mAdapter.addChildClickViewIds(R.id.tv_appointment_time)
        mViewModel.mAdapter.setOnItemChildClickListener { _, _, position ->
            val item = mViewModel.mDatas[position]
            resetAllItemChoose()
            item.isSelect = true

            mViewModel.mAdapter.notifyDataSetChanged()
        }
    }

    //重置全部选中的
    private fun resetAllItemChoose() {
        mViewModel.mDatas.forEach {
            it.isSelect = false
        }
    }

    private fun initRV() {
        mBinding.recyclerView.vertical(3).adapter = mViewModel.mAdapter
    }

    /**
     * DataBinding事件处理
     */
    inner class ClickProxy {

        //选择日期
        fun pickerData() {
            mViewModel.showPikeDateDialog(mActivity).observe(this@PensonalAppointmentMakeActivity) {
                it?.let {
                    mBinding.tvSelectDate.text = DateAndTimeUtil.stampToDate(it)
                }
            }
        }

        //点击确定预约
        fun doSubmit() {
            PensonalAppointmentSuccessActivity.startInstance()
            finish()
        }

    }

}