package com.gh.gamecenter.amway.search

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.DisplayType
import com.gh.gamecenter.R
import com.gh.gamecenter.SearchActivity
import com.gh.gamecenter.SearchType
import com.gh.gamecenter.search.SearchDefaultFragment

class AmwaySearchActivity : SearchActivity() {

    private lateinit var mViewModel: AmwaySearchViewModel
    private val mSearchHistoryDao by lazy { AmwaySearchDao() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mViewModel = viewModelProvider()
    }

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

        when (type) {
            SearchType.AUTO -> {
                mSearchKey = key
                updateDisplayType(DisplayType.GAME_DIGEST)
                mViewModel.getSearchResult(mSearchKey!!)
            }

            SearchType.HISTORY -> {
                mSearchKey = key
                searchEt.setText(key)
                searchEt.setSelection(searchEt.text.length)
                mViewModel.getSearchResult(mSearchKey!!)
                updateDisplayType(DisplayType.GAME_DETAIL)
            }

            else -> {
                val newSearchKey = searchEt.text.toString().trim { it <= ' ' }
                if (newSearchKey != mSearchKey || mDisplayType != DisplayType.GAME_DETAIL) {
                    mSearchKey = newSearchKey
                    if (!TextUtils.isEmpty(mSearchKey)) {
                        mViewModel.getSearchResult(mSearchKey!!)
                        mSearchHistoryDao.add(mSearchKey!!)
                        updateDisplayType(DisplayType.GAME_DETAIL)
                    } 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)
                    ?: AmwaySearchDefaultFragment()
                transaction.replace(R.id.search_result, fragment, SearchDefaultFragment::class.java.name)
            }
            else -> {
                val fragment = supportFragmentManager.findFragmentByTag(AmwaySearchListFragment::class.java.name)
                    ?: AmwaySearchListFragment()
                transaction.replace(R.id.search_result, fragment, AmwaySearchListFragment::class.java.name)
            }
        }
        mDisplayType = type
        transaction.commitAllowingStateLoss()
    }

    companion object {
        fun getIntent(context: Context): Intent {
            val intent = Intent(context, AmwaySearchActivity::class.java)
            return intent
        }
    }

}