package com.mints.goodmoney.ad.video;

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

import com.mints.goodmoney.common.AppConfig;
import com.mints.goodmoney.common.Constant;
import com.mints.goodmoney.mvp.model.VedioAdingBean;
import com.mints.goodmoney.mvp.model.WeightBean;
import com.mints.goodmoney.utils.LogUtil;

import java.util.ArrayList;
import java.util.Random;

/**
 * 视频广告播放
 */
public class VedioAdingManager {

    private static final String TAG = VedioAdingManager.class.getSimpleName();

    private static VedioAdingManager _inst;

    private VedioAdingListener vedioAdingListener;

    private CsjVedioAdManager csjVedioAdManager;
    private YlhVedioAdManager ylhVedioAdManager;

    private int ylhWeight;
    private int csjVedioWeight;
    private ArrayList<WeightBean> weightList;

    private Activity activity;
    private boolean isVedioAdDownload = false;// 是否点击广告下载 true-点击

    public static VedioAdingManager getInstance(Activity activity) {
        if (_inst != null) {
            return _inst;
        } else {
            _inst = new VedioAdingManager(activity);
            return _inst;
        }
    }

    private VedioAdingManager(Activity activity) {
        init(activity);
        return;
    }

    /**
     * 初始化广告manager
     *
     * @param activity
     */
    private void init(Activity activity) {
        this.activity = activity;
        csjVedioAdManager = CsjVedioAdManager.getInstance(activity);
        ylhVedioAdManager = YlhVedioAdManager.getInstance(activity);
        weightList = new ArrayList<>();
    }

    /**
     * 初始化广告权重
     */
    public void initAdWeight(int csjVedioWeight, int ylhWeight) {
        this.csjVedioWeight = csjVedioWeight;
        this.ylhWeight = ylhWeight;
    }

    /**
     * 根据类型加载对应视频(第一次加载)
     *
     * @param bean
     */
    public void loadAding(Activity activity, VedioAdingBean bean) {
        this.activity = activity;
        isVedioAdDownload = false;
        loadAdVedio(getVedioAdType(), bean);
    }

    /**
     * 加载失败优先级对应视频
     *
     * @param bean
     */
    public void loadFailAding(Activity activity, VedioAdingBean bean) {
        this.activity = activity;
        isVedioAdDownload = false;
        loadAdVedio(getNextAdType(), bean);
    }

    /**
     * 根据广告优先级获取下一个
     * <p>
     * 穿山甲激励> 优量汇>闪电盒子> 珊瑚>创神>mintegral>全屏>多盟>东方网
     */
    private String getNextAdType() {
        String nextAdType = "";
        if (AppConfig.csjVedioAdCount > 0) {
            nextAdType = Constant.CSJ_VEDIO_AD;
        } else {
            nextAdType = Constant.YLH_VEDIO_AD;
        }
        return nextAdType;
    }

    /**
     * 获取广告类型配置
     */
    private String getVedioAdType() {
        // 根据权重获取广告类型
        String adType = getRandomWeight();

        LogUtil.d(TAG, "权重结果返回的广告类型：" + adType);
        return adType;
    }

    /**
     * 根据权重获取广告类型
     *
     * @return
     */
    private String getRandomWeight() {
        if (weightList != null) {
            weightList.clear();
        }
        int weight = 0;

        // 若没有视频完成数，不加入权重计算范围内
        // 穿山甲激励> 优量汇
        if (AppConfig.csjVedioAdCount > 0) {
            weightList.add(new WeightBean(csjVedioWeight, Constant.CSJ_VEDIO_AD));
            weight = weight + csjVedioWeight;
            LogUtil.d(TAG, "权重值：csjVedioWeight:" + csjVedioWeight);
        }
        if (AppConfig.ylhAdCount > 0) {
            weightList.add(new WeightBean(ylhWeight, Constant.YLH_VEDIO_AD));
            weight = weight + ylhWeight;
            LogUtil.d(TAG, "权重值：ylhWeight:" + ylhWeight);
        }
        // 权重随机
        if (weightList != null && weightList.size() > 0 && weight > 0) {
            LogUtil.d(TAG, "1、权重总值：" + weight);
            Random r = new Random();
            int randomWeight = r.nextInt(weight) + 1;
            LogUtil.d(TAG, "2、权重随机值：" + randomWeight);

//            ToastUtil.showLong(activity,"权重随机值：" + randomWeight);

            for (WeightBean weightBean : weightList) {
                randomWeight -= weightBean.getWeight();
                if (randomWeight <= 0) {
                    LogUtil.d(TAG, "3、权重结果：" + weightBean.getType());
                    return weightBean.getType();
                }
            }
        }
        return "";
    }

