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

import android.os.Bundle
import android.os.CountDownTimer
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.enation.javashop.utils.base.tool.BaseToolActivity
import com.wdkl.app.ncs.callingbed.R
import com.wdkl.app.ncs.callingbed.helper.RingPlayHelper
import com.wdkl.app.ncs.callingbed.settings.SettingConfig
import com.wdkl.ncs.android.lib.base.BaseApplication
import com.wdkl.ncs.android.lib.utils.showMessage
import com.wdkl.ncs.android.middleware.common.Constant
import com.wdkl.ncs.android.middleware.common.MessageEvent
import com.wdkl.ncs.android.middleware.tcp.channel.VoiceUtil
import com.wdkl.ncs.android.middleware.tcp.dto.TcpModel
import org.greenrobot.eventbus.EventBus

abstract class BaseCallFragment: Fragment() {

    private var layout: View? = null

    protected lateinit var baseActivity: BaseToolActivity

    //通话状态：0-去电, 1-来电, 2-探视
    protected var callState : Int = 0
    protected var tcpModel: TcpModel? = null

    //计时器
    lateinit var countDownTimer: CountDownTimer


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        retainInstance = true
        callState = arguments.getInt("call_state")
        if (arguments.getSerializable("tcp_model") != null) {
            tcpModel = arguments.getSerializable("tcp_model") as TcpModel
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (layout == null) {
            layout = inflater.inflate(getLayId(), null)
        }

        /**初始化宿主Activity*/
        baseActivity = getActivity() as BaseToolActivity

        return layout
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        init()
        bindEvent()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        destroy()
    }

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

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

    protected abstract fun getLayId(): Int

    protected abstract fun init()

    protected abstract fun bindEvent()

    protected abstract fun destroy()

    protected abstract fun callEnd(handoff: Boolean)

    //初始化计时器
    protected fun initCountDownTimer(view: TextView) {
        val overTime = SettingConfig.getSipOverTime(baseActivity) * 1000L
        countDownTimer = object: CountDownTimer(overTime, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                if (view != null) {
                    val time = millisUntilFinished/1000
                    view.setText(BaseApplication.appContext.getString(R.string.countdown_time, time))
                }
            }

            override fun onFinish() {
                //呼叫超时，返回到主界面
                RingPlayHelper.stopRingTone()
                showMessage(R.string.no_response)
                Constant.CALL_STATE = Constant.CALL_STANDBY
                VoiceUtil.cancelAudioCall(Constant.DEVICE_ID)
                //backToMain()
                callEnd(false)
            }
        }
    }

    //开始计时
    protected fun startTimer() {
        if (countDownTimer != null) {
            countDownTimer.start()
        }
    }

    //取消计时器
    protected fun cancelTimer() {
        if (countDownTimer != null) {
            countDownTimer.cancel()
        }
    }

    //返回主界面
    protected fun backToMain() {
        EventBus.getDefault().post(MessageEvent("BackCall", Constant.EVENT_REMOVE_CALL_FRAGMENT))
    }

}