package com.wdkl.app.ncs.callingbed.fragment

import android.os.CountDownTimer
import com.enation.javashop.net.engine.model.NetState
import com.wdkl.app.ncs.callingbed.R
import com.wdkl.app.ncs.callingbed.databinding.CallCareViewBinding
import com.wdkl.app.ncs.callingbed.launch.CallingbedLaunch
import com.wdkl.ncs.android.lib.base.BaseFragment
import com.wdkl.ncs.android.lib.utils.debugLog
import com.wdkl.ncs.android.lib.vo.filter
import com.wdkl.ncs.android.middleware.common.Constant
import com.wdkl.ncs.android.middleware.common.MessageEvent
import com.wdkl.ncs.android.middleware.logic.contract.callingbed.CallCareFragmentContract
import com.wdkl.ncs.android.middleware.logic.presenter.callingbed.CallCareFragmentPresenter
import kotlinx.android.synthetic.main.call_care_view.*
import org.greenrobot.eventbus.EventBus

class CallCareFragment : BaseFragment<CallCareFragmentPresenter, CallCareViewBinding>(), CallCareFragmentContract.View {
    val TAG = "CallCareFragment"

    //呼叫倒计时
    lateinit var countDownTimer: CountDownTimer

    override fun getLayId(): Int {
        return R.layout.call_care_view
    }

    override fun bindDagger() {
        CallingbedLaunch.component.inject(this)
    }

    override fun init() {
        initCountDownTimer()
        countDownTimer.start()
    }

    override fun bindEvent() {
        //debugLog(TAG,"bindEvent")
    }

    override fun destory() {
        countDownTimer.cancel()
        //debugLog(TAG,"destory")
    }

    override fun onError(message: String, type: Int) {
        //
    }

    override fun complete(message: String, type: Int) {
        //
    }

    override fun start() {
        //
    }

    override fun networkMonitor(state: NetState) {
        state.filter(onWifi = {

        },onMobile = {

        },offline = {

        })
    }

    fun initCountDownTimer() {
        countDownTimer = object: CountDownTimer(Constant.CALL_TIMEOUT*1000L, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                val time = millisUntilFinished/1000
                view_call_layout_tv_call_timeout?.setText("" + time + " 秒")
            }

            override fun onFinish() {
                //呼叫超时时间到，返回到主界面
                EventBus.getDefault().post(MessageEvent("BackToMain", Constant.EVENT_BACK_MAIN))
            }
        }
    }
}