package com.gh.gamecenter.message.view.message

import android.app.Dialog
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.view.isVisible
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.common.utils.viewModelProviderFromParent
import com.gh.gamecenter.message.HaloApp
import com.gh.gamecenter.message.databinding.DialogSortedMessageActionBinding
import com.gh.gamecenter.message.entity.SortedMessageEntity
import com.gh.gamecenter.message.utils.NewFlatLogUtils

class SortedMessageActionDialog : BaseDialogFragment() {
    private lateinit var mBinding: DialogSortedMessageActionBinding
    private lateinit var mViewModel: SortedMessageListViewModel
    private var mMessageEntity: SortedMessageEntity? = null
    private var mPosition = 0
    private var mEntrance = ""

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mMessageEntity = requireArguments().getParcelable(KEY_MESSAGE)
        mPosition = requireArguments().getInt(EntranceConsts.KEY_POSITION)
        mEntrance = requireArguments().getString(EntranceConsts.KEY_ENTRANCE) ?: ""
        mViewModel = viewModelProviderFromParent()

        val gameId =
            if (mMessageEntity?.type == SortedMessageListAdapter.TYPE_GAME) mMessageEntity?.game?.id ?: "" else ""
        val gameName =
            if (mMessageEntity?.type == SortedMessageListAdapter.TYPE_GAME) mMessageEntity?.game?.name ?: "" else ""
        mBinding.run {
            markReadTv.isVisible = mMessageEntity?.unreadCount != 0
            markReadDivider.isVisible = mMessageEntity?.unreadCount != 0
            markReadTv.setOnClickListener {
                mMessageEntity?.let { entity ->
                    NewFlatLogUtils.logMessageInformClick(
                        mEntrance,
                        entity.typeChinese,
                        gameId,
                        gameName,
                        mPosition + 1,
                        entity.setting.top,
                        entity.unreadCount,
                        "标记已读"
                    )
                    NewFlatLogUtils.logMessageInformClean(1, entity.unreadCount)
                    mViewModel.markSortedMessageRead(mPosition, entity.id)
                    dismissAllowingStateLoss()
                }
            }

            setTopTv.text = if (mMessageEntity?.setting?.top == true) "取消置顶" else "置顶会话"
            setTopTv.setOnClickListener {
                mMessageEntity?.let { entity ->
                    NewFlatLogUtils.logMessageInformClick(
                        mEntrance,
                        entity.typeChinese,
                        gameId,
                        gameName,
                        mPosition + 1,
                        entity.setting.top,
                        entity.unreadCount,
                        setTopTv.text.toString()
                    )
                    mViewModel.setSortedMessageTop(entity.id, !entity.setting.top)
                    dismissAllowingStateLoss()
                }
            }

            doNotDisturbTv.text = if (mMessageEntity?.setting?.doNotDisturb == true) "取消免打扰" else "设置免打扰"
            doNotDisturbTv.setOnClickListener {
                mMessageEntity?.let { entity ->
                    NewFlatLogUtils.logMessageInformClick(
                        mEntrance,
                        entity.typeChinese,
                        gameId,
                        gameName,
                        mPosition + 1,
                        entity.setting.top,
                        entity.unreadCount,
                        doNotDisturbTv.text.toString()
                    )
                    mViewModel.setSortedMessageDoNotDisturb(mPosition, entity.id, !entity.setting.doNotDisturb)
                    dismissAllowingStateLoss()
                }
            }

            deleteTv.setOnClickListener {
                mMessageEntity?.let { entity ->
                    NewFlatLogUtils.logMessageInformClick(
                        mEntrance,
                        entity.typeChinese,
                        gameId,
                        gameName,
                        mPosition + 1,
                        entity.setting.top,
                        entity.unreadCount,
                        "删除会话"
                    )
                }
                DialogHelper.showDeleteDialog(
                    requireContext(),
                    title = "删除会话",
                    msg = "删除后将清除会话的消息记录",
                    confirmText = "确定",
                    cancelText = "取消",
                    confirmClickCallback = {
                        // 删除会话
                        mMessageEntity?.let {
                            mViewModel.deleteSortedMessage(mPosition, it.id)
                        }
                    },
                    cancelClickCallback = null,
                    isReverse = false
                )
                dismissAllowingStateLoss()
            }

            cancelBtn.setOnClickListener {
                dismissAllowingStateLoss()
            }
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val createDialog = super.onCreateDialog(savedInstanceState)
        createDialog.setCanceledOnTouchOutside(true)
        val window = createDialog.window
        window?.setGravity(Gravity.BOTTOM)
        window?.setWindowAnimations(com.gh.gamecenter.common.R.style.community_publication_animation)
        return createDialog
    }

    override fun onStart() {
        super.onStart()
        val width = HaloApp.getInstance().resources.displayMetrics.widthPixels
        val height = dialog?.window?.attributes?.height ?: ViewGroup.LayoutParams.WRAP_CONTENT
        dialog?.window?.setLayout(width, height)
    }

    companion object {
        private const val KEY_MESSAGE = "message"

        fun show(
            activity: AppCompatActivity,
            position: Int,
            messageEntity: SortedMessageEntity,
            entrance: String,
            parentTag: String
        ) {
            SortedMessageActionDialog().apply {
                arguments = bundleOf(
                    EntranceConsts.KEY_PARENT_TAG to parentTag,
                    EntranceConsts.KEY_POSITION to position,
                    EntranceConsts.KEY_ENTRANCE to entrance,
                    KEY_MESSAGE to messageEntity
                )
            }.show(activity.supportFragmentManager, SortedMessageActionDialog::class.java.name)
        }
    }
}