package com.yidianling.consultant.router

import android.app.Activity
import android.content.Intent
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.ydl.ydlcommon.utils.LogUtil
import com.yidianling.consultant.ExpertSearchActivity
import com.yidianling.consultant.data.ConsultantDataManager
import com.yidianling.router.consultant.IConsultantRouter
import com.yidianling.router.consultant.Keyworks

class ConsultantRouterImp : IConsultantRouter{
    val gson: Gson = Gson()

    override fun expertSearchIntent(activity: Activity, category: Int, showType: Int, isInitShowHot: Boolean): Intent {
        return ExpertSearchActivity.newIntent(activity, category, showType, isInitShowHot)
    }

    override fun getHotSearch(): MutableList<Keyworks> {
        val hotSearch = ConsultantDataManager.getRam().getHotSearch()
        if (hotSearch.size==0){
            return mutableListOf()
        }
        val list = try {
            val jsonStr = gson.toJson(hotSearch)
            gson.fromJson<MutableList<Keyworks>>(jsonStr, object : TypeToken<MutableList<Keyworks>>() {
            }.type)
        } catch (e: Exception) {
            LogUtil.e(e.message)
            return mutableListOf()
        }

        return list
    }

    override fun setHotSearch(hotSearch: MutableList<Keyworks>) {
        if (hotSearch.size==0){
            return
        }
        val list: MutableList<com.yidianling.consultant.bean.Keyworks> = try {
            val jsonStr = gson.toJson(hotSearch)
            Gson().fromJson<MutableList<com.yidianling.consultant.bean.Keyworks>>(jsonStr, object : TypeToken<MutableList<com.yidianling.consultant.bean.Keyworks>>() {
            }.type)
        } catch (e: Exception) {
            LogUtil.e(e.message)
            mutableListOf()
        }
        return ConsultantDataManager.getRam().setHotSearch(list)
    }
}