package basic.ad.model;

import android.net.Uri;

import com.wasu.comp.ad.AdConfig;
import com.wasu.comp.ad.WasuAdEngine;
import com.wasu.compfactory.WasuCompFactory;
import com.wasu.cs.model.BkbmTimeModel;
import com.wasu.module.log.WLog;
import com.wasu.vmap.model.AdBreak;
import com.wasu.vmap.model.VMAPModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import basic.app.TvAppLike;
import cn.com.wasu.main.AppUtil;

import static cn.com.wasu.main.AppUtil.bkbmSwitch;

/**
 * Created by fanwang on 9/12/16.
 */
public class WasuAdManager {
    private String TAG = "WasuAdManager";

    private static WasuAdManager sInstance = null;

    private WasuAdManager() {
    }

    public static WasuAdManager getsInstance() {
        if (sInstance == null) {
            synchronized (WasuAdManager.class) {
                if (sInstance == null) {
                    sInstance = new WasuAdManager();
                    sInstance.init();
                }
            }
        }

        return sInstance;
    }

    private void init() {
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String> params2 = new HashMap<String, String>();

        params.put(WASU_AD.KEY_USERID, WASU_AD.VALUE_USERID);
        params.put(WASU_AD.KEY_PROL, WASU_AD.VALUE_PROL);
        params2.put(WASU_AD.KEY_81, WASU_AD.VALUE_81);
        params2.put(WASU_AD.KEY_82, WASU_AD.VALUE_82);
        params2.put(WASU_AD.KEY_76, WASU_AD.VALUE_76);
        params2.put(WASU_AD.KEY_80, WASU_AD.VALUE_80);
        params2.put(WASU_AD.KEY_69, WASU_AD.VALUE_69);
        params2.put(WASU_AD.KEY_74, WASU_AD.VALUE_74);
        params2.put(WASU_AD.KEY_184, WASU_AD.VALUE_184);

//        AdConfig appconfig = new AdConfig(
//                AdConfig.AdType.VAST,
//                WASU_AD.AD_URL,
//                WASU_AD.AD,
//                params,
//                params2);

        AdConfig.Builder builder = AdConfig.builder().adType(AdConfig.AdType.VAST).vmapUrl(WASU_AD.AD_URL).ueUrl(WASU_AD.AD)
                .bkbmUrl(WASU_AD.AD_BKBM).directParams(params).posParams(params2).bkbmListener(new AdConfig.BkbmListener() {
                    @Override
                    public void onSuccess(VMAPModel vmapModel) {
                        if (vmapModel != null && vmapModel.getAdBreaks() != null && vmapModel.getAdBreaks().size()>0){
                            AppUtil.bkbmTime = new ArrayList<BkbmTimeModel>();
                            long time;
                            int pos;

                            for (AdBreak adBreak : vmapModel.getAdBreaks()){
                                time = getIntTime(adBreak.getTimeOffset());
                                pos = getAdPos(adBreak.getAdSource().getAdTagURIs().getContent());
                                if (time != 0 && pos != 0){
                                    AppUtil.bkbmTime.add(AppUtil.bkbmTime.size(),new BkbmTimeModel(time, pos));
                                    WLog.i(TAG, "边看边买增加时间点:"+time+", 广告位为"+pos);
                                }
                            }
                            if (AppUtil.bkbmTime.size() == 0){
                                WLog.e(TAG, "边看边买下发数据无法取到时间点，边看边买关闭！");
                                bkbmSwitch = false;
                            }
                        }else{
                            WLog.e(TAG,"边看边买下发数据错误或为0，边看边买关闭！");
                            bkbmSwitch = false;
                        }
                    }

                    @Override
                    public void onFail() {
                        // 获取失败关闭边看边买
                        bkbmSwitch = false;
                        WLog.e(TAG,"边看边买下发数据错误或为0，边看边买关闭！");
                    }
                });

        WasuAdEngine.getInstance().init(TvAppLike.getContext(), builder.build());
//        WasuAdEngine.getInstance().init(TvApp.getContext(), appconfig);
        new WasuCompFactory().createCompAd(TvAppLike.getContext()).fetchAdGet(WASU_AD.AD, params, null);
    }

    /**
     * 从时间点转换出对应的int时间(秒)
     * */
    private int getIntTime(String sTime){
        int time = 0;
        try {
            time = Integer.valueOf(sTime).intValue();
        }catch (NumberFormatException e){
            e.printStackTrace();
            return 0;
        }

        return time*60;
//        return time;
    }

    /**
     * 从url中获得广告的pos
     * */
    private int getAdPos(String url){
        int pos = 0;
        String p = Uri.parse(url).getQueryParameter("pos");
        try {
            pos = Integer.valueOf(p).intValue();
        }catch (NumberFormatException e){
            e.printStackTrace();
            return 0;
        }
        return pos;
    }
}
