package com.zxhl.cms.ad.upload.cache;

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

import com.zxhl.cms.utils.PhoneUtils;
import com.zxhl.cms.ad.upload.model.ReportAd;

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


/**
 * Created by gaoleichao on 2018/7/14.
 */
public class AdCache {
    private static ACache aCache;

    private static HashMap aMemory = new HashMap<String, Object>();
    private static String ADLOAD_SIZE = "adupload_size";

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

    public static synchronized String getAdImpressionCount(Context context) {
        if (aMemory.containsKey(ADLOAD_SIZE)) {
            return (String) aMemory.get(ADLOAD_SIZE);
        }
        String str = init(context).getAsString(ADLOAD_SIZE);
        if (TextUtils.isEmpty(str)) {
            return "";
        } else {
            aMemory.put(ADLOAD_SIZE, str);
            return str;
        }
    }

    public static void addAdAdImpression(Context context, ReportAd entity) {
        String str = getAdImpressionCount(context);
        List<ReportAd> list;
        if (!TextUtils.isEmpty(str)) {
            list = PhoneUtils.INSTANCE.getAdImpressionList(str);
        } else {
            list = new ArrayList();
        }
        list.add(entity);
        init(context).put(ADLOAD_SIZE, PhoneUtils.INSTANCE.ListToJson(list));
        aMemory.put(ADLOAD_SIZE, PhoneUtils.INSTANCE.ListToJson(list));
    }

    public static void saveAdImpressionCount(Context context, String data) {
        init(context).put(ADLOAD_SIZE, data);
        aMemory.put(ADLOAD_SIZE, data);
    }

}