package com.gh.vspace.gapps

import android.animation.ValueAnimator
import android.app.Dialog
import android.os.Build
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.view.View
import android.view.animation.DecelerateInterpolator
import androidx.core.text.bold
import androidx.core.text.color
import com.gh.common.util.NewFlatLogUtils
import com.gh.download.simple.*
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.BaseBottomDialogFragment
import com.gh.gamecenter.common.utils.EnvHelper
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.core.AppExecutor
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.core.runOnUiThread
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.core.utils.UnZipUtils
import com.gh.gamecenter.databinding.DialogGappsDownloadBinding
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.vspace.VHelper
import com.lg.download.DownloadError
import com.lg.download.DownloadStatus
import com.lg.download.httpclient.DefaultHttpClient
import com.lg.ndownload.DownloadConfigBuilder
import com.lightgame.download.FileUtils
import java.io.File

class GAppsDownloadDialogFragment : BaseBottomDialogFragment<DialogGappsDownloadBinding>() {

    private var mIsInstalling = false // 是否正在安装，避免重复触发
    private var mRequiredAutoInstall = false // 是否需要自动安装（下载完成后自动安装）
    private var mInstallSuccess = false // 安装是否已经成功
    private var mInstallProgressAnimator: ValueAnimator? = null

    private var mGameId = ""
    private var mGameName = ""
    private var mTriggerPackageName = ""

    private var mGAppsDownloadAndInstallStartTimestampMills = 0L // 谷歌框架下载开始的时间戳
    private var mGAppsDownloadAndInstallElapsedTimeMills = 0L // 谷歌框架下载开始已用的时间（仅下载暂停时记录）

    private val mGAppsUnZipDestPath by lazy {
        requireContext().filesDir.absolutePath + File.separator + "gapps" + File.separator
    }

    private val mGAppsUnZipDestFile by lazy {
        File(mGAppsUnZipDestPath).also { if (!it.exists()) it.mkdir() }
    }

    private val mBatchInstallListener: (isSuccess: Boolean) -> Unit = { isSuccess ->
        val elapsedTime =
            (System.currentTimeMillis() - mGAppsDownloadAndInstallStartTimestampMills + mGAppsDownloadAndInstallElapsedTimeMills) / 1000

        mGAppsDownloadAndInstallStartTimestampMills = 0L
        mGAppsDownloadAndInstallElapsedTimeMills = 0L

        if (isSuccess) {
            showSuccessStyle()
            mInstallSuccess = true
            NewFlatLogUtils.logGAppsInstallSuccess(mGameId, mGameName, elapsedTime.toInt())
            clearCaches()
        } else {
            mInstallSuccess = false
            NewFlatLogUtils.logGAppsInstallFailed(mGameId, mGameName, elapsedTime.toInt())
            resetDownloadStatus(null)
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return super.onCreateDialog(savedInstanceState).also { it.setCanceledOnTouchOutside(false) }
    }

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

        mGameId = arguments?.getString(GAME_ID) ?: ""
        mGameName = arguments?.getString(GAME_NAME) ?: ""
        mTriggerPackageName = arguments?.getString(TRIGGER_PACKAGE_NAME) ?: ""

        VHelper.registerBatchInstallListener(mBatchInstallListener)

        showDownloadStyle()
        updateViewByDownload(getDownloadEntity())

        AutoUnregisteredSimpleDownloadListener(VHelper.G_APPS_DOWNLOAD_ID, this, object : DownloadListener {
            override fun onError(error: DownloadError) {
                ToastUtils.toast(error.toString())
            }

            override fun onProgress(progress: Float) {
                mBinding.downloadButton.progress = progress.toInt() * 5
                mBinding.downloadButton.showProgress = true
            }

            override fun onSizeReceived(fileSize: Long) {
                // do nothing
            }

            override fun onStatusChanged(status: DownloadStatus) {
                updateViewByDownloadStatus(status)
            }

            override fun onSpeedChanged(speed: Float) {
                // do nothing
            }
        })

        NewFlatLogUtils.logGAppsDialogShowed(mGameId, mGameName)
    }

    private fun updateViewByDownload(downloadEntity: SimpleDownloadEntity?) {
        if (downloadEntity != null) {
            updateViewByDownloadStatus(downloadEntity.status)
        } else {
            updateViewByDownloadStatus(DownloadStatus.UNKNOWN)
        }
    }

