package com.gem.media.ad;

import android.text.TextUtils;

import com.gem.media.splash.base.utils.ALog;
import com.gem.media.splash.base.utils.PROP;
import com.or.ange.dot.utils.LLog;

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

public class AdData {

    public static final String AD_TYPE_OPEN = "kaipin";
    public static final String AD_TYPE_INTERSTITIAL = "chapin";
    public static final String AD_TYPE_NATIVE = "yuansheng";
    public static final String AD_TYPE_HALF_INTERSTITIAL = "halfChapin";

    public String appId;
    public List<String> adTypes;
    public HashMap<String, List<String>> mapHashMap = new HashMap<>();


    public void setAppId(String appId) {
        this.appId = appId;
    }


//    public void setAdType(String adType) {
//        if (null == adTypes) {
//            adTypes = new ArrayList<>();
//        }
//        if (adTypes.isEmpty()) {
//            adTypes.add(adType);
//        } else {
//            if (!adTypes.contains(adType)) {
//                adTypes.add(adType);
//            }
//        }
//    }

    public void addData(String adType, String adId) {
        if (null == mapHashMap){
            mapHashMap = new HashMap<>();
        }
        List<String> data = mapHashMap.get(adType);
        if (data == null) {
            data = new ArrayList<>();
            data.add(adId);
            mapHashMap.put(adType, data);
        } else {
            if (!data.contains(adId)) {
                data.add(adId);
            }
        }
    }

    public boolean hasAdData() {
        if (mapHashMap == null || mapHashMap.isEmpty()) {
            return false;
        }
        if (adTypes.isEmpty()) {
            return false;
        }
        boolean hasAdData = false;
        for (String type : adTypes) {
            List<String> data = mapHashMap.get(type);
            hasAdData = (data != null && !data.isEmpty());
            if (!hasAdData) {
                break;
            }
        }
        return hasAdData;
    }

    public void clearAdIdData() {
        if (mapHashMap == null || mapHashMap.isEmpty()) {
            return;
        }
        mapHashMap.put(AdData.AD_TYPE_INTERSTITIAL, null);
    }

    public String getAdAdId(String adType) {
        ALog.w("GEM_WZ", "   getAdAdId   == " + mapHashMap);
        if (mapHashMap == null || mapHashMap.isEmpty()) {
            return "";
        }
        List<String> data = mapHashMap.get(adType);
        if (data == null || data.isEmpty()) {
            return "";
        }
        return data.get(0);
    }

    public String getAdAdId(String adType, int index) {
        LLog.w("GEM_WZ", "   getAdAdId   == " + mapHashMap);
        if (index < 0) {
            return "";
        }
        if (mapHashMap == null || mapHashMap.isEmpty()) {
            return "";
        }
        List<String> data = mapHashMap.get(adType);
        if (data == null || data.isEmpty()) {
            return "";
        }
        if (index < data.size()) {
            return data.get(index);
        }
        return "";
    }


}
