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

import android.content.Intent
import android.text.TextUtils
import android.util.Log
import android.view.View
import com.enation.javashop.net.engine.model.NetState
import com.wdkl.app.ncs.callingbed.BuildConfig
import com.wdkl.app.ncs.callingbed.R
import com.wdkl.app.ncs.callingbed.activity.CallingbedActivity
import com.wdkl.app.ncs.callingbed.databinding.MainViewLayoutBinding
import com.wdkl.app.ncs.callingbed.helper.SerialPortHelper
import com.wdkl.app.ncs.callingbed.launch.CallingbedLaunch
import com.wdkl.app.ncs.callingbed.net.NettyClient
//import com.wdkl.app.ncs.sip.activity.VoipAudioActivity
import com.wdkl.ncs.android.lib.base.BaseFragment
import com.wdkl.ncs.android.lib.utils.*
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.MainFragmentContract
import com.wdkl.ncs.android.middleware.logic.presenter.callingbed.MainFragmentPresenter
import com.wdkl.ncs.android.middleware.model.vo.CustomerInfoVO
import com.wdkl.ncs.android.middleware.utils.StringUtil
import kotlinx.android.synthetic.main.main_view.*
import kotlinx.android.synthetic.main.main_view_layout.*
import kotlinx.android.synthetic.main.view_bed_name.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainFragment: BaseFragment<MainFragmentPresenter, MainViewLayoutBinding>(), MainFragmentContract.View{
    val TAG = "MainFragment"

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

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

    override fun init() {
        updateInfo()
    }

    override fun bindEvent() {
        //
    }

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

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

    override fun destory() {
        //
    }

    override fun showCustomInfo(customInfo: CustomerInfoVO) {
        //隐藏空页面并展示入住信息
        ll_empty.visibility = View.GONE
        ll_custom_view.visibility = View.VISIBLE

        //显示入住信息
        tv_custom_name.text = customInfo.named
        if (customInfo.sex == 0) {
            tv_custom_gender.setBackgroundResource(R.mipmap.ic_gender_female)
        } else if (customInfo.sex == 1) {
            tv_custom_gender.setBackgroundResource(R.mipmap.ic_gender_male)
        }
        tv_custom_age.text = "" + customInfo.age + customInfo.ageUnit
        if (customInfo.inDate != null) {
            tv_custom_indate.text = StringUtil.getResString(R.string.indate) + TimeHandle.getDateTime(customInfo.inDate * 1000, "yyyy.MM.dd")
        }
        if (TextUtils.isEmpty(customInfo.advice)) {
            tv_order_content.text = StringUtil.getResString(R.string.str_empty)
        } else {
            tv_order_content.text = customInfo.advice
        }

        if (Constant.doctorValid == 1) {
            ll_doctor_view.visibility = View.VISIBLE
            tv_doctor_title.text = Constant.doctorTitle
        } else {
            ll_doctor_view.visibility = View.GONE
        }

        if (Constant.nurseValid == 1) {
            ll_nurse_view.visibility = View.VISIBLE
            tv_nurse_title.text = Constant.nurseTitle
        } else {
            ll_nurse_view.visibility = View.GONE
        }

        tv_doctor_name.text = customInfo.doctorName
        tv_nurse_name.text = customInfo.nurseName

        //更新护理项
        if (customInfo.list != null) {
            AppTool.Time.delay(1800) {
                if (activity != null) {
                    (activity as CallingbedActivity).updateNurseConfig(customInfo.list)
                }
            }
        }
    }

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

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

    override fun start() {
        //
    }

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

        },onMobile = {

        },offline = {

        })
    }

    override fun onNoNet() {
        showMessage("No network")
    }

    private fun updateInfo() {
        if (Constant.DEVICE_STATUS == 0) {
            tv_bed_name.text = StringUtil.getResString(R.string.device_disable)
            tv_bed_name.setTextColor(resources.getColor(R.color.red_color))
        } else {
            tv_bed_name.text = Constant.BED_NAME
            tv_bed_name.setTextColor(resources.getColor(R.color.main_color))
        }

        if (Constant.CUSTOM_ID != -1) {
            ll_empty.visibility = View.GONE
            presenter.loadCustomInfo(Constant.CUSTOM_ID)
        }

        tv_version.text = "V"+BuildConfig.VERSION_NAME+"_"+BuildConfig.VERSION_CODE
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMoonEvent(messageEvent: MessageEvent) {
        when (messageEvent.getType()) {
            Constant.EVENT_UPDATE_CUSTOM -> {
                updateInfo()
            }
        }
    }
}