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 androidx.core.widget.doOnTextChanged
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.databinding.DialogInputUrlBinding
import com.halo.assistant.HaloApp
import com.lightgame.utils.Util_System_Keyboard

class InputUrlDialogFragment : BaseDialogFragment() {

    private lateinit var mBinding: DialogInputUrlBinding

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val url = arguments?.getString(EntranceConsts.KEY_URL, "") ?: ""

        mBinding.reprintUrlEt.postDelayed({
            Util_System_Keyboard.showSoftKeyboard(requireActivity(), mBinding.reprintUrlEt)
        }, 100)
        mBinding.reprintUrlEt.doOnTextChanged { text, _, _, _ ->
            mBinding.confirmTv.alpha = if (text.isNullOrEmpty()) 0.6f else 1f
            mBinding.confirmTv.isEnabled = !text.isNullOrEmpty()
        }
        mBinding.reprintUrlEt.setText(url)
        mBinding.reprintUrlEt.setSelection(url.length)
        mBinding.confirmTv.setOnClickListener {
            val reprintUrl = mBinding.reprintUrlEt.text.toString()
            if (!reprintUrl.startsWith("http")) {
                ToastUtils.showToast("请输入正确的链接")
                return@setOnClickListener
            }
            val parentTag = requireArguments().getString(EntranceConsts.KEY_PARENT_TAG)
            val intent = Intent().apply {
                putExtra(EntranceConsts.KEY_DATA, reprintUrl)
            }
            activity?.supportFragmentManager?.findFragmentByTag(parentTag)
                ?.onActivityResult(REQUEST_CODE, Activity.RESULT_OK, intent)
            dismissAllowingStateLoss()
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val createDialog = super.onCreateDialog(savedInstanceState)
        createDialog.setCanceledOnTouchOutside(true)
        val window = createDialog.window
        window?.setGravity(Gravity.BOTTOM)
        return createDialog
    }

    override fun onStart() {
        super.onStart()
        val width = HaloApp.getInstance().application.resources.displayMetrics.widthPixels
        dialog?.window?.setLayout(width, 56f.dip2px())
    }

    companion object {
        const val REQUEST_CODE = 1104

        fun show(activity: AppCompatActivity, url: String, tag: String) {
            InputUrlDialogFragment().also {
                it.arguments = bundleOf(
                    EntranceConsts.KEY_URL to url,
                    EntranceConsts.KEY_PARENT_TAG to tag
                )
            }.show(activity.supportFragmentManager, InputUrlDialogFragment::class.java.name)
        }
    }
}