package com.wdkl.lcd_board.activity

import android.content.ComponentName
import android.content.Intent
import android.os.Build
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.lcd_board.R
import com.wdkl.lcd_board.databinding.UpdateLayBinding
import com.wdkl.lcd_board.helper.AppUpdateHelper
import com.wdkl.lcd_board.helper.HttpHelper
import com.wdkl.lcd_board.launch.MainLaunch
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.conversion_box.AppUpdateContract
import com.wdkl.ncs.android.middleware.logic.presenter.conversion_box.AppUpdatePresenter
import kotlinx.android.synthetic.main.update_lay.*


@Router(path = "/lcd_board/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() {
        MainLaunch.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("下载路径异常")
            finish()
        }
    }

    /**
     * 下载APK包
     */
    fun downLoadAPK(url: String) {
        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 = "正在安装，请稍后..."
                }
                if (Build.BOARD.equals("rk30sdk")) {
                    startInstallApk()
                } else {
                    startInstallApp()
                }
            }

            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("升级失败")
                        finish()
                    }
                }

                override fun onSuccess() {
                    runOnUiThread {
                        showMessage("升级成功")
                        finish()
                        //android.os.Process.killProcess(android.os.Process.myPid())
                        //System.exit(0)
                    }
                }
            })
        }.start()
    }

    fun startInstallApp() {
        val path = AppUpdateHelper.FILE_APK_PATH + "/" + AppUpdateHelper.FILE_APK_NAME
        val intent = Intent("rk.android.apkinstall")
        intent.putExtra("instll_path", path)
        intent.putExtra("commit_start", true)
        if (Build.VERSION.SDK_INT >= 26) {
            val componentName = ComponentName("com.android.packageinstaller", "com.android.packageinstaller.SilentInstallerReceiver")
            intent.setComponent(componentName)
        }
        sendBroadcast(intent)
    }

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

        })
    }
}