package com.module.backstage.fragment.date

import android.util.Log
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.hboxs.serialport.plc.DialogClickListener
import com.module.backstage.adapter.OrderDataAdapter
import com.module.backstage.databinding.BackstageFragmentOrderDataBinding
import com.module.backstage.dialog.DateDialog
import com.module.backstage.vm.OrderDataFragmentVM
import com.module.database.room.entity.LocalOrderBean
import com.quyunshuo.androidbaseframemvvm.base.ktx.observeLiveData
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
import com.quyunshuo.androidbaseframemvvm.common.util.ToastUtil
import dagger.hilt.android.AndroidEntryPoint

/**
 * 订单明细查看Fragment
 *
 * @author Qu Yunshuo
 * @since 2021/8/1 11:46 下午
 */
@AndroidEntryPoint
class OrderDataFragment : BaseFragment<BackstageFragmentOrderDataBinding, OrderDataFragmentVM>(),
    View.OnClickListener {

    private var hour: Int = 0;
    private var day: Int = 1;
    private var month: Int = 2;
    private var year: Int = 3;
    private var unit: Int = hour
    private var startTime: String = ""
    private var endTime: String = ""
    private var dataAdapter: OrderDataAdapter? = null

    private val TAG = "DataStatFragment"
    override val mViewModel: OrderDataFragmentVM by viewModels()
    override fun createVB() = BackstageFragmentOrderDataBinding.inflate(layoutInflater)
    var dateStatFragment: DateDialog? = null


    override fun BackstageFragmentOrderDataBinding.initView() {
        tvEndTime.setOnClickListener(this@OrderDataFragment)
        tvStartTime.setOnClickListener(this@OrderDataFragment)
        tvScreen.setOnClickListener(this@OrderDataFragment)
        rgFancy.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                mBinding.rbHour.id -> {
                    unit = hour
                }

                mBinding.rbYear.id -> {
                    unit = year
                }

                mBinding.rbDay.id -> {
                    unit = day
                }

                mBinding.rbMonth.id -> {
                    unit = month
                }

                else -> {}
            }
            mBinding.tvStartTime.text = "请选择开始时间"
            mBinding.tvEndTime.text = "请选择结束时间"
            startTime = ""
            endTime = ""

        }
        rvAlarm.layoutManager = LinearLayoutManager(this@OrderDataFragment.context)

    }

    override fun initObserve() {
        observeLiveData(mViewModel.allLocalOrder, ::allLocalOrder)
    }

    fun allLocalOrder(list: MutableList<LocalOrderBean>) {
        Log.d(TAG, "allLocalOrder: " + list)
        mBinding.tvErrorTips.visibility = View.GONE
        dataAdapter = list.let { OrderDataAdapter(it) }

        dataAdapter?.animationEnable = true//开启动画
        dataAdapter?.isAnimationFirstOnly =true// 如果你只想在第一次加载数据时显示动画，可以设置isFirstOnly为true
        mBinding.rvAlarm.adapter = dataAdapter
    }

    override fun initRequestData() {
        //查询今日数据
        mViewModel.screen(mViewModel.todoTime(0), mViewModel.todoTime(1), unit)


    }


    override fun onClick(v: View?) {
        when (v!!.id) {
            mBinding.tvStartTime.id -> {
                showDateDialog(true)
            }

            mBinding.tvEndTime.id -> {
                showDateDialog(false)
            }

            mBinding.tvScreen.id -> {
                if (startTime == "" || endTime == "") {
                    ToastUtil.switchToastStyleToWarn("请选择时间")
                    return
                }

                if (startTime == endTime) {
                    ToastUtil.switchToastStyleToWarn("请不要输入相同的时间")
                    return
                }
                //开始搜索
                mViewModel.screen(startTime, endTime,unit)
            }

            else -> {}
        }
    }

    private fun showDateDialog(isStartTime: Boolean) {
        if (dateStatFragment == null) {
            dateStatFragment = this@OrderDataFragment.context?.let { DateDialog(it) }
        }
        dateStatFragment?.setListener(object : DialogClickListener {
            override fun onClickListener(type: Int, text: String?) {
                when (type) {
                    dateStatFragment?.type1 -> {
                        if (isStartTime) {
                            mBinding.tvStartTime.text = text
                            startTime = text!!
                        } else {
                            mBinding.tvEndTime.text = text
                            endTime = text!!
                        }
                        Log.d(TAG, "text: " + text)
                    }

                    else -> {}
                }
            }

        })
        dateStatFragment?.type = unit
        dateStatFragment?.show()
    }
    override fun onDestroyView() {
        mBinding.rvAlarm.adapter = null
        super.onDestroyView()
    }
}