package com.wdkl.app.ncs.callingbed.activity

import android.text.TextUtils
import android.util.Log
import com.enation.javashop.android.jrouter.external.annotation.Router
import com.enation.javashop.net.engine.model.NetState
import com.wdkl.app.ncs.callingbed.R
import com.wdkl.app.ncs.callingbed.databinding.UpdateLayBinding
import com.wdkl.app.ncs.callingbed.helper.AppUpdateHelper
import com.wdkl.app.ncs.callingbed.helper.HttpHelper
import com.wdkl.app.ncs.callingbed.launch.CallingbedLaunch
import com.wdkl.ncs.android.lib.base.BaseActivity
import com.wdkl.ncs.android.lib.utils.showMessage
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.AppUpdateContract
import com.wdkl.ncs.android.middleware.logic.presenter.callingbed.AppUpdatePresenter
import com.wdkl.ncs.android.middleware.utils.StringUtil
import kotlinx.android.synthetic.main.update_lay.*

@Router(path = "/callingbed/update")
class AppUpdateActivity :BaseActivity<AppUpdatePresenter, UpdateLayBinding>(), AppUpdateContract.View {
    private val TAG = "AppUpdateActivity"

    private val urlManager = UrlManager.build()

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

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

    override fun init() {
        if (!TextUtils.isEmpty(Constant.APP_PATH)) {
            val downloadPath = urlManager.base + Constant.APP_PATH
            Log.e(TAG, "start download: $downloadPath")
            downLoadAPK(downloadPath)
        } else {
            showMessage(R.string.download_error)
            finish()
        }
    }

    /**
     * 下载APK包
     */
    fun downLoadAPK(url: String) {
        Log.d(TAG, "downLoadAPK  url==$url")
        activity_appupdate_dialog_progressview.setCurProgress(0)
        HttpHelper.download(url, object : HttpHelper.DownloadListener {
            override fun onDownloadSuccess() {
                Log.d("download", "onDownloadSuccess==" + "成功")
                runOnUiThread {
                    activity_calling_bed_text_download.text = StringUtil.getResString(R.string.updating)
                }
                startInstallApk()
            }

            override fun onDownloading(progress: Int) {
                runOnUiThread {
                    activity_appupdate_dialog_progressview.setCurProgress(progress)
                }
            }

            override fun onDownloadFailed() {
                Log.d("download", "onDownloadFailed==" + "失败")
                finish()
            }
        })
    }

    fun startInstallApk() {
        Thread{
            AppUpdateHelper.updateApp(this, object : AppUpdateHelper.UpdateCallBack {
                override fun onFailed() {
                    runOnUiThread {
                        showMessage(R.string.update_fail)
                        finish()
                    }
                }

                override fun onSuccess() {
                    runOnUiThread {
                        showMessage(R.string.update_success)
                        //finish()
                        android.os.Process.killProcess(android.os.Process.myPid())
                        System.exit(0)
                    }
                }
            })
        }.start()
    }

    override fun bindEvent() {
        //
    }

    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(onMobile = {

        },onWifi = {

        },offline = {

        })
    }
}