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.activity.CallingbedActivity
import com.wdkl.app.ncs.callingbed.databinding.CallNurseViewBinding
import com.wdkl.app.ncs.callingbed.launch.CallingbedLaunch
import com.wdkl.ncs.android.lib.base.BaseFragment
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.CallNurseFragmentContract
import com.wdkl.ncs.android.middleware.logic.presenter.callingbed.CallNurseFragmentPresenter
import kotlinx.android.synthetic.main.call_nurse_view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class CallNurseFragment : BaseFragment<CallNurseFragmentPresenter, CallNurseViewBinding>(), CallNurseFragmentContract.View {
    val TAG = "CallNurseFragment"

    //呼叫状态：0-呼叫中，1-接通，2-挂断
    var callState : Int = 1
    //呼叫倒计时
    lateinit var countDownTimer: CountDownTimer

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

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

    override fun init() {
        initCountDownTimer()
        when (callState) {
            0 -> {
                view_nurse_layout_tv_call_text.setText("正在呼叫，等待接听中...")
                countDownTimer.start()
            }
            1 -> {
                view_nurse_layout_tv_call_text.setText("通话中...")
                view_nurse_layout_tv_call_timeout.setText("")
            }
            2 -> {
                //挂断
            }
        }
    }

    override fun onStart() {
        EventBus.getDefault().register(this)
        super.onStart()
    }

    override fun onStop() {
        EventBus.getDefault().unregister(this)
        super.onStop()
    }

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

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

    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_nurse_layout_tv_call_timeout?.setText("" + time + " 秒")
            }

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

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMoonEvent(messageEvent: MessageEvent) {
        when (messageEvent.getType()) {
            //通话状态状态
            Constant.EVENT_SIP_CALL_STATUS -> {
                run {
                    val value = messageEvent.message as Int
                    if (value == 0) {
                        countDownTimer.cancel()
                        //分机呼叫主机，主机接听会向分机发起通话请求，分机收到通话请求自动接听
                        view_nurse_layout_tv_call_text?.setText("通话中...")
                        view_nurse_layout_tv_call_timeout?.setText("")
                    }
                }
            }
        }
    }
}