package com.gh.gamecenter.forum.search

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.gh.common.util.*
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.search.SearchDefaultFragment
import com.lightgame.utils.Util_System_Keyboard

class ForumOrUserSearchActivity : SearchActivity() {

    private var mBbsId = ""
    private val mSearchHistoryDao by lazy { ForumSearchDao() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBbsId = intent.getStringExtra(EntranceConsts.KEY_BBS_ID) ?: ""
        searchEt.hint = if (mEntrance == "论坛首页") {
            "搜索论坛内容、用户"
        } else {
            "搜索此论坛中的内容"
        }
        searchEt.showKeyBoard()
        searchBtn.setTextColor(R.color.theme_font.toColor(this))
        searchBtn.setOnClickListener {
            val key = searchEt.text.toString().trim { it <= ' ' }
            NewLogUtils.logForumSearchClick(key, if (mEntrance == "论坛首页") "社区搜索" 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 == "论坛首页") {
                NewFlatLogUtils.logClickSearchReturn(key)
            } else {
                NewFlatLogUtils.logClickBbsSearchReturn(key, mBbsId)
            }
            onBackPressed()
        }
    }

    override fun search(type: SearchType, key: String?) {
        mSearchType = type
        mIsAutoSearchDisabled = true

        when (type) {
            SearchType.AUTO -> {
                mSearchKey = key
                updateDisplayType(DisplayType.FORUM_OR_USER)
            }

            SearchType.HISTORY -> {
                mSearchKey = key
                searchEt.setText(key)
                searchEt.setSelection(searchEt.text.length)
                updateDisplayType(DisplayType.FORUM_OR_USER)
            }
            else -> {
                val newSearchKey = searchEt.text.toString().trim { it <= ' ' }
                if (newSearchKey != mSearchKey || mDisplayType != DisplayType.GAME_DETAIL) {
                    mSearchKey = newSearchKey
                    if (!TextUtils.isEmpty(mSearchKey)) {
                        mSearchHistoryDao.add(mSearchKey!!)
                        updateDisplayType(DisplayType.FORUM_OR_USER)
                    } else {
                        toast("请先输入搜索内容再搜索~")
                    }
                }
            }
        }

        mIsAutoSearchDisabled = false
    }

    override fun updateDisplayType(type: DisplayType) {
        val transaction = supportFragmentManager.beginTransaction()
        when (type) {
            DisplayType.DEFAULT -> {
                val fragment = supportFragmentManager.findFragmentByTag(SearchDefaultFragment::class.java.name)
                    ?: ForumOrUserSearchDefaultFragment()
                fragment.arguments = intent.extras
                transaction.replace(R.id.search_result, fragment, SearchDefaultFragment::class.java.name)
                val key = searchEt.text.toString().trim { it <= ' ' }
                if (key.isNotEmpty()) {
                    val stayTime = (System.currentTimeMillis() - startPageTime) / 1000
                    if (mEntrance == "论坛首页") {
                        NewFlatLogUtils.logViewSearchList(stayTime, key)
                    } else {
                        NewFlatLogUtils.logViewBbsSearchList(stayTime, key, mBbsId)
                    }
                }
            }
            else -> {
                if (mEntrance == "论坛首页") {
                    val fragment =
                        supportFragmentManager.findFragmentByTag(ForumOrUserSearchFragment::class.java.name) as? ForumOrUserSearchFragment
                            ?: ForumOrUserSearchFragment()
                    fragment.setSearchKey(mSearchKey ?: "")
                    transaction.replace(R.id.search_result, fragment, ForumOrUserSearchFragment::class.java.name)
                } else {
                    val fragment =
                        supportFragmentManager.findFragmentByTag(ForumContentSearchListFragment::class.java.name) as? ForumContentSearchListFragment
                            ?: ForumContentSearchListFragment()
                    fragment.setSearchKey(mSearchKey ?: "")
                    fragment.arguments = intent.extras
                    transaction.replace(R.id.search_result, fragment, ForumContentSearchListFragment::class.java.name)
                }
                startPageTime = System.currentTimeMillis()
            }
        }
        mDisplayType = type
        transaction.commitAllowingStateLoss()
    }

    companion object {
        fun getIntent(context: Context, bbsId: String, mEntrance: String): Intent {
            val intent = Intent(context, ForumOrUserSearchActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_BBS_ID, bbsId)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, mEntrance)
            return intent
        }
    }
}