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.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.base.BaseRichEditorActivity
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.observeNonNull
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.view.VerticalItemDecoration
import com.gh.gamecenter.databinding.DialogChooseSectionBinding
import com.gh.gamecenter.entity.ForumDetailEntity
import com.halo.assistant.HaloApp

class ChooseSectionDialogFragment : BaseDialogFragment() {
    private var mLastSelectSectionId = ""
    private lateinit var binding: DialogChooseSectionBinding
    private var mViewModel: ChooseSectionViewModel? = null
    private var mAdapter: ChooseSectionAdapter? = null
    private var mItemDecoration: RecyclerView.ItemDecoration? = null

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val bbsId = requireArguments().getString(EntranceConsts.KEY_BBS_ID) ?: ""
        mLastSelectSectionId = requireArguments().getString(TAG_SECTION_ID) ?: ""

        mViewModel = viewModelProvider(
            ChooseSectionViewModel.Factory(
                bbsId,
                requireArguments().getBoolean(IS_MODERATOR)
            )
        )
        mAdapter = ChooseSectionAdapter(requireContext(), ForumDetailEntity.Section(id = mLastSelectSectionId)) {
            val intent = Intent().apply {
                putExtra(EntranceConsts.KEY_DATA, mAdapter?.selectedSection)
            }
            val tag = requireArguments().getString(EntranceConsts.KEY_PARENT_TAG)
            if (tag == "editorActivity") {
                (requireActivity() as BaseRichEditorActivity<*>).onActivityDialogResult(
                    REQUEST_CODE,
                    Activity.RESULT_OK,
                    intent
                )
            } else {
                requireActivity().supportFragmentManager.findFragmentByTag(tag)
                    ?.onActivityResult(REQUEST_CODE, Activity.RESULT_OK, intent)
            }
            dismissAllowingStateLoss()
        }
        binding.sectionRv.run {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = mAdapter
            (itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
            mItemDecoration = VerticalItemDecoration(requireContext(), 0.5F, false, com.gh.gamecenter.common.R.color.ui_divider)
            addItemDecoration(mItemDecoration!!)
        }
        mViewModel?.sectionListLiveData?.observeNonNull(this) {
            mAdapter?.submitList(it)
        }
        binding.closeTv.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().application.resources.displayMetrics.widthPixels
        val height = dialog?.window?.attributes?.height ?: ViewGroup.LayoutParams.WRAP_CONTENT
        dialog?.window?.setLayout(width, height)
    }

    companion object {
        const val TAG_SECTION_ID = "tagSectionId"
        const val IS_MODERATOR = "is_moderator"
        const val REQUEST_CODE = 1106

        fun show(
            activity: AppCompatActivity,
            bbsId: String,
            tagSectionId: String? = "",
            isModerator: Boolean = false,
            parentTag: String
        ) {
            ChooseSectionDialogFragment().apply {
                arguments = bundleOf(
                    TAG_SECTION_ID to tagSectionId,
                    EntranceConsts.KEY_BBS_ID to bbsId,
                    IS_MODERATOR to isModerator,
                    EntranceConsts.KEY_PARENT_TAG to parentTag
                )
            }.show(activity.supportFragmentManager, ChooseSectionDialogFragment::class.java.name)
        }
    }
}