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_INTERSTITIAL = "chapin";
    public static final String AD_TYPE_OPEN = "kaipin";

    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;


    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 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 "";
    }

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

    public int getAdIdSize(String adType) {
        if (mapHashMap == null || mapHashMap.isEmpty()) {
            return -1;
        }
        List<String> data = mapHashMap.get(adType);
        if (data == null || data.isEmpty()) {
            return -1;
        }
        return data.size();
    }

    public boolean canLoadAdData(String adType) {
        boolean can = false;
        if (null == adTypes) {
            adTypes = new ArrayList<>();
            LLog.w("GEM_WZ", " canLoadAdData  adTypes   ==  null");
            return false;
        }
        if (TextUtils.isEmpty(adType)) {
            return false;
        }
        LLog.w("GEM_WZ", " canLoadAdData  adTypes size == " +adTypes.size());
        for (String type : adTypes) {
            LLog.w("GEM_WZ", " canLoadAdData  adTypes type == " +type +"   "+adType);
            if (!TextUtils.isEmpty(type) && type.equals(adType)) {
                can = true;
                break;
            }
        }
        return can;
    }

}
