package com.gh.gamecenter.game.upload

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.DialogUploadScheduleBinding

/**
 * 游戏上传进度dialog
 */
class UploadScheduleDialog : BaseDialogFragment() {

    lateinit var binding: DialogUploadScheduleBinding
    var isDisable = false
        set(value) {
            field = value
            binding.circleProgressBar.isDisable(value)
        }
    var onClickListener: ((pos: Int) -> Unit)? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DialogUploadScheduleBinding.bind(layoutInflater.inflate(R.layout.dialog_upload_schedule, null, false))
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.tvGiveUpUpload.setOnClickListener {
            onClickListener?.invoke(0)
            MtaHelper.onEvent("游戏上传", "游戏上传", "放弃上传")
        }
        binding.tvExit.setOnClickListener {
            onClickListener?.invoke(1)
        }
        binding.tvContactCustomerService.setOnClickListener {
            onClickListener?.invoke(2)
        }
    }

    /**
     * 更新进度
     */
    fun updateProgress(prgress: Int) {
        val text = "${prgress * 100 / 360}%"
        binding.circleProgressBar.update(prgress, text)
    }

    /**
     * 上传成功（要显示上传失败）
     */
    /*fun uploadSuccess(isSuccess: Boolean) {
        binding.isUploadSuccess = isSuccess
        binding.ivUpload.setImageDrawable(ContextCompat.getDrawable(requireContext(), if (isSuccess) R.drawable.ic_upload_failure else R.drawable.ic_uploading))
        binding.tvUpload.text = if (isSuccess) "游戏上传失败" else "游戏正在上传中"
        binding.circleProgressBar.isDisable(isSuccess)

        binding.executePendingBindings()
    }*/

    fun uploadFail() {
        MtaHelper.onEvent("游戏上传", "游戏上传", "上传失败")
        binding.tvGiveUpUpload.visibility = View.GONE
        binding.tvExit.visibility = View.VISIBLE
        binding.tvContactCustomerService.visibility = View.VISIBLE
        binding.ivUpload.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.ic_upload_failure))
        binding.tvUpload.text = "游戏上传失败"
        binding.circleProgressBar.isDisable(true)
    }


    override fun onBack(): Boolean {
        return true
    }
}