    /**
     * 是否点击下载
     *
     * @return
     */
    public boolean isVedioAdDownload() {
        return this.isVedioAdDownload;
    }

    /**
     * 获取所有视频完成数状态  true-无可看视频
     *
     * @return
     */
    public boolean getVedioFinishFlag() {
        int csjVedioAdCount = AppConfig.csjVedioAdCount;
        int ylhAdCount = AppConfig.ylhAdCount;

        if (ylhAdCount <= 0 && csjVedioAdCount <= 0) {
//            showToast("今日视频已看完，请明天再来吧");
            return true;
        }
        return false;
    }

    /**
     * 释放资源
     */
    public void onDestory() {
        if (csjVedioAdManager != null) {
            csjVedioAdManager.setCsjVedioAdListener(null);
            csjVedioAdManager.onDestory();
            csjVedioAdManager = null;
        }
        if (ylhVedioAdManager != null) {
            ylhVedioAdManager.setYlhVedioAdListener(null);
            ylhVedioAdManager.onDestory();
            ylhVedioAdManager = null;
        }

        activity = null;
    }

    public void setVedioAdingListener(VedioAdingListener vedioAdingListener) {
        this.vedioAdingListener = vedioAdingListener;
    }

    /**
     * 加载成功失败回调
     */
    public interface VedioAdingListener {
        void vedioAdingListenerError(String adType);

        void vedioAdingListenerFail(String adType);

        void vedioAdingListenerSuccess(String adType);
    }

    /**
     * 展示视频
     *
     * @param vedioAdType
     * @param bean
     */
    private void loadAdVedio(String vedioAdType, VedioAdingBean bean) {
        try {
            if (TextUtils.equals(vedioAdType, Constant.YLH_VEDIO_AD)) {
                // 优量汇
                if (ylhVedioAdManager == null) {
                    if (activity == null) {
                        if (vedioAdingListener != null) {
                            vedioAdingListener.vedioAdingListenerError(Constant.YLH_VEDIO_AD);
                        }
                        return;
                    }
                    ylhVedioAdManager = YlhVedioAdManager.getInstance(activity);
                }

                ylhVedioAdManager.setYlhVedioAdListener(new YlhVedioAdManager.YlhVedioAdListener() {
                    @Override
                    public void ylhVedioAdFail() {
                        if (vedioAdingListener != null) {
                            vedioAdingListener.vedioAdingListenerFail(Constant.YLH_VEDIO_AD);
                        }
                    }

                    @Override
                    public void ylhVedioAdSuccess() {
                        AppConfig.ylhAdCount--;
                        if (vedioAdingListener != null) {
                            vedioAdingListener.vedioAdingListenerSuccess(Constant.YLH_VEDIO_AD);
                        }
                    }

                    @Override
                    public void ylhVedioAdDownload() {
                        isVedioAdDownload = true;
                    }
                });
                ylhVedioAdManager.loadAd(activity, bean.getCurCoin(), bean.getCarrierType(), bean.getExtraId());
            } else if (TextUtils.equals(vedioAdType, Constant.CSJ_VEDIO_AD)) {
                // 穿山甲激励视频
                if (csjVedioAdManager == null) {
                    if (activity == null) {
                        if (vedioAdingListener != null) {
                            vedioAdingListener.vedioAdingListenerError(Constant.CSJ_VEDIO_AD);
                        }
                        return;
                    }
                    csjVedioAdManager = CsjVedioAdManager.getInstance(activity);
                }


                csjVedioAdManager.setCsjVedioAdListener(new CsjVedioAdManager.CsjVedioAdListener() {
                    @Override
                    public void csjVedioAdFail() {
                        if (vedioAdingListener != null) {
                            vedioAdingListener.vedioAdingListenerFail(Constant.CSJ_VEDIO_AD);
                        }
                    }

                    @Override
                    public void csjVedioAdSuccess() {
                        AppConfig.csjVedioAdCount--;
                        if (vedioAdingListener != null) {
                            vedioAdingListener.vedioAdingListenerSuccess(Constant.CSJ_VEDIO_AD);
                        }
                    }

                    @Override
                    public void csjVedioDownload() {
                        isVedioAdDownload = true;
                    }
                });
                csjVedioAdManager.loadAd(activity, bean.getCurCoin(), bean.getCarrierType(), bean.getExtraId());
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (vedioAdingListener != null) {
                vedioAdingListener.vedioAdingListenerError(bean.getCarrierType());
            }
        }
    }
}
