package com.ccwangluo.accelerator.ui.dialog


import android.content.Context
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.ccwangluo.accelerator.R
import com.ccwangluo.accelerator.manager.GameDownLoadManger
import com.ccwangluo.accelerator.model.GameInfo
import com.ccwangluo.accelerator.utils.SysUtils
import com.ccwangluo.accelerator.utils.datareport.DataRePortUtils
import com.umeng.socialize.utils.DeviceConfigInternal.context
import com.xuexiang.xpage.base.XPageFragment
import com.xuexiang.xui.widget.dialog.materialdialog.CustomMaterialDialog
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog


class GameDownloadDialog(
    val gameInfo: GameInfo,
    val fragment: XPageFragment
) : CustomMaterialDialog(fragment.context) {

    override fun getDialogBuilder(context: Context?): MaterialDialog.Builder {
        return MaterialDialog.Builder(context!!)
            .customView(R.layout.dialog_download, false);
    }

    override fun initViews(context: Context?) {

    }

    init {
        val appImg = findViewById<ImageView>(R.id.app_img)
        fragment.context?.let {
            Glide.with(it)
                .load(gameInfo.icon)
                .into(appImg)
        }
        var version = gameInfo.version
        if (SysUtils.compareVersionTo(gameInfo.versionTools, gameInfo.version)) {
            gameInfo.versionTools?.let {
                version = it
            }
        }
        val appName = findViewById<TextView>(R.id.app_name)
        appName.setText(gameInfo.gameName)
        val appSize = findViewById<TextView>(R.id.app_size)
        appSize.setText("文件大小:${gameInfo.size}")
        val appVersion = findViewById<TextView>(R.id.app_version)
        appVersion.setText("版本号:${version}")
        val btnLeft = findViewById<TextView>(R.id.btn_left)
        btnLeft.setOnClickListener {
            DataRePortUtils.report("st_pop_dld_cancel", mapOf("gameid" to gameInfo.id.toString()))
            dismiss<GameDownloadDialog>()
        }

    }

    fun download(url : String) {
        val btnRight = findViewById<TextView>(R.id.btn_right)

        btnRight.setOnClickListener {
            DataRePortUtils.report("st_pop_dld_clk", mapOf("gameid" to gameInfo.id.toString()))
            //流量下载提示
            if (SysUtils.isMobileNet()) {
                if (context != null) {
                    CommonDialog(context)
                        .setContent(
                            "您正在使用计量网络下载," +
                                    "将产生${gameInfo.size}的流量，是否继续？"
                        )
                        .setBtnLeft("取消") {

                        }.setBtnRight("继续") {
                            GameDownLoadManger.download(fragment,gameInfo,url)
                        }.show<CommonDialog>()
                }
            } else {
                dialog.dismiss()
                GameDownLoadManger.download(fragment, gameInfo, url)
            }
        }
    }


}