package com.anythink.reactnative.reward;

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

import com.thinkup.core.api.TUAdInfo;
import com.thinkup.core.api.TUAdStatusInfo;
import com.thinkup.core.api.TUNetworkConfirmInfo;
import com.thinkup.core.api.TUShowConfig;
import com.thinkup.core.api.AdError;
import com.anythink.reactnative.event.ATReactNativeEventEmitter;
import com.anythink.reactnative.listener.AdListenerFactory;
import com.anythink.reactnative.utils.BridgeJsonMapUtil;
import com.anythink.reactnative.utils.Const;
import com.anythink.reactnative.utils.MsgTools;
import com.anythink.reactnative.utils.Utils;
import com.thinkup.rewardvideo.api.TURewardVideoAutoAd;
import com.thinkup.rewardvideo.api.TURewardVideoAutoEventListener;
import com.thinkup.rewardvideo.api.TURewardVideoAutoLoadListener;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;

/**
* Incentive video AutoLoad.
* callbackName is forwarded through the only channel, which is exactly the same as manual loading.
 */
public class ATAutoLoadRewardVideoHelper {

    public final Set<String> placementIDs = Collections.synchronizedSet(new HashSet<>());
    private final Map<String, Map<String, Object>> placementSettings = new HashMap<>();

    private static volatile ATAutoLoadRewardVideoHelper instance;

    private ATAutoLoadRewardVideoHelper() {
    }

    public static ATAutoLoadRewardVideoHelper getInstance() {
        if (instance == null) {
            synchronized (ATAutoLoadRewardVideoHelper.class) {
                if (instance == null) {
                    instance = new ATAutoLoadRewardVideoHelper();
                }
            }
        }
        return instance;
    }

    private void emit(String callbackKey, String placementId, TUAdInfo adInfo, String errMsg,
                      Map<String, Object> extra) {
        Map<String, Object> info = null;
        if (adInfo != null) {
            try {
                info = Utils.jsonStrToMap(adInfo.toString());
            } catch (Throwable ignore) {
            }
        }
        ATReactNativeEventEmitter.getInstance().sendCallback(
                Const.CallbackMethodCall.rewardedVideoCall, callbackKey, placementId, info, errMsg, extra);
    }

