package com.gh.gamecenter.qa.dialog

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.base.BaseRichEditorActivity
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.DialogChooseActivityBinding
import com.gh.gamecenter.entity.ActivityLabelEntity
import com.gh.gamecenter.video.label.VideoLabelFragment
import com.halo.assistant.HaloApp

class ChooseActivityDialogFragment : BaseDialogFragment() {
    private var mLastSelectActivityId = ""
    private lateinit var binding: DialogChooseActivityBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = DialogChooseActivityBinding.inflate(inflater, null, false)
        return binding.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(VideoLabelFragment::class.java.name) as? VideoLabelFragment
                ?: VideoLabelFragment()
        fragment.arguments = arguments
        beginTransaction.replace(binding.container.id, fragment, VideoLabelFragment::class.java.name)
        beginTransaction.commitAllowingStateLoss()
        binding.confirmTv.setOnClickListener {
            val activityLabelEntity = fragment.getSelectedActivity()
            val tag = requireArguments().getString(EntranceConsts.KEY_PARENT_TAG)
            val intent = Intent().apply {
                putExtra(EntranceConsts.KEY_DATA, activityLabelEntity)
            }
            if (tag == "editorActivity") {
                (activity as? BaseRichEditorActivity<*>)
                    ?.onActivityDialogResult(REQUEST_CODE, Activity.RESULT_OK, intent)
            } else {
                activity?.supportFragmentManager?.findFragmentByTag(tag)
                    ?.onActivityResult(REQUEST_CODE, Activity.RESULT_OK, intent)
            }
            dismissAllowingStateLoss()
        }
        binding.confirmTv.isEnabled = false
        binding.confirmTv.alpha = 0.6f
    }

    fun chooseActivityLabelCallback(label: ActivityLabelEntity?) {
        val isEnable = checkConfirmButtonIsEnable(label)
        binding.confirmTv.isEnabled = isEnable
        binding.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)
    }

    companion object {
        const val TAG_ACTIVITY_ID = "tagActivityId"
        const val REQUEST_CODE = 1102

        fun show(
            activity: AppCompatActivity,
            location: ActivityLabelLocation,
            bbsId: String,
            tagActivityId: String? = "",
            parentTag: String
        ) {
            ChooseActivityDialogFragment().apply {
                arguments = bundleOf(
                    TAG_ACTIVITY_ID to tagActivityId,
                    EntranceConsts.KEY_BBS_ID to bbsId,
                    EntranceConsts.KEY_LOCATION to location.value,
                    EntranceConsts.KEY_PARENT_TAG to parentTag
                )
            }.show(activity.supportFragmentManager, ChooseActivityDialogFragment::class.java.name)
        }
    }

    enum class ActivityLabelLocation(val value: String) {
        BBS_QUESTION("bbs_question"),
        BBS_VIDEO("bbs_video"),
        BBS_ARTICLE("bbs_article")
    }

}