package com.gh.gamecenter.forum.search

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.gh.common.util.NewFlatLogUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.DisplayType
import com.gh.gamecenter.R
import com.gh.gamecenter.SearchActivity
import com.gh.gamecenter.SearchType
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.showKeyBoard
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.db.ISearchHistoryDao
import com.gh.gamecenter.forum.detail.ForumDetailFragment
import com.gh.gamecenter.search.SearchDefaultFragment
import com.lightgame.utils.Util_System_Keyboard

/**
 * 论坛搜索
 */
class ForumOrUserSearchActivity : SearchActivity() {

    private var mBbsId = ""
    private var mLocation = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBbsId = intent.getStringExtra(EntranceConsts.KEY_BBS_ID) ?: ""
        mLocation = intent.getStringExtra(EntranceConsts.KEY_LOCATION) ?: ""
        mSourceEntrance = intent.getStringExtra(EntranceConsts.KEY_SOURCE_ENTRANCE) ?: ""
        searchEt.hint = if (mEntrance != ForumDetailFragment.ENTRANCE) {
            "搜索论坛内容、用户"
        } else {
            "搜索此论坛中的内容"
        }
        searchEt.showKeyBoard()
        searchBtn.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(this))
        searchBtn.setOnClickListener {
            val key = searchEt.text.toString().trim { it <= ' ' }
            NewLogUtils.logForumSearchClick(key, if (mEntrance != ForumDetailFragment.ENTRANCE) "社区搜索" else "论坛详情页搜索")
            Util_System_Keyboard.hideSoftKeyboard(this)
            search(SearchType.MANUAL, null)
        }
    }

    override fun initSearchBar() {
        super.initSearchBar()
        backBtn.setOnClickListener {
            val key = searchEt.text.toString().trim { it <= ' ' }
            if (mEntrance != ForumDetailFragment.ENTRANCE) {
                NewFlatLogUtils.logClickSearchReturn(key)
            } else {
                NewFlatLogUtils.logClickBbsSearchReturn(key, mBbsId)
            }
            onBackPressed()
        }
    }

    override fun provideDao(): ISearchHistoryDao = ForumSearchDao()

    override fun handleAutoSearch(key: String?) {
        mSearchKey = key
        updateDisplayType(DisplayType.FORUM_OR_USER)
    }

    override fun handleHistorySearch(key: String?) {
        mSearchKey = key
        searchEt.setText(key)
        searchEt.setSelection(searchEt.text.length)
        updateDisplayType(DisplayType.FORUM_OR_USER)
    }

    private fun handleOtherSearch(type: SearchType) {
        val newSearchKey = searchEt.text.toString().trim { it <= ' ' }
        if (newSearchKey != mSearchKey || mDisplayType != DisplayType.GAME_DETAIL) {
            mSearchKey = newSearchKey
            if (!TextUtils.isEmpty(mSearchKey)) {
                mDao.add(mSearchKey!!)
                updateDisplayType(DisplayType.FORUM_OR_USER)
                val searchType = if (type == SearchType.MANUAL) {
                    TRACK_SEARCH_TYPE_INPUT
                } else {
                    TRACK_SEARCH_TYPE_DEFAULT
                }
            } else {
                toast("请先输入搜索内容再搜索~")
            }
        }
    }

    override fun search(type: SearchType, key: String?) {
        mSearchType = type
        mIsAutoSearchDisabled = true
        when (type) {
            SearchType.AUTO -> handleAutoSearch(key)
            SearchType.HISTORY -> handleHistorySearch(key)
            else -> handleOtherSearch(type)
        }

        mIsAutoSearchDisabled = false
    }

    override fun updateDisplayType(type: DisplayType) {
        val transaction = when (type) {
            DisplayType.DEFAULT -> showFragment(
                    SearchDefaultFragment::class.java.name,
                    onFragmentCreate = { ForumOrUserSearchDefaultFragment() }
                ) { fragment ->
                    fragment.arguments = intent.extras
                }.apply {
                    val key = searchEt.text.toString().trim { it <= ' ' }
                    if (key.isNotEmpty()) {
                        val stayTime = (System.currentTimeMillis() - startPageTime) / 1000
                        if (mEntrance != ForumDetailFragment.ENTRANCE) {
                            NewFlatLogUtils.logViewSearchList(stayTime, key)
                        } else {
                            NewFlatLogUtils.logViewBbsSearchList(stayTime, key, mBbsId)
                        }
                    }
                }

            else -> if (mEntrance != ForumDetailFragment.ENTRANCE) {
                    showFragment(
                        ForumOrUserSearchFragment::class.java.name,
                        { ForumOrUserSearchFragment() }
                    ) { fragment ->
                        fragment.setSearchKeyAndType(mSearchKey ?: "", mSearchType.value)
                    }
                } else {
                    showFragment(
                        ForumOrUserSearchFragment::class.java.name,
                        { ForumContentSearchListFragment() }
                    ) { fragment ->
                        fragment.setSearchKeyAndType(mSearchKey ?: "", mSearchType.value)
                        fragment.arguments = intent.extras
                    }
                }.apply {
                startPageTime = System.currentTimeMillis()
            }
        }
        mDisplayType = type
        transaction.commitAllowingStateLoss()
    }

    override fun updateStaticViewBackground(view: View?) {
        updateStaticView(view, listOf(R.id.tab_title))
    }

    companion object {

        const val LOCATION_COMMUNITY = "社区"

        fun getIntent(
            context: Context,
            bbsId: String,
            mEntrance: String,
            sourceEntrance: String,
            location: String,
            bottomTab: String = "",
            multiTabId: String = "",
            multiTabName: String = "",
            customPageId: String = "",
            customPageName: String = "",
            searchBoxPattern: String = ""
        ): Intent {
            val intent = Intent(context, ForumOrUserSearchActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_BBS_ID, bbsId)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, mEntrance)
            intent.putExtra(EntranceConsts.KEY_SOURCE_ENTRANCE, sourceEntrance)
            intent.putExtra(EntranceConsts.KEY_LOCATION, location)
            intent.putExtra(EntranceConsts.KEY_BOTTOM_TAB_NAME, bottomTab)
            intent.putExtra(EntranceConsts.KEY_MULTI_TAB_NAV_ID, multiTabId)
            intent.putExtra(EntranceConsts.KEY_MULTI_TAB_NAV_NAME, multiTabName)
            intent.putExtra(EntranceConsts.KEY_CUSTOM_PAGE_ID, customPageId)
            intent.putExtra(EntranceConsts.KEY_CUSTOM_PAGE_NAME, customPageName)
            intent.putExtra(EntranceConsts.KEY_SEARCH_BOX_PATTERN, searchBoxPattern)
            return intent
        }
    }
}