package com.gh.common.dialog

import android.app.Activity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.view.CustomLinkMovementMethod
import com.gh.gamecenter.core.utils.SpanBuilder
import com.gh.gamecenter.databinding.DialogApplyModeratorBinding


class ApplyModeratorDialogFragment : BaseDialogFragment() {
    private lateinit var binding: DialogApplyModeratorBinding
    private var mGroupNumber = ""
    private var mGroupKey = ""
    private var mParentTag = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requireArguments().run {
            mGroupNumber = getString(KEY_GROUP_NUMBER) ?: ""
            mGroupKey = getString(KEY_GROUP_KEY) ?: ""
            mParentTag = getString(EntranceConsts.KEY_PARENT_TAG) ?: ""
        }
    }

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val startText = "版主考核群："
        val text = "$startText$mGroupNumber\n感谢你对论坛建设的支持\n请加入版主考核群并联系群主进行版主资格考核"
        binding.desTv.text = SpanBuilder(text)
            .click(
                requireContext(),
                startText.length,
                startText.length + mGroupNumber.length,
                com.gh.gamecenter.common.R.color.text_theme,
                true
            ) {
                DirectUtils.directToQqGroup(
                    requireContext(),
                    mGroupKey
                )
            }
            .build()
        binding.desTv.movementMethod = CustomLinkMovementMethod.getInstance()
        binding.confirmTv.setOnClickListener {
            dismissAllowingStateLoss()
            activity?.supportFragmentManager?.findFragmentByTag(mParentTag)
                ?.onActivityResult(REQUEST_CODE, Activity.RESULT_OK, null)
        }
    }

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

    companion object {
        const val KEY_GROUP_NUMBER = "group_number"
        const val KEY_GROUP_KEY = "group_key"
        const val REQUEST_CODE = 1103

        @JvmStatic
        fun show(
            activity: AppCompatActivity,
            number: String,
            key: String,
            tag: String
        ) {
            ApplyModeratorDialogFragment().apply {
                arguments = Bundle().apply {
                    putString(KEY_GROUP_NUMBER, number)
                    putString(KEY_GROUP_KEY, key)
                    putString(EntranceConsts.KEY_PARENT_TAG, tag)
                }
            }.show(
                activity.supportFragmentManager,
                ApplyModeratorDialogFragment::class.java.name
            )
        }
    }
}