package com.github.material.monitor.demo.mock;
 
import com.thinkup.core.api.TUAdConst;
import com.thinkup.core.api.TUAdInfo;
import com.thinkup.core.api.TUCustomContentResult;
import com.thinkup.core.basead.adx.api.ITUAdxHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Collections;
import java.util.Map;
import java.util.UUID;

/**
 * 无真实广告回调时的 {@link TUAdInfo} 替身，填充网关 meta 必填字段。
 *
 * <p>正式接入请使用 {@code onAdShow} 等回调中的真实 {@link TUAdInfo}。</p>
 */
public final class MockAdInfo extends TUAdInfo {

    private final String placementId;
    private final String showId;
    private final String format;
    private final String adSourceId;
    private final int networkFirmId;
    private final int adSourceAdType;
    private final double ecpm;
    private final String requestId;
    private final Map<String, Object> extInfoMap;

    public MockAdInfo(String placementId,
                      String showId,
                      String format,
                      String adSourceId,
                      int networkFirmId,
                      int adSourceAdType,
                      double ecpm,
                      String requestId,
                      String creativeId) {
        this.placementId = nz(placementId);
        this.showId = nz(showId);
        this.format = nz(format);
        this.adSourceId = nz(adSourceId);
        this.networkFirmId = networkFirmId;
        this.adSourceAdType = adSourceAdType;
        this.ecpm = ecpm;
        this.requestId = nz(requestId);
        this.extInfoMap = buildExtInfoMap(creativeId);
    }

    private static String nz(String s) {
        return s != null ? s : "";
    }

    /** 按海外渠道（如 AdMob）格式，把随机 creative_id 放进 {@code nw_extra_info}。 */
    private static Map<String, Object> buildExtInfoMap(String creativeId) {
        String prefix = creativeId != null && creativeId.length() > 0 ? creativeId : "cr_demo";
        String randomCreativeId = prefix + "_" + UUID.randomUUID().toString().replace("-", "").substring(0, 8);
        try {
            JSONObject nwExtraInfo = new JSONObject();
            nwExtraInfo.put("creative_id", randomCreativeId);
            return Collections.<String, Object>singletonMap("nw_extra_info", nwExtraInfo);
        } catch (JSONException e) {
            return Collections.emptyMap();
        }
    }

    @Override
    public int getNetworkFirmId() {
        return networkFirmId;
    }

    @Override
    public String getAdsourceId() {
        return adSourceId;
    }

    @Override
    public int getAdsourceIndex() {
        return 0;
    }

    @Override
    public double getEcpm() {
        return ecpm;
    }

    @Override
    public double getEcpm(TUAdConst.CURRENCY currency) {
        return ecpm;
    }

    @Override
    public double getEcpmWithCurrency(TUAdConst.CURRENCY currency) {
        return ecpm;
    }

    @Override
    public String getEncEcpmInfo() {
        return "";
    }

    @Override
    public String getRmbEncEcpmInfo() {
        return "";
    }

    @Override
    public int getSecretId() {
        return 0;
    }

    @Override
    public int isHeaderBiddingAdsource() {
        return 0;
    }

    @Override
    public String getShowId() {
        return showId;
    }

    @Override
    public Double getPublisherRevenue() {
        return ecpm;
    }

    @Override
    public Double getPublisherRevenue(TUAdConst.CURRENCY currency) {
        return ecpm;
    }

    @Override
    public String getCurrency() {
        return "USD";
    }

    @Override
    public String getCountry() {
        return "";
    }

    @Override
    public String getTopOnPlacementId() {
        return placementId;
    }

    @Override
    public String getTopOnAdFormat() {
        return format;
    }

    @Override
    public String getPlacementId() {
        return placementId;
    }

    @Override
    public String getFormat() {
        return format;
    }

    @Override
    public String getEcpmPrecision() {
        return "";
    }

    @Override
    public String getAdNetworkType() {
        return "";
    }

    @Override
    public String getNetworkPlacementId() {
        return "";
    }

    @Override
    public int getEcpmLevel() {
        return 0;
    }

    @Override
    public int getSegmentId() {
        return 0;
    }

    @Override
    public String getScenarioId() {
        return "";
    }

    @Override
    public String getScenarioRewardName() {
        return "";
    }

    @Override
    public int getScenarioRewardNumber() {
        return 0;
    }

    @Override
    public String getSubChannel() {
        return "";
    }

    @Override
    public String getChannel() {
        return "";
    }

    @Override
    public String getCustomRule() {
        return "";
    }

    @Override
    public Map<String, Object> getExtInfoMap() {
        return extInfoMap;
    }

    @Override
    public String getRewardUserCustomData() {
        return "";
    }

    @Override
    public Map<String, Object> getLocalExtra() {
        return Collections.emptyMap();
    }

    @Override
    public String getTpBidId() {
        return "";
    }

    @Override
    public int getABTestId() {
        return 0;
    }

    @Override
    public int getDismissType() {
        return 0;
    }

    @Override
    public int getPlacementType() {
        return 0;
    }

    @Override
    public String getSharedPlacementId() {
        return "";
    }

    @Override
    public double getBidFloor() {
        return 0.0;
    }

    @Override
    public String getWaterfallId() {
        return "";
    }

    @Override
    public ITUAdxHandler getAdxHandler() {
        return null;
    }

    @Override
    public int getAdSourceAdType() {
        return adSourceAdType;
    }

    @Override
    public String getAdSourceCustomExt() {
        return "";
    }

    @Override
    public String getNetworkName() {
        return "";
    }

    @Override
    public String getShowCustomExt() {
        return "";
    }

    @Override
    public Map<String, String> getUrlTagParams() {
        return Collections.emptyMap();
    }

    @Override
    public String getRequestId() {
        return requestId;
    }

    @Override
    public int getBidType() {
        return 0;
    }

    @Override
    public TUCustomContentResult getCustomContentResult() {
        return null;
    }
}
