package com.gh.gamecenter.gamedetail.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.common.DefaultUrlHandler
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.R
import com.gh.gamecenter.WebActivity
import com.gh.gamecenter.common.callback.ConfirmListener
import com.gh.gamecenter.databinding.GamePermissionDialogBinding
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.gamedetail.entity.GameInfo

class GamePermissionDialogFragment : BaseDialogFragment() {

    lateinit var binding: GamePermissionDialogBinding
    private var mGame: GameEntity? = null
    private var mGameInfo: GameInfo? = null

    var callback: ConfirmListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requireArguments().run {
            mGame = getParcelable(KEY_GAME)
            mGameInfo = getParcelable(KEY_GAME_INFO)
        }
    }

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mGame?.let {
            binding.gameIconView.displayGameIcon(it)
            binding.gameNameTv.text = it.name
            binding.gameVersionTv.text = "版本号：${mGameInfo?.version}"
        }
        mGameInfo?.permissions?.let {
            binding.permissionsRv.layoutManager = LinearLayoutManager(requireContext())
            binding.permissionsRv.adapter = GamePermissionAdapter(requireContext(), it)
        }
        binding.developerTv.text = "开发者：${mGameInfo?.manufacturer}"
        binding.confirmTv.setOnClickListener {
            dismissAllowingStateLoss()
            callback?.onConfirm()
        }
        binding.privacyContainer.goneIf(mGameInfo?.privacyPolicyUrl.isNullOrEmpty())
        binding.privacyContainer.setOnClickListener {
            mGameInfo?.privacyPolicyUrl?.let {
                if (!DefaultUrlHandler.transformNormalScheme(binding.root.context, it, "隐私政策")) {
                    val intent = WebActivity.getIntent(binding.root.context, it, "隐私政策", false, false)
                    binding.root.context.startActivity(intent)
                }
            }
        }
    }

    override fun onStart() {
        super.onStart()
        val width = requireContext().resources.displayMetrics.widthPixels - 40F.dip2px()
        val height = ViewGroup.LayoutParams.WRAP_CONTENT
        dialog?.window?.setLayout(width, height)
        dialog?.setCanceledOnTouchOutside(true)
    }

    companion object {
        const val KEY_GAME = "game"
        const val KEY_GAME_INFO = "game_info"

        @JvmStatic
        fun show(
            activity: AppCompatActivity,
            game: GameEntity?,
            gameInfo: GameInfo?,
            callBack: ConfirmListener? = null
        ) {
            if (callBack == null || game?.permissionDialogStatus == "on") {
                GamePermissionDialogFragment().apply {
                    arguments = Bundle().apply {
                        putParcelable(KEY_GAME, game)
                        putParcelable(KEY_GAME_INFO, gameInfo)
                    }
                }.show(activity.supportFragmentManager, GamePermissionDialogFragment::class.java.name)
            } else {
                callBack.onConfirm()
            }
        }
    }
}