    private final TURewardVideoAutoLoadListener autoLoadListener = new TURewardVideoAutoLoadListener() {
        @Override
        public void onRewardVideoAutoLoaded(String placementId) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onRewardVideoAutoLoaded",
                    "placementId=" + placementId);
            emit(Const.RewardVideoCallback.LoadedCallbackKey, placementId, null, null, null);
        }

        @Override
        public void onRewardVideoAutoLoadFail(String placementId, AdError adError) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onRewardVideoAutoLoadFail",
                    "placementId=" + placementId + "--error=" + adError.getFullErrorInfo());
            emit(Const.RewardVideoCallback.LoadFailCallbackKey, placementId, null, adError.getFullErrorInfo(), null);
        }
    };

    private final TURewardVideoAutoEventListener autoEventListener = new TURewardVideoAutoEventListener() {
        @Override
        public void onRewardedVideoAdPlayStart(TUAdInfo atAdInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onRewardedVideoAdPlayStart",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo);
            emit(Const.RewardVideoCallback.PlayStartCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, null);
        }

        @Override
        public void onRewardedVideoAdPlayEnd(TUAdInfo atAdInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onRewardedVideoAdPlayEnd",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo);
            emit(Const.RewardVideoCallback.PlayEndCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, null);
        }

        @Override
        public void onRewardedVideoAdPlayFailed(AdError adError, TUAdInfo atAdInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onRewardedVideoAdPlayFailed",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo
                            + "--error=" + adError.getFullErrorInfo());
            emit(Const.RewardVideoCallback.PlayFailCallbackKey, atAdInfo.getPlacementId(), atAdInfo,
                    adError.getFullErrorInfo(), null);
        }

        @Override
        public void onRewardedVideoAdClosed(TUAdInfo atAdInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onRewardedVideoAdClosed",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo);
            emit(Const.RewardVideoCallback.CloseCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, null);
        }

        @Override
        public void onRewardedVideoAdPlayClicked(TUAdInfo atAdInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onRewardedVideoAdPlayClicked",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo);
            emit(Const.RewardVideoCallback.ClickCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, null);
        }

        @Override
        public void onReward(TUAdInfo atAdInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onReward",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo);
            emit(Const.RewardVideoCallback.RewardCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, null);
        }

        @Override
        public void onRewardFailed(TUAdInfo atAdInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onRewardFailed",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo);
            emit(Const.RewardVideoCallback.RewardFailedCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, null);
        }

        public void onDeeplinkCallback(TUAdInfo atAdInfo, boolean isSuccess) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onDeeplinkCallback",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo + "--isSuccess=" + isSuccess);
            Map<String, Object> extra = new HashMap<>();
            extra.put(Const.CallbackKey.isDeeplinkSuccess, isSuccess);
            emit(Const.RewardVideoCallback.DeeplinkCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, extra);
        }

        public void onDownloadConfirm(Context context, TUAdInfo atAdInfo, TUNetworkConfirmInfo networkConfirmInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onDownloadConfirm",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo);
            emit(Const.RewardVideoCallback.DownloadConfirmCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, null);
        }

        public void onRewardedVideoAdAgainPlayStart(TUAdInfo atAdInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onRewardedVideoAdAgainPlayStart",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo);
            emit(Const.RewardVideoCallback.AgainPlayStartCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, null);
        }

        public void onRewardedVideoAdAgainPlayEnd(TUAdInfo atAdInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onRewardedVideoAdAgainPlayEnd",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo);
            emit(Const.RewardVideoCallback.AgainPlayEndCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, null);
        }

        public void onRewardedVideoAdAgainPlayFailed(AdError adError, TUAdInfo atAdInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onRewardedVideoAdAgainPlayFailed",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo
                            + "--error=" + adError.getFullErrorInfo());
            emit(Const.RewardVideoCallback.AgainPlayFailCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, null);
        }

        public void onRewardedVideoAdAgainPlayClicked(TUAdInfo atAdInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onRewardedVideoAdAgainPlayClicked",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo);
            emit(Const.RewardVideoCallback.AgainClickCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, null);
        }

        public void onAgainReward(TUAdInfo atAdInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onAgainReward",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo);
            emit(Const.RewardVideoCallback.AgainRewardCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, null);
        }

        @Override
        public void onAgainRewardFailed(TUAdInfo atAdInfo) {
            MsgTools.printCallback("RewardVideo", "ATAutoLoadRewardVideoHelper", "onAgainRewardFailed",
                    "placementId=" + atAdInfo.getPlacementId() + "--adInfo=" + atAdInfo);
            emit(Const.RewardVideoCallback.AgainRewardFailedCallbackKey, atAdInfo.getPlacementId(), atAdInfo, null, null);
        }
    };

    public boolean containsPlacementID(String placementID) {
        return placementIDs.contains(placementID);
    }

    public void autoLoad(String placementId, Map<String, Object> settings, Activity activity) {
        if (TextUtils.isEmpty(placementId) || activity == null) {
            MsgTools.printMsg("autoLoad: empty placementId or no activity");
            return;
        }
        placementIDs.add(placementId);
        if (settings != null) {
            placementSettings.put(placementId, settings);
        }
        String[] arr = new String[]{placementId};
        TURewardVideoAutoAd.init(activity, arr, autoLoadListener);
        if (settings != null) {
            TURewardVideoAutoAd.setLocalExtra(placementId, settings);
        }
        TURewardVideoAutoAd.addPlacementId(arr);
    }

    public void removePlacement(String placementId) {
        if (TextUtils.isEmpty(placementId)) {
            return;
        }
        placementIDs.remove(placementId);
        placementSettings.remove(placementId);
        TURewardVideoAutoAd.removePlacementId(new String[]{placementId});
    }

    public boolean isAdReady(String placementId) {
        return !TextUtils.isEmpty(placementId) && TURewardVideoAutoAd.isAdReady(placementId);
    }

    public void show(String placementId, String scenario, Activity activity) {
        Map<String, Object> cfg = new HashMap<>();
        if (!TextUtils.isEmpty(scenario)) {
            cfg.put("scenarioId", scenario);
        }
        showWithConfigMap(placementId, cfg, activity);
    }

    public void showWithConfigMap(String placementId, Map<String, Object> cfg, Activity activity) {
        if (activity == null) {
            return;
        }
        TUShowConfig.Builder builder = new TUShowConfig.Builder();
        if (cfg != null) {
            String scenario = cfg.containsKey("scenarioId")
                    ? String.valueOf(cfg.get("scenarioId"))
                    : (cfg.containsKey(Const.SCENE_ID) ? String.valueOf(cfg.get(Const.SCENE_ID)) : "");
            String showCustomExt = cfg.containsKey(Const.SHOW_CUSTOM_EXT)
                    ? String.valueOf(cfg.get(Const.SHOW_CUSTOM_EXT)) : null;
            if (!TextUtils.isEmpty(scenario)) {
                builder.scenarioId(scenario);
            }
            if (!TextUtils.isEmpty(showCustomExt)) {
                builder.showCustomExt(showCustomExt);
            }
            BridgeJsonMapUtil.applyAtCustomContentToShowConfigBuilder(
                    builder, cfg.get(Const.AT_CUSTOM_CONTENT_RESULT));
        }
        TURewardVideoAutoAd.show(activity, placementId, builder.build(), autoEventListener,
                AdListenerFactory.revenueListener());
    }
}
