package com.gh.gamecenter.gamedetail.dialog

import android.app.Dialog
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.common.view.divider.HorizontalDividerItemDecoration
import com.gh.download.dialog.DownloadDialog
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.databinding.DialogGameTagsBinding
import com.gh.gamecenter.entity.TagStyleEntity
import com.gh.gamecenter.tag.TagsActivity
import com.halo.assistant.HaloApp

class GameTagsDialog : BaseDialogFragment() {

    private lateinit var mTagStyles: ArrayList<TagStyleEntity>
    private lateinit var mGameId: String
    private lateinit var mGameName: String
    private val mBinding by lazy { DialogGameTagsBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mGameId = requireArguments().getString(EntranceConsts.KEY_GAME_ID) ?: ""
        mGameName = requireArguments().getString(EntranceConsts.KEY_GAME_NAME) ?: ""
        mTagStyles = requireArguments().getParcelableArrayList(EntranceConsts.KEY_TAGTYPE)
            ?: ArrayList()
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val createDialog = super.onCreateDialog(savedInstanceState)
        createDialog.setCanceledOnTouchOutside(true)

        val window = createDialog.window
        window?.setGravity(Gravity.BOTTOM)
        window?.setWindowAnimations(R.style.community_publication_animation)
        return createDialog
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return mBinding.root
    }

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.gameTagsRv.apply {
            layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
            val itemDecoration = HorizontalDividerItemDecoration.Builder(requireContext())
                .margin(DisplayUtils.dip2px(20f))
                .size(DisplayUtils.dip2px(1f))
                .color(ContextCompat.getColor(requireContext(), R.color.background)).build()
            addItemDecoration(itemDecoration)
            val gameTagsAdapter = GameTagsAdapter(requireContext(), mTagStyles)
            adapter = gameTagsAdapter
            gameTagsAdapter.onClickListener = {
                requireContext().startActivity(
                    TagsActivity.getIntent(
                        requireContext(),
                        it.name, it.name, "", "游戏介绍"
                    )
                )
                NewLogUtils.logGameDetailTagClick(mGameId, mGameName, it.id, it.name)
                MtaHelper.onEvent("游戏标签弹窗", "进入标签", "${mGameName}+${it.name}")
            }
        }
    }

    companion object {
        fun showGameTagsDialog(
            context: FragmentActivity,
            tagStyles: ArrayList<TagStyleEntity>,
            gameId: String,
            gameName: String
        ) {
            val dialog = GameTagsDialog().apply {
                arguments = bundleOf(
                    EntranceConsts.KEY_GAME_ID to gameId,
                    EntranceConsts.KEY_GAME_NAME to gameName,
                    EntranceConsts.KEY_TAGTYPE to tagStyles
                )
            }
            dialog.show(context.supportFragmentManager, DownloadDialog::class.java.name)
        }
    }
}