package com.zxhl.cms.utils.cache;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.zxhl.cms.AppContext;
import com.zxhl.cms.ad.upload.cache.ACache;
import com.zxhl.cms.utils.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gaoleichao on 2018/5/5.
 */

public class UserCacheUtils {

    private static final String USER_INFO_KEY = "user_info_key";
    private static final String USER_TOKEN_KEY = "user_token_key";

    private static final String NEWS_READ_RECORD = "news_read_record";

    private static final String SE_EN_FILE = "se_en_file";


    private static final String SHARE_MINIPROGRAM_IMAGE_URL = "share_mini_program_image_url";
    private static final String SHARE_INVITE_IMAGE_URL = "share_invite_image_url";

    private static final String SHARE_INVITE_SMS_TEXT = "share_invite_sms_text";
    private static final String SHARE_AWAKE_SMS_TEXT = "share_awake_sms_text";


    private static ACache aCache;
    private static Map<String, Object> aMemory = new HashMap<>();

    private static ACache init(Context context) {
        if (aCache != null) {
            return aCache;
        }
        aCache = ACache.get(context);
        return aCache;
    }


    /**
     * 保存阅读过的新闻
     *
     * @param context
     */
    public static void saveNewsRecord(Context context, String id) {
        String ids = getNewsRecord(context);
        if (TextUtils.isEmpty(ids)) {
            ids = id;
        } else {
            if (!isReaded(id)) {
                ids = ids + "," + id;
            } else {
                return;
            }
        }
        LogUtils.d("UserCacheUtils", ids);
        init(context).put(NEWS_READ_RECORD, ids);
        aMemory.put(NEWS_READ_RECORD, ids);
    }

    /**
     * 是否阅读过该条新闻
     *
     * @param
     */
    public static boolean isReaded(String currentId) {
        String idsStr = getNewsRecord(AppContext.get());
        if (TextUtils.isEmpty(idsStr)) {
            return false;
        } else {
            String[] ids = idsStr.split(",");
            for (String id : ids) {
                if (TextUtils.equals(currentId, id)) {
                    return true;
                }
            }
            return false;
        }
    }

    /**
     * 获取阅读过的新闻
     *
     * @param context
     * @return
     */
    public static synchronized String getNewsRecord(Context context) {
        if (aMemory.containsKey(NEWS_READ_RECORD)) {
            return (String) aMemory.get(NEWS_READ_RECORD);
        }
        String id = init(context).getAsString(NEWS_READ_RECORD);
        aMemory.put(NEWS_READ_RECORD, id);
        return id;
    }

    public static synchronized String getSeen(Context context) {
        if (aMemory.containsKey(SE_EN_FILE)) {
            return (String) aMemory.get(SE_EN_FILE);
        }
        String str = init(context).getAsString(SE_EN_FILE);
        aMemory.put(SE_EN_FILE, str);
        return str;
    }

    public static void saveSeen(Context context, String str) {
        init(context).put(SE_EN_FILE, str);
        aMemory.put(SE_EN_FILE, str);
    }

    public static synchronized String getMiniProgramImageUrl() {
        if (aMemory.containsKey(SHARE_MINIPROGRAM_IMAGE_URL)) {
            return (String) aMemory.get(SHARE_MINIPROGRAM_IMAGE_URL);
        }
        String str = init(AppContext.get()).getAsString(SHARE_MINIPROGRAM_IMAGE_URL);
        aMemory.put(SHARE_MINIPROGRAM_IMAGE_URL, str);
        return str;
    }

    public static void svaeMiniProgramImageUrl(String str) {
        init(AppContext.get()).put(SHARE_MINIPROGRAM_IMAGE_URL, str);
        aMemory.put(SHARE_MINIPROGRAM_IMAGE_URL, str);
    }

    public static synchronized String getInviteImageUrl() {
        if (aMemory.containsKey(SHARE_INVITE_IMAGE_URL)) {
            return (String) aMemory.get(SHARE_INVITE_IMAGE_URL);
        }
        String str = init(AppContext.get()).getAsString(SHARE_INVITE_IMAGE_URL);
        aMemory.put(SHARE_INVITE_IMAGE_URL, str);
        return str;
    }

    public static void svaeInviteImageUrl(String str) {
        init(AppContext.get()).put(SHARE_INVITE_IMAGE_URL, str);
        aMemory.put(SHARE_INVITE_IMAGE_URL, str);
    }

    public static synchronized String getInviteSmsText() {
        if (aMemory.containsKey(SHARE_INVITE_SMS_TEXT)) {
            return (String) aMemory.get(SHARE_INVITE_SMS_TEXT);
        }
        String str = init(AppContext.get()).getAsString(SHARE_INVITE_SMS_TEXT);
        aMemory.put(SHARE_INVITE_SMS_TEXT, str);
        return str;
    }

    public static void saveInviteSmsText(String str) {
        init(AppContext.get()).put(SHARE_INVITE_SMS_TEXT, str);
        aMemory.put(SHARE_INVITE_SMS_TEXT, str);
    }

    public static synchronized String getAwakeSmsText() {
        if (aMemory.containsKey(SHARE_AWAKE_SMS_TEXT)) {
            return (String) aMemory.get(SHARE_AWAKE_SMS_TEXT);
        }
        String str = init(AppContext.get()).getAsString(SHARE_AWAKE_SMS_TEXT);
        aMemory.put(SHARE_AWAKE_SMS_TEXT, str);
        return str;
    }

    public static void saveAwakeSmsText(String str) {
        init(AppContext.get()).put(SHARE_AWAKE_SMS_TEXT, str);
        aMemory.put(SHARE_AWAKE_SMS_TEXT, str);
    }

    public static void insertUpDate(Context context, String title, String content) {
        init(context).put(title, content);
    }

    public static String queryByTitle(Context context, String title) {
        return init(context).getAsString(title);
    }

    /**
     * @param context
     * @param keyword
     */
    public static synchronized void addDataList(Context context, String keyword, String key) {
        boolean isHasSame = false;
        List<String> list;
        list = getDataList(context, key);
        StringBuilder sb = new StringBuilder();
        if (list != null && list.size() > 0) {
            LogUtils.d("addSearchHistoriesList", "befor=" + list.toString());
            if (list.get(0).equals(keyword)) {
                return;
            }
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).equals(keyword)) {
                    isHasSame = true;
                    continue;
                }
                if (i == 0) {
                    sb.append(keyword).append("@");
                }
                if (list.size() == 50) {
                    if (i < 49 | isHasSame) {
                        sb.append(list.get(i)).append("@");
                    }
                } else {
                    sb.append(list.get(i)).append("@");
                }
            }
            saveSearchHistoriesList(context, sb.toString(), key);
        } else {
            saveSearchHistoriesList(context, keyword, key);
        }
    }

    public static void saveSearchHistoriesList(Context context, String keyword, String key) {
        insertUpDate(context, key, keyword);
    }

    /**
     * 获取历史搜索列表
     *
     * @param context
     * @return
     */
    public static List<String> getDataList(Context context, String key) {
        List<String> list = new ArrayList<>();
        String data = queryByTitle(context, key);
        if (TextUtils.isEmpty(data))
            return null;
        String[] searchkeyword = data.split("@");
        for (int i = 0; i < searchkeyword.length; i++) {
            list.add(searchkeyword[i]);
        }
        return list;
    }

    public static void clearDataList(Context context, String key) {
        delete(context, key);
    }

    public static void delete(Context context, String title) {
        init(context).remove(title);
    }
}
