package com.gh.gamecenter.mypost

import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.BaseFragment_TabLayout
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.personalhome.home.UserHistoryViewModel
import com.gh.gamecenter.personalhome.home.game.UserCommentHistoryFragment
import com.gh.gamecenter.qa.draft.CommunityDraftWrapperActivity
import com.gh.gamecenter.qa.myqa.MyAnswerFragment

class MyPostWrapperFragment : BaseFragment_TabLayout() {

    private var mDraftMenu: MenuItem? = null
    private var mLastPosition = 0

    override fun getLayoutId() = R.layout.fragment_no_padding_tablayout_viewpager

    override fun initTabTitleList(tabTitleList: MutableList<String>) {
        tabTitleList.add("评价")
        tabTitleList.add(getString(R.string.collection_article))
        tabTitleList.add(getString(R.string.ask_search_questions))
        tabTitleList.add(getString(R.string.video))
        tabTitleList.add(getString(R.string.answer))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mDividerLineView?.visibility = View.VISIBLE

        initMenu(R.menu.menu_my_post)
        mDraftMenu = getItemMenu(R.id.menu_draft)
        mDraftMenu?.actionView?.findViewById<TextView>(R.id.draftTv)?.visibility = View.GONE
    }

    override fun onResume() {
        super.onResume()
        setNavigationTitle("我的发布")
    }

    override fun onMenuItemClick(menuItem: MenuItem?) {
        super.onMenuItemClick(menuItem)
        menuItem?.run {
            if (itemId == R.id.menu_draft && mLastPosition != 0) {
                val tabName = when (mLastPosition) {
                    1 -> getString(R.string.collection_article)
                    2 -> getString(R.string.ask_search_questions)
                    3 -> getString(R.string.video)
                    4 -> getString(R.string.answer)
                    else -> ""
                }
                NewFlatLogUtils.logHaloSelfPublishContent(tabName, "草稿箱")
                startActivity(CommunityDraftWrapperActivity.getIntent(requireContext()))
            }
        }
    }

    override fun initFragmentList(fragments: MutableList<Fragment>) {
        fragments.add(UserCommentHistoryFragment.getInstance("我的发布", UserManager.getInstance().userId))
        fragments.add(MyPostFragment.getInstance(UserHistoryViewModel.TYPE.COMMUNITY_ARTICLE))
        fragments.add(MyPostFragment.getInstance(UserHistoryViewModel.TYPE.QUESTION))
        fragments.add(MyPostFragment.getInstance(UserHistoryViewModel.TYPE.VIDEO))
        fragments.add(MyAnswerFragment.getInstance("我的发布"))
    }

    override fun onPageSelected(position: Int) {
        super.onPageSelected(position)
        NewFlatLogUtils.logHaloSelfPublishTab(
            when (position) {
                0 -> "评价"
                1 -> getString(R.string.collection_article)
                2 -> getString(R.string.ask_search_questions)
                3 -> getString(R.string.video)
                4 -> getString(R.string.answer)
                else -> ""
            }
        )
        mLastPosition = position
        mDraftMenu?.actionView?.findViewById<TextView>(R.id.draftTv)?.goneIf(position == 0)
    }
}