package com.gh.gamecenter.gamecollection.publish

import android.app.Activity
import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.databinding.DialogChooseActivityBinding
import com.gh.gamecenter.entity.ActivityLabelEntity
import com.halo.assistant.HaloApp

class ChooseGameCollectionActivityDialog : BaseDialogFragment() {
    private var mLastSelectActivityId = ""
    private lateinit var mBinding: DialogChooseActivityBinding

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mLastSelectActivityId = requireArguments().getString(TAG_ACTIVITY_ID) ?: ""
        val beginTransaction = childFragmentManager.beginTransaction()
        val fragment =
            childFragmentManager.findFragmentByTag(GameCollectionActivityLabelFragment::class.java.name) as? GameCollectionActivityLabelFragment
                ?: GameCollectionActivityLabelFragment()
        fragment.arguments = arguments
        beginTransaction.replace(mBinding.container.id, fragment, GameCollectionActivityLabelFragment::class.java.name)
        beginTransaction.commitAllowingStateLoss()
        mBinding.titleTv.text = "选择活动标签"
        mBinding.confirmTv.setOnClickListener {
            val activityLabelEntity = fragment.getSelectedActivity()
            val intent = Intent().apply {
                putExtra(EntranceConsts.KEY_DATA, activityLabelEntity)
            }
            (requireActivity() as GameCollectionEditActivity).onActivityDialogResult(
                GameCollectionEditActivity.REQUEST_CHOOSE_ACTIVITY,
                Activity.RESULT_OK,
                intent
            )

            dismissAllowingStateLoss()
        }
        mBinding.confirmTv.isEnabled = false
        mBinding.confirmTv.alpha = 0.6f
    }

    fun chooseActivityLabelCallback(label: ActivityLabelEntity?) {
        val isEnable = checkConfirmButtonIsEnable(label)
        mBinding.confirmTv.isEnabled = isEnable
        mBinding.confirmTv.alpha = if (isEnable) 1f else 0.6f
    }

    private fun checkConfirmButtonIsEnable(label: ActivityLabelEntity?): Boolean {
        return if (mLastSelectActivityId.isEmpty()) {
            label != null
        } else {
            if (label != null) {
                label.id != mLastSelectActivityId
            } else {
                true
            }
        }
    }

    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 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 onDarkModeChanged() {
        super.onDarkModeChanged()
        if (::mBinding.isInitialized) {
            mBinding.dialogContainer.background =
                R.drawable.game_detail_more_dialog_background.toDrawable(requireContext())
            mBinding.titleTv.setTextColor(R.color.text_title.toColor(requireContext()))
            mBinding.confirmTv.setTextColor(R.color.theme_font.toColor(requireContext()))
        }
    }

    companion object {
        const val TAG_ACTIVITY_ID = "tagActivityId"

        fun show(activity: AppCompatActivity, tagActivityId: String? = "", parentTag: String) {
            ChooseGameCollectionActivityDialog().apply {
                arguments = bundleOf(
                    TAG_ACTIVITY_ID to tagActivityId,
                    EntranceConsts.KEY_PARENT_TAG to parentTag
                )
            }.show(activity.supportFragmentManager, ChooseGameCollectionActivityDialog::class.java.name)
        }
    }
}