package com.ycgis.macall.personalcenter.util;

import com.rs.macall.androidx.basemodel.utils.StringUtil;
import com.ycgis.macall.personalcenter.p.app.RuanseeApplication;


/**
 * created by:   Macall
 * create time:  2022/12/5 15:41
 * copyright:    @ruansee.com
 * Describe:
 */
@Deprecated
public class SearchHistoryUtils {

    public static final String SEARCH_HISTORY = "searchHistory";

    /**
     * 添加搜索记录
     * @param search    搜索内容
     */
    public static void addHistory(String search) {
        if (StringUtil.isNullOrEmpty(search))return;
        String searchHistory = RuanseeApplication.getStringValue(SEARCH_HISTORY, "");
        if (StringUtil.hasContent(searchHistory)) {
            String nowSearchHistory = search + "," + searchHistory;
//            StringBuilder builder = new StringBuilder();
//            String sp = ",";
//            String[] split = searchHistory.split(",");
//            builder.append(search);
//            for (int i = 0; i < split.length; i++) {
//                if (split[i].equals(search)){
//                    continue;
//                }
//                builder.append(sp);
//                builder.append(split[i]);
//            }
            RuanseeApplication.saveData(SEARCH_HISTORY, nowSearchHistory);
            return;
        }
        RuanseeApplication.saveData(SEARCH_HISTORY, search);
    }

    public static String[] getListHistory() {
        String[] searchArr = null;
        String searchHistory = RuanseeApplication.getStringValue(SEARCH_HISTORY, "");
        if (StringUtil.hasContent(searchHistory)) {
            String[] split = searchHistory.split(",");
            StringBuilder builder = new StringBuilder();
            if (split.length > 8) {
                searchArr = new String[8];
                for (int i = 0; i < 8; i++) {
                    searchArr[i] = split[i];
                }
                searchHistory = builder.toString();
                RuanseeApplication.saveData(SEARCH_HISTORY, searchHistory);
            } else {
                searchArr = split;
            }
        }
        return searchArr;
    }

    public static void getCancelHistory() {
        RuanseeApplication.cleanStringValue(SEARCH_HISTORY);
    }

    /**
     * 是否存在历史记录
     * @return true 存在    false 不存在
     */
    public static boolean isHistory(){
        String searchHistory = RuanseeApplication.getStringValue(SEARCH_HISTORY, "");
        return StringUtil.hasContent(searchHistory);

    }
}
