package com.gh.gamecenter.gamedetail.accelerator.dialog

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.HaloApp
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.core.utils.CurrentActivityHolder
import com.gh.gamecenter.databinding.DialogFragmentSpeedReplaceGameBinding
import com.lightgame.dialog.BaseDialogFragment

class AcceleratorReplaceDialogFragment : BaseDialogFragment() {

    private lateinit var binding: DialogFragmentSpeedReplaceGameBinding
    private var pkgName = ""
    private var gameId = ""
    private var gameName = ""
    private var sourceEntrance = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        pkgName = arguments?.getString(EntranceConsts.KEY_PACKAGENAME) ?: ""
        gameId = arguments?.getString(EntranceConsts.KEY_GAMEID) ?: ""
        gameName = arguments?.getString(EntranceConsts.KEY_GAMENAME) ?: ""
        sourceEntrance = arguments?.getString(EntranceConsts.KEY_SOURCE_ENTRANCE) ?: ""
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return DialogFragmentSpeedReplaceGameBinding.inflate(inflater, container, false)
            .also {
                binding = it
            }.root
    }

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

        SensorsBridge.trackNetworkAccelerationConflictDialogShow(pkgName, gameId, gameName, sourceEntrance)

        binding.tvContent.text = getString(R.string.speed_replace_game_tips, gameName)

        binding.tvCancel.setOnClickListener {
            SensorsBridge.trackNetworkAccelerationConflictDialogClick(
                pkgName,
                gameId,
                gameName,
                BUTTON_NAME_CANCEL,
                sourceEntrance
            )
            dismiss()
        }

        binding.tvSubmit.setOnClickListener {
            SensorsBridge.trackNetworkAccelerationConflictDialogClick(
                pkgName,
                gameId,
                gameName,
                BUTTON_NAME_CONTINUE,
                sourceEntrance
            )
            // 加速
            dismiss()
            callback?.invoke()
        }
    }

    override fun onStart() {
        super.onStart()
        val width = HaloApp.getInstance().resources.displayMetrics.widthPixels - 60F.dip2px()
        val height = dialog?.window?.attributes?.height ?: ViewGroup.LayoutParams.WRAP_CONTENT
        dialog?.window?.setLayout(width, height)
    }

    private var callback: (() -> Unit)? = null
    fun setOnSubmitListener(callback: () -> Unit) {
        this.callback = callback
    }

    companion object {

        private const val BUTTON_NAME_CANCEL = "暂不启动"
        private const val BUTTON_NAME_CONTINUE = "继续启动"

        fun show(
            context: Context,
            pkgName: String,
            gameId: String,
            gameName: String,
            sourceEntrance: String,
            callback: () -> Unit
        ) {
            if (context is AppCompatActivity) {
                context.supportFragmentManager
            } else {
                (CurrentActivityHolder.getCurrentActivity() as? AppCompatActivity)?.supportFragmentManager
            }?.let {
                val fragment = AcceleratorReplaceDialogFragment().apply {
                    setOnSubmitListener(callback)
                    arguments = Bundle().apply {
                        putString(EntranceConsts.KEY_PACKAGENAME, pkgName)
                        putString(EntranceConsts.KEY_GAMEID, gameId)
                        putString(EntranceConsts.KEY_GAMENAME, gameName)
                        putString(EntranceConsts.KEY_SOURCE_ENTRANCE, sourceEntrance)
                    }
                }
                fragment.show(it, AcceleratorDialogFragment::class.java.simpleName)
            }
        }
    }
}