package com.gh.download.simple

import com.lg.download.DownloadError
import com.lg.download.DownloadStatus
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.roundTo
import com.gh.gamecenter.common.utils.setDebouncedClickListener
import com.gh.gamecenter.core.utils.ToastUtils
import com.lightgame.utils.Utils

open class DownloadButtonHelper(
    private val mDownloadId: String,
    private val mProgressView: SimpleProgressView,
) : DownloadListener {

    private var mProgressText = ""

    init {
        mProgressView.setTag(R.string.app_name, mDownloadId)
        mProgressView.setTextSize(12F.dip2px().toFloat())
    }

    override fun onError(error: DownloadError) {
        Utils.log(error)
    }

    override fun onProgress(progress: Float) {
        if (mProgressView.getTag(R.string.app_name) == mDownloadId) {
            val realTimeDownloadEntity = DownloadMessageHandler.findEntity(mDownloadId)

            mProgressText = "${progress.roundTo(1)}%"
            mProgressView.progress = (progress).toInt()

            if (realTimeDownloadEntity?.status == DownloadStatus.DOWNLOADING) {
                mProgressView.text = mProgressText
            }
        }
    }

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

    override fun onStatusChanged(status: DownloadStatus) {
        if (mProgressView.getTag(R.string.app_name) == mDownloadId) {
            when (status) {
                DownloadStatus.PAUSED -> {
                    mProgressView.text = "继续"
                    mProgressView.setDebouncedClickListener {
                        SimpleDownloadManager.resume(mDownloadId)
                    }
                    updateProgressViewStyle(mProgressView)
                }
                DownloadStatus.QUEUED,
                DownloadStatus.WAITINGWIFI -> {
                    mProgressView.text = "等待"
                    mProgressView.setOnClickListener {
                    }
                    updateProgressViewStyle(mProgressView, isWaiting = true)
                }
                DownloadStatus.DOWNLOADING -> {
                    mProgressView.setOnClickListener {
                        SimpleDownloadManager.pause(mDownloadId)
                        onStatusChanged(status = DownloadStatus.PAUSED)
                    }
                    updateProgressViewStyle(mProgressView)
                }
                DownloadStatus.COMPLETED -> {
                    mProgressView.text = "安装"
                    mProgressView.progress = 100
                    mProgressView.setOnClickListener {
                        ToastUtils.toast("安装")
                    }
                    updateProgressViewStyle(mProgressView)
                }
                else -> {
                    mProgressView.text = "下载"
                    // do nothing
                }
            }
        }
    }

    override fun onSpeedChanged(speed: Float) {}

    private fun updateProgressViewStyle(
        progressView: SimpleProgressView,
        isEmpty: Boolean = false,
        isWaiting: Boolean = false
    ) {
        when {
            isEmpty -> {
                // do nothing
            }
            isWaiting -> {
                // do nothing
            }
            else -> {
               // do nothing
            }
        }
    }

}
