package com.gh.gamecenter.discovery.interestedgame

import android.app.Dialog
import android.content.DialogInterface
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 com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.observeNonNull
import com.gh.gamecenter.common.utils.setRootBackgroundDrawable
import com.gh.gamecenter.common.view.FixLinearLayoutManager
import com.gh.gamecenter.core.utils.EmptyCallback
import com.gh.gamecenter.databinding.DialogInterestedGameTypeBinding
import com.halo.assistant.HaloApp

class InterestedGameTypeDialogFragment(
    private val mViewModel: InterestedGameViewModel,
    private val dismissCallback: EmptyCallback
) : BaseDialogFragment() {

    private val mBinding by lazy { DialogInterestedGameTypeBinding.inflate(layoutInflater) }

    override fun getThemeRes() = R.style.DialogFragmentDimAmount

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewModel.initTempTagList()
        mViewModel.selectedCountLiveData.observeNonNull(this) {
            updateConfirmBtnText(it)
        }
        mBinding.run {
            updateConfirmBtnText(mViewModel.getSelectedCount())
            cancelTv.setOnClickListener {
                dismissAllowingStateLoss()
            }
            typeRv.run {
                layoutManager = FixLinearLayoutManager(requireContext())
                adapter = InterestedGameTypeAdapter(requireContext(), mViewModel) {
                    typeRv.adapter?.notifyDataSetChanged()
                }
            }
            clearBtn.setOnClickListener {
                if (mViewModel.getSelectedCount() > 0) {
                    mViewModel.resetTemp()
                    typeRv.adapter?.notifyDataSetChanged()
                }
            }
            confirmBtn.setOnClickListener {
                mViewModel.confirmTempList()
                dismissAllowingStateLoss()
            }
        }
    }

    private fun updateConfirmBtnText(count: Int) {
        val suffix = if (count > 0) "(${count})" else ""
        mBinding.confirmBtn.text = "确认$suffix"
    }

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

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

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mBinding.run {
            root.setRootBackgroundDrawable(R.drawable.background_shape_white_radius_12_top_only)
        }
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        dismissCallback.onCallback()
    }

    companion object {

        fun show(
            activity: AppCompatActivity,
            viewModel: InterestedGameViewModel,
            dismissCallback: EmptyCallback
        ) {
            InterestedGameTypeDialogFragment(viewModel, dismissCallback).show(
                activity.supportFragmentManager,
                InterestedGameTypeDialogFragment::class.java.name
            )
        }
    }
}