package com.gh.common.dialog

import android.app.Dialog
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.common.entity.ErrorEntity
import com.gh.gamecenter.core.utils.CurrentActivityHolder
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.DialogWechatBindingFailedBinding
import com.gh.gamecenter.login.user.UserManager
import com.lightgame.utils.Utils

class WechatBindingFailedDialogFragment : BaseDialogFragment() {

    private lateinit var binding: DialogWechatBindingFailedBinding

    private var currentUserId: String? = null
    private lateinit var userConflict: ErrorEntity.Data.UserConflict

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        userConflict = arguments?.getParcelable(KEY_USER_CONFLICT) ?: ErrorEntity.Data.UserConflict()
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return super.onCreateDialog(savedInstanceState).apply {
            setCanceledOnTouchOutside(true)
        }
    }

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        UserManager.getInstance().userInfoEntity?.let {
            currentUserId = it.getShortUserId()
            binding.tvCurrentName.text = it.name ?: ""
            binding.ivCurrentAvatar.displayAvatar(it.icon)
            binding.tvUserId.text = getString(R.string.user_id, currentUserId)
        }

        binding.tvConflictName.text = userConflict.name
        binding.ivConflictAvatar.displayAvatar(userConflict.icon)
        binding.tvConflictUserId.text = getString(R.string.user_id, userConflict.seq)


        binding.tvConflictProblem.setOnClickListener {
            dismiss()
            WechatBindingConflictDialogFragment.show(requireContext())
        }

        binding.tvIKnow.setOnClickListener {
            dismiss()
        }

        binding.tvUserId.setOnLongClickListener {
            currentUserId?.let {
                copyTextToClipboard(it)
                true
            } ?: false
        }

        binding.tvConflictUserId.setOnLongClickListener {
            copyTextToClipboard(userConflict.id)
            true
        }
    }

    private fun copyTextToClipboard(text: String) {
        val clipboardManager = context?.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
        if (clipboardManager != null) {
            val clip = ClipData.newPlainText(COPY_ID_TEXT, text)
            clipboardManager.setPrimaryClip(clip)
            Utils.toast(requireContext(), R.string.copy_user_id_successfully)
        }

    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.let {
            it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            val params = it.attributes
            params.width = DisplayUtils.dip2px(300F)
            it.attributes = params
        }
    }

    companion object {

        private const val COPY_ID_TEXT = "user_id"
        private const val KEY_USER_CONFLICT = "key_user_conflict"

        fun show(context: Context, userConflict: ErrorEntity.Data.UserConflict?) {
            if (context is AppCompatActivity) {
                context.supportFragmentManager
            } else {
                (CurrentActivityHolder.getCurrentActivity() as? AppCompatActivity)?.supportFragmentManager
            }?.let {
                val fragment = WechatBindingFailedDialogFragment().apply {
                    arguments = Bundle().apply {
                        putParcelable(KEY_USER_CONFLICT, userConflict)
                    }
                }
                fragment.show(it, WechatBindingFailedDialogFragment::class.java.name)
            }
        }
    }
}