package com.gh.gamecenter.gamedetail.fuli.kaifu

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.fragment.app.activityViewModels
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.databinding.DialogServersCalendarMoreBinding
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

/**
 * 开服日历表-更多菜单
 */
class ServersCalendarMoreDialog : BottomSheetDialogFragment() {

    private lateinit var viewBinding: DialogServersCalendarMoreBinding

    private val viewModel by activityViewModels<ServersCalendarViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, com.gh.gamecenter.common.R.style.DialogWindowTransparent)
    }

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

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

        viewBinding.unsubscribe.setOnClickListener {
            viewModel.unsubscribeServer()
            NewLogUtils.logLaunchServerSubscribeCancel(
                gameId = viewModel.game.id,
                gameName = viewModel.game.name ?: ""
            )
            SensorsBridge.trackLaunchServerSubscribeCancelClick(
                gameId = viewModel.game.id,
                gameName = viewModel.game.name ?: "",
                gameType = viewModel.game.categoryChinese
            )
            dismissAllowingStateLoss()
        }

        viewBinding.cancel.setOnClickListener {
            dismissAllowingStateLoss()
        }
    }

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