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

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.QrcodeViewBinding
import com.wdkl.app.ncs.callingbed.helper.AppUpdateHelper
import com.wdkl.app.ncs.callingbed.helper.NetHelper
import com.wdkl.app.ncs.callingbed.helper.ServerConfigDialogHelper
import com.wdkl.app.ncs.callingbed.launch.CallingbedLaunch
import com.wdkl.ncs.android.lib.base.BaseFragment
import com.wdkl.ncs.android.lib.utils.EcodeHelper
import com.wdkl.ncs.android.lib.vo.filter
import com.wdkl.ncs.android.middleware.api.UrlManager
import com.wdkl.ncs.android.middleware.common.Constant
import com.wdkl.ncs.android.middleware.logic.contract.callingbed.QrCodeFragmentContract
import com.wdkl.ncs.android.middleware.logic.presenter.callingbed.QrCodeFragmentPresenter
import com.wdkl.ncs.android.middleware.utils.StringUtil
import kotlinx.android.synthetic.main.qrcode_view.*
import kotlinx.android.synthetic.main.view_bed_name.*

class QrCodeFragment : BaseFragment<QrCodeFragmentPresenter, QrcodeViewBinding>(), QrCodeFragmentContract.View {
    val TAG = "QrCodeFragment"
    val QR_CODE_PATH = "http://m.wdklian.com/care/apk/care.user?type=NCS_DEVICE"

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

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

    override fun init() {
        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))
        }

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

        Thread{
            //val logoBitmap = BitmapFactory.decodeResource(resources, R.mipmap.erlogo)
            var builder = StringBuilder()
            builder.append(QR_CODE_PATH)
            builder.append("&code=")
            builder.append(Constant.DEVICE_CODE)
            builder.append("&mac=")
            builder.append(Constant.LOCAL_MAC)
            builder.append("&model=")
            builder.append(Constant.DEVICE_MODEL)
            builder.append("&hard_ver=")
            builder.append(Constant.DEVICE_HARD_VER)
            builder.append("&soft_ver=")
            builder.append(Constant.DEVICE_SOFT_VER)
            builder.append("&device_type=")
            builder.append(Constant.DEVICE_TYPE)
            builder.append("&device_name=")
            builder.append(Constant.DEVICE_NAME)
            val code = EcodeHelper().createQRImage(builder.toString(),180, null)
            activity.runOnUiThread {
                view_qr_code?.setImageBitmap(code)
            }
        }.start()

        val macAddr = NetHelper.getInstance().macAddress
        val ipAddr = NetHelper.getInstance().localIP
        if (Constant.DEVICE_STATUS == 0) {
            tv_device_id.text = "Device ID: " + Constant.DEVICE_ID + " - " + StringUtil.getResString(R.string.str_disable)
        } else if (Constant.DEVICE_STATUS == 1) {
            tv_device_id.text = "Device ID: " + Constant.DEVICE_ID + " - " + StringUtil.getResString(R.string.str_enable)
        } else {
            tv_device_id.text = "Device ID: " + Constant.DEVICE_ID
        }

        val serverIp =  UrlManager.build().base.substringAfterLast("//").substringBefore(":")
        tv_local_ip.text = "IP: " + ipAddr + " - " + serverIp
        tv_local_mac.text = "MAC: " + macAddr
        tv_app_version.text = "Version: V" + BuildConfig.VERSION_NAME + "_" + BuildConfig.VERSION_CODE
        tv_mcu_version.text = "MCU: " + Constant.MCU_VERSION_NUMBER
    }

    override fun bindEvent() {
        tv_btn_update_app.setOnClickListener {
            (activity as CallingbedActivity).checkAppVersion()
        }

        tv_btn_reboot.setOnClickListener {
            AppUpdateHelper.reboot(context)
        }

        tv_btn_server_config.setOnClickListener {
            ServerConfigDialogHelper.showPasswordDialog(activity)
        }
    }

    override fun 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 = {

        })
    }
}