    private fun updateViewByDownloadStatus(status: DownloadStatus) {
        when (status) {
            DownloadStatus.COMPLETED -> {
                val downloadEntity = getDownloadEntity()

                if (downloadEntity == null) {
                    ToastUtils.toast("下载异常，需要重新下载")
                    SimpleDownloadManager.cancel(VHelper.G_APPS_DOWNLOAD_ID)
                    return
                }

                if (mGAppsUnZipDestFile.listFiles()?.size != 3) {
                    // 畅玩功能仅支持 7.1 以后的设备，所以这里的这个判断条件不需要 else
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        runOnIoThread {
                            try {
                                UnZipUtils.unzip(
                                    File(downloadEntity.dirPath + downloadEntity.fileName),
                                    mGAppsUnZipDestPath
                                )
                                batchInstall(mGAppsUnZipDestFile)
                            } catch (e: Exception) {
                                resetDownloadStatus(e)
                            }
                        }
                    }
                } else if (mRequiredAutoInstall && !mIsInstalling) {
                    batchInstall(mGAppsUnZipDestFile)
                }

                mBinding.downloadButton.text = "安装中"
                mBinding.downloadButton.buttonStyle = DownloadButton.ButtonStyle.DOWNLOADING_NORMAL

                mBinding.downloadButton.setOnClickListener {
                    if (mInstallSuccess) {
                        VHelper.launch(requireContext(), mTriggerPackageName)
                        NewFlatLogUtils.logGAppsInstalledAndLaunchGame(mGameId, mGameName)
                        dismiss()
                    } else {
                        ToastUtils.toast("正在安装中，请稍候")
                    }
                }
            }

            DownloadStatus.UNKNOWN,
            DownloadStatus.CANCELLED -> {
                mBinding.downloadButton.buttonStyle = DownloadButton.ButtonStyle.NORMAL
                mBinding.downloadButton.text = "安装"
                mBinding.downloadButton.setOnClickListener {
                    val freeSpace = FileUtils.getFreeSpaceByPath(requireContext().filesDir.absolutePath)
                    // 系统少于 600M 可用空间，不允许下载。因为下载要 100M，解压 150M，安装估计也要不少空间
                    if (freeSpace < 600) {
                        ToastUtils.toast("设备存储空间不足，请稍后再试")
                    }

                    NewFlatLogUtils.logGAppsDialogClicked(mGameId, mGameName, "安装")

                    // 记录当前时间戳，供统计最终安装完成耗时用
                    mGAppsDownloadAndInstallStartTimestampMills = System.currentTimeMillis()

                    createDownload()
                    mRequiredAutoInstall = true
                }
            }

            DownloadStatus.DOWNLOADING -> {
                mBinding.downloadButton.buttonStyle = DownloadButton.ButtonStyle.DOWNLOADING_NORMAL
                mBinding.downloadButton.text = "暂停"
                mBinding.cancelTv.visibility = View.GONE
                mBinding.downloadButton.setOnClickListener {
                    SimpleDownloadManager.pause(VHelper.G_APPS_DOWNLOAD_ID)
                }

                // 恢复下载时更新下载开始时间
                if (mGAppsDownloadAndInstallStartTimestampMills == 0L) {
                    mGAppsDownloadAndInstallStartTimestampMills = System.currentTimeMillis()
                }
            }

            DownloadStatus.AUTOPAUSED,
            DownloadStatus.WAITINGWIFI,
            DownloadStatus.QUEUED,
            DownloadStatus.PAUSED -> {
                mBinding.downloadButton.buttonStyle = DownloadButton.ButtonStyle.LAUNCH_OR_OPEN
                mBinding.downloadButton.text = "继续"

                // 统计暂停时已经用了的下载时长
                mGAppsDownloadAndInstallElapsedTimeMills =
                    System.currentTimeMillis() - mGAppsDownloadAndInstallStartTimestampMills
                mGAppsDownloadAndInstallStartTimestampMills = 0L

                mBinding.downloadButton.setOnClickListener {
                    try {
                        SimpleDownloadManager.resume(VHelper.G_APPS_DOWNLOAD_ID)
                    } catch (e: Exception) {
                        createDownload()
                    }
                }
            }
        }
    }

    /**
     * 创建下载任务
     */
    private fun createDownload() {
        SimpleDownloadManager.download(
            DownloadConfigBuilder()
                .setUniqueId(VHelper.G_APPS_DOWNLOAD_ID)
                .setFileName(VHelper.G_APPS_DOWNLOAD_ID + ".zip")
                .apply {
                    if (EnvHelper.isDevEnv) {
                        setUrl(VHelper.G_APPS_DOWNLOAD_DEV_URL)
                    } else {
                        setUrl(VHelper.G_APPS_DOWNLOAD_URL)
                    }
                }
                .setPathToStore(requireContext().filesDir.absolutePath + File.separator)
                .setHttpClient(DefaultHttpClient())
                .setDownloadThreadSize(2)
                .setDownloadListener(DownloadMessageHandler)
                .setDownloadExecutor(AppExecutor.ioExecutor)
                .build()
        )
    }

    private fun getDownloadEntity(): SimpleDownloadEntity? {
        return DownloadMessageHandler.findEntity(VHelper.G_APPS_DOWNLOAD_ID)
    }

    private fun showSuccessStyle() {
        mInstallProgressAnimator?.end()
        mBinding.downloadButton.progress = 1000
        mBinding.downloadButton.text = "启动游戏"
        mBinding.downloadButton.buttonStyle = DownloadButton.ButtonStyle.LAUNCH_OR_OPEN
        mBinding.successIv.visibility = View.VISIBLE
        mBinding.cancelTv.visibility = View.GONE
        mBinding.descTv1.visibility = View.GONE
        mBinding.descTv2.text = "谷歌框架已安装成功，可启动游戏体验~"
    }

    private fun showDownloadStyle() {
        mBinding.cancelTv.setOnClickListener {
            SimpleDownloadManager.cancel(VHelper.G_APPS_DOWNLOAD_ID)
            NewFlatLogUtils.logGAppsDialogClicked(mGameId, mGameName, "暂不安装")
            VHelper.launch(requireContext(), mTriggerPackageName, true)
            dismiss()
        }
        mBinding.cancelTv.visibility = View.VISIBLE
        mBinding.descTv1.text = SpannableStringBuilder()
            .append("游戏需要安装")
            .bold { color(R.color.text_subtitle.toColor(requireContext())) { append(" 谷歌框架 ") } }
            .append("后才能稳定运行，为了您的游戏体验，请先安装！")
        mBinding.descTv2.text = "下载过程中请勿退出光环助手，以免影响下载进度，若存在问题请反馈至客服，我们将及时解决"
    }

    /**
     * 批量安装
     */
    private fun batchInstall(destFile: File) {
        val installFileMap = hashMapOf<String, File>()
        destFile.listFiles()?.forEach { installFileMap[it.nameWithoutExtension] = it }

        context?.let {
            mIsInstalling = true
            VHelper.batchInstall(it, installFileMap)
        }

        runOnUiThread {
            mInstallProgressAnimator = ValueAnimator.ofInt(500, 1000)
            mInstallProgressAnimator?.duration = 10 * 1000L
            mInstallProgressAnimator?.interpolator = DecelerateInterpolator()
            mInstallProgressAnimator?.addUpdateListener {
                mBinding.downloadButton.progress = it.animatedValue as Int
            }
            mInstallProgressAnimator?.start()
        }
    }

    private fun resetDownloadStatus(e: Exception?) {
        // 遇到异常，删除下载任务和解压内容，重新下载
        mGAppsUnZipDestFile.deleteRecursively()
        SimpleDownloadManager.cancel(VHelper.G_APPS_DOWNLOAD_ID)

        runOnUiThread {
            // 重置 UI
            updateViewByDownloadStatus(DownloadStatus.UNKNOWN)
        }
        ToastUtils.toast(e?.localizedMessage ?: "安装失败")
    }

    /**
     * 清理下载、解压缓存
     */
    private fun clearCaches() {
        runOnIoThread {
            SimpleDownloadManager.cancel(VHelper.G_APPS_DOWNLOAD_ID)
            mGAppsUnZipDestFile.deleteRecursively()
        }
    }

    override fun onDestroy() {
        super.onDestroy()

        VHelper.registerBatchInstallListener(null)
    }

    companion object {

        private const val TRIGGER_PACKAGE_NAME = "trigger_package_name" // 触发弹窗的原始游戏包名
        private const val GAME_ID = "game_id"
        private const val GAME_NAME = "game_name"

        fun getInstance(packageName: String, gameId: String, gameName: String): GAppsDownloadDialogFragment {
            return GAppsDownloadDialogFragment().also {
                it.arguments = Bundle().apply {
                    putString(TRIGGER_PACKAGE_NAME, packageName)
                    putString(GAME_ID, gameId)
                    putString(GAME_NAME, gameName)
                }
            }
        }
    }

}