package com.ss.android.ugc.aweme.discover.model;

import X.2J2;
import X.2kf;
import X.9Y1;
import X.CqM;
import X.Enn;
import X.O9Z;
import X.ST7;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Pair;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.google.gson.Gson;
import com.google.gson.b.a;
import com.ss.android.ugc.aweme.AccountService;
import com.ss.android.ugc.aweme.IAccountService;
import com.ss.android.ugc.aweme.IAccountUserService;
import com.ss.android.ugc.aweme.music.model.MusicSearchHistory;
import com.ss.android.ugc.aweme.profile.model.User;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class SearchHistoryManager implements 2J2, ISearchHistoryManager {
    public static int MAX_VISIBLE_HISTORY_COUNT = 20;
    public static String UN_LOGIN_USER_ID = "FAKE_USER";
    public static final Map<String, ISearchHistoryManager> managerMap = new ArrayMap(4);
    public IAccountService accountService;
    public List<Pair<String, List<SearchHistory>>> allLocalHistory;
    public List<SearchHistory> currentUserLocalHistory;
    public Gson gson = new Gson();
    public int mMaxCacheNumber = 20;
    public int mMaxUserCacheNumber = 10;
    public String saveKey;

    public void clearForAccountChange() {
    }

    public String getKey() {
        return this.saveKey;
    }

    public List<SearchHistory> getSearchHistory() {
        return this.currentUserLocalHistory;
    }

    private void onSearchHistoryChanged() {
        CqM.LIZIZ(new O9Z());
    }

    static {
        Covode.recordClassIndex(65184);
    }

    private String getCurrentUserId() {
        IAccountUserService LJFF = this.accountService.LJFF();
        if (LJFF.isLogin()) {
            return LJFF.getCurUserId();
        }
        return UN_LOGIN_USER_ID;
    }

    private String getNewSearchHistory() {
        return Enn.LIZ("search").LIZ(getKey(), "");
    }

    private void removeFakeUserHistory() {
        Iterator<Pair<String, List<SearchHistory>>> it = this.allLocalHistory.iterator();
        while (it.hasNext()) {
            Pair next = it.next();
            if (next != null && TextUtils.equals((CharSequence) next.first, UN_LOGIN_USER_ID)) {
                2kf.LIZIZ(4, "Search_history_manager", "removeFakeUserHistory, remove history = : " + getHistoryKeywordList((List) next.second));
                it.remove();
            }
        }
    }

    private void updateSearchHistoryInSp() {
        try {
            2kf.LIZIZ(4, "Search_history_manager", "updateSearchHistoryInSp, currentUserId = " + getCurrentUserId() + ", saveKey = " + getKey() + ", allLocalHistory = " + this.allLocalHistory);
            String LIZIZ = this.gson.LIZIZ(this.allLocalHistory, new a<List<Pair<String, List<MusicSearchHistory>>>>() {
                static {
                    Covode.recordClassIndex(65185);
                }
            }.type);
            updateSearchHistory(LIZIZ);
            2kf.LIZIZ(4, "Search_history_manager", "updateSearchHistoryInSp, history saved to file = ".concat(String.valueOf(LIZIZ)));
        } catch (Exception e) {
            2kf.LIZIZ(4, "Search_history_manager", "updateSearchHistoryInSp, *** exception = " + e.getMessage());
        }
    }

    public void clearSearchHistory() {
        2kf.LIZIZ(4, "Search_history_manager", "clearSearchHistory, currentUserId = " + getCurrentUserId() + ", original =  " + getHistoryKeywordList(this.currentUserLocalHistory));
        this.currentUserLocalHistory.clear();
        StringBuilder sb = new StringBuilder("clearSearchHistory, new =  ");
        sb.append(getHistoryKeywordList(this.currentUserLocalHistory));
        2kf.LIZIZ(4, "Search_history_manager", sb.toString());
        updateSearchHistoryInSp();
        onSearchHistoryChanged();
    }

    private void ensureCurrentUserLocalHistory() {
        String currentUserId = getCurrentUserId();
        Iterator<Pair<String, List<SearchHistory>>> it = this.allLocalHistory.iterator();
        this.currentUserLocalHistory = new ArrayList();
        while (it.hasNext()) {
            Pair next = it.next();
            if (next != null && TextUtils.equals((CharSequence) next.first, currentUserId)) {
                this.currentUserLocalHistory.addAll((Collection) next.second);
                it.remove();
            }
        }
        2kf.LIZIZ(4, "Search_history_manager", "ensureCurrentUserLocalHistory, currentUserId = " + getCurrentUserId() + ", currentUserLocalHistory = " + getHistoryKeywordList(this.currentUserLocalHistory));
        this.allLocalHistory.add(0, new Pair(currentUserId, this.currentUserLocalHistory));
        StringBuilder sb = new StringBuilder("ensureCurrentUserLocalHistory, allLocalHistory = ");
        sb.append(this.allLocalHistory);
        2kf.LIZIZ(4, "Search_history_manager", sb.toString());
        if (this.mMaxUserCacheNumber > 0 && this.allLocalHistory.size() > this.mMaxUserCacheNumber) {
            List<Pair<String, List<SearchHistory>>> list = this.allLocalHistory;
            list.remove(list.size() - 1);
            2kf.LIZIZ(4, "Search_history_manager", "ensureCurrentUserLocalHistory, allLocalHistory = " + this.allLocalHistory);
        }
        updateSearchHistoryInSp();
    }

    private List<Pair<String, List<SearchHistory>>> getSearchHistoryInSp() {
        try {
            2kf.LIZIZ(4, "Search_history_manager", "getSearchHistoryInSp, currentUserId = " + getCurrentUserId() + ", saveKey = " + getKey());
            String newSearchHistory = getNewSearchHistory();
            2kf.LIZIZ(4, "Search_history_manager", "getSearchHistoryInSp, history json = ".concat(String.valueOf(newSearchHistory)));
            if (!TextUtils.isEmpty(newSearchHistory)) {
                this.allLocalHistory = (List) this.gson.LIZ(newSearchHistory, new a<List<Pair<String, List<SearchHistory>>>>() {
                    static {
                        Covode.recordClassIndex(65186);
                    }
                }.type);
            }
        } catch (Exception e) {
            2kf.LIZIZ(4, "Search_history_manager", "getSearchHistoryInSp, *** exception = " + e.getMessage());
        }
        if (this.allLocalHistory == null) {
            this.allLocalHistory = new ArrayList();
        }
        2kf.LIZIZ(4, "Search_history_manager", "getSearchHistoryInSp, allLocalHistory get from file = " + this.allLocalHistory);
        return this.allLocalHistory;
    }

    private void updateSearchHistory(String str) {
        Enn.LIZ("search").LIZIZ(getKey(), str);
    }

    private List<String> getHistoryKeywordList(List<SearchHistory> list) {
        ArrayList arrayList = new ArrayList(list.size());
        for (SearchHistory next : list) {
            if (next != null) {
                arrayList.add(next.keyword);
            }
        }
        return arrayList;
    }

    public static String getSpKeyByEnterFrom(String str) {
        if (!ST7.LIZ(str) || 9Y1.LIZ().LIZ("TTK_mall_search_specify_history", 0, true) != 1) {
            return "recent_history_v2";
        }
        return "history_".concat(String.valueOf(str));
    }

    public static ISearchHistoryManager inst(String str) {
        MethodCollector.i(8478);
        String spKeyByEnterFrom = getSpKeyByEnterFrom(str);
        Map<String, ISearchHistoryManager> map = managerMap;
        SearchHistoryManager searchHistoryManager = (ISearchHistoryManager) map.get(spKeyByEnterFrom);
        if (searchHistoryManager == null) {
            synchronized (map) {
                try {
                    searchHistoryManager = (ISearchHistoryManager) map.get(spKeyByEnterFrom);
                    if (searchHistoryManager == null) {
                        searchHistoryManager = new SearchHistoryManager(spKeyByEnterFrom);
                        map.put(spKeyByEnterFrom, searchHistoryManager);
                    }
                } finally {
                    MethodCollector.o(8478);
                }
            }
        }
        return searchHistoryManager;
    }

    public SearchHistoryManager(String str) {
        this.saveKey = str;
        IAccountService LIZ = AccountService.LIZ();
        this.accountService = LIZ;
        LIZ.LIZ(this);
        2kf.LIZIZ(4, "Search_history_manager", "SearchHistoryManager init, currentUserId = " + getCurrentUserId() + ", saveKey = " + getKey());
        this.allLocalHistory = getSearchHistoryInSp();
        ensureCurrentUserLocalHistory();
    }

    public void deleteSearchHistory(SearchHistory searchHistory) {
        2kf.LIZIZ(4, "Search_history_manager", "deleteSearchHistory, currentUserId = " + getCurrentUserId() + ", delete history = " + searchHistory.keyword + ", original = " + getHistoryKeywordList(this.currentUserLocalHistory));
        this.currentUserLocalHistory.remove(searchHistory);
        StringBuilder sb = new StringBuilder("deleteSearchHistory, new = ");
        sb.append(getHistoryKeywordList(this.currentUserLocalHistory));
        2kf.LIZIZ(4, "Search_history_manager", sb.toString());
        updateSearchHistoryInSp();
        onSearchHistoryChanged();
    }

    public List<SearchHistory> getSearchHistoryByType(int i) {
        ArrayList<SearchHistory> arrayList;
        ArrayList arrayList2 = new ArrayList();
        List<SearchHistory> list = this.currentUserLocalHistory;
        if (list == null || list.size() <= 0) {
            2kf.LIZIZ(4, "Search_history_manager", "getSearchHistoryByType, currentUserLocalHistory is null or empty, currentUserId = " + getCurrentUserId());
            return arrayList2;
        }
        try {
            arrayList = new ArrayList<>(this.currentUserLocalHistory);
        } catch (Exception e) {
            2kf.LIZIZ(4, "Search_history_manager", "getSearchHistoryByType, *** exception = " + e.getMessage());
            arrayList = new ArrayList<>();
        }
        for (SearchHistory searchHistory : arrayList) {
            if (searchHistory != null && searchHistory.type == i) {
                arrayList2.add(searchHistory);
            }
            if (i == 0 && arrayList2.size() == MAX_VISIBLE_HISTORY_COUNT) {
                break;
            }
        }
        2kf.LIZIZ(4, "Search_history_manager", "getSearchHistoryByType, currentUserId = " + getCurrentUserId() + ", get history result = " + getHistoryKeywordList(arrayList2));
        return arrayList2;
    }

    public void recordSearchHistory(SearchHistory searchHistory) {
        2kf.LIZIZ(4, "Search_history_manager", "recordSearchHistory, currentUserId = " + getCurrentUserId() + ", add history = " + searchHistory.keyword + ", original =  " + getHistoryKeywordList(this.currentUserLocalHistory));
        this.currentUserLocalHistory.remove(searchHistory);
        this.currentUserLocalHistory.add(0, searchHistory);
        StringBuilder sb = new StringBuilder("recordSearchHistory, new =  ");
        sb.append(getHistoryKeywordList(this.currentUserLocalHistory));
        2kf.LIZIZ(4, "Search_history_manager", sb.toString());
        if (this.mMaxCacheNumber > 0 && this.currentUserLocalHistory.size() > this.mMaxCacheNumber) {
            2kf.LIZIZ(4, "Search_history_manager", "recordSearchHistory, remove last one when exceed maxCacheNumber, original =  " + getHistoryKeywordList(this.currentUserLocalHistory));
            List<SearchHistory> list = this.currentUserLocalHistory;
            list.remove(list.size() + -1);
            2kf.LIZIZ(4, "Search_history_manager", "recordSearchHistory, remove last one when exceed maxCacheNumber, new =  " + getHistoryKeywordList(this.currentUserLocalHistory));
        }
        updateSearchHistoryInSp();
        onSearchHistoryChanged();
    }

    public void onAccountResult(int i, boolean z, int i2, User user) {
        if (i != 2) {
            if (i == 1 || i == 3) {
                removeFakeUserHistory();
                updateSearchHistoryInSp();
            } else {
                return;
            }
        }
        2kf.LIZIZ(4, "Search_history_manager", "onAccountResult clear history, currentUserId = " + getCurrentUserId() + ", original = " + this.allLocalHistory);
        this.allLocalHistory.clear();
        StringBuilder sb = new StringBuilder("onAccountResult clear history, new = ");
        sb.append(this.allLocalHistory);
        2kf.LIZIZ(4, "Search_history_manager", sb.toString());
        this.allLocalHistory = getSearchHistoryInSp();
        ensureCurrentUserLocalHistory();
    }
}
