package com.duben.microtribe.ad;

import android.content.Context;
import android.util.Log;

import com.bytedance.hume.readapk.HumeSDK;
import com.bytedance.sdk.openadsdk.TTAdConfig;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTCustomController;
import com.bytedance.sdk.openadsdk.mediation.init.MediationConfigUserInfoForSegment;
import com.bytedance.sdk.openadsdk.mediation.init.MediationPrivacyConfig;
import com.duben.microtribe.BuildConfig;
import com.duben.microtribe.MintsApplication;
import com.duben.microtribe.common.Constant;
import com.duben.microtribe.utils.AppPreferencesManager;
import com.duben.microtribe.utils.DeviceUuidFactory;
import com.duben.microtribe.utils.LogUtil;

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


/**
 * 可以用一个单例来保存TTAdManager实例，在需要初始化sdk的时候调用
 */
public class TTAdManagerHolder {

    private static final String TT_AD_APPID = BuildConfig.GROMORE_APP_ID;
    private static final String TT_AD_NAME = Constant.MINTS_APP_NAME;

    private static final String TAG = "TTAdManagerHolder";

    private static boolean sInit;


    public static TTAdManager get() {

        return TTAdSdk.getAdManager();
    }

    public static void init(final Context context) {
        //初始化穿山甲SDK
        doInit(context);
    }

    //step1:接入网盟广告sdk的初始化操作，详情见接入文档和穿山甲平台说明
    private static void doInit(Context context) {
        if (!sInit) {
            //TTAdSdk.init(context, buildConfig(context));
            //setp1.1：初始化SDK
            TTAdSdk.init(context, buildConfig(context));
            //setp1.2：启动SDK
            TTAdSdk.start(new TTAdSdk.Callback() {
                @Override
                public void success() {
                    Log.i(TAG, "success: " + TTAdSdk.isInitSuccess());
                }

                @Override
                public void fail(int code, String msg) {
                    Log.i(TAG, "fail:  code = " + code + " msg = " + msg);
                }
            });
            sInit = true;
        }
    }


    private static TTAdConfig buildConfig(Context context) {

        return new TTAdConfig.Builder()
                /**
                 * 注：需要替换成在媒体平台申请的appID ，切勿直接复制
                 */
                .appId(TT_AD_APPID)
                .appName(TT_AD_NAME)
                /**
                 * 上线前需要关闭debug开关，否则会影响性能
                 */
                .debug(BuildConfig.DEBUG)
                /**
                 * 使用聚合功能此开关必须设置为true，默认为false，不会初始化聚合模板，聚合功能会吟唱
                 */
                .useMediation(true)
                .customController(getTTCustomController()) //如果您需要设置隐私策略请参考该api
//                .setMediationConfig(new MediationConfig.Builder() //可设置聚合特有参数详细设置请参考该api
//                        .setMediationConfigUserInfoForSegment(getUserInfoForSegment())//如果您需要配置流量分组信息请参考该api
//                        .build())
                .build();
    }

    private static MediationConfigUserInfoForSegment getUserInfoForSegment(){
        MediationConfigUserInfoForSegment userInfo = new MediationConfigUserInfoForSegment();
        userInfo.setUserId(new DeviceUuidFactory().getDeviceUuid().toString());
        userInfo.setGender(MediationConfigUserInfoForSegment.GENDER_MALE);
//        userInfo.setChannel("msdk-channel");
        userInfo.setSubChannel(HumeSDK.getChannel(MintsApplication.getContext()));
//        userInfo.setAge(999);
//        userInfo.setUserValueGroup("msdk-demo-user-value-group");

        Map<String, String> customInfos = new HashMap<>();
//        customInfos.put("aaaa", "test111");
//        customInfos.put("bbbb", "test222");
        userInfo.setCustomInfos(customInfos);
        return userInfo;
    }

    private static TTCustomController getTTCustomController(){
        return new TTCustomController() {

            @Override
            public boolean isCanUsePhoneState() {
                return false;
            }

            @Override
            public boolean isCanUseWifiState() {
                return false;
            }

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

            @Override
            public boolean isCanUseWriteExternal() {
                return false;
            }

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

            @Override
            public boolean isCanUseAndroidId() {
                return false;
            }

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

            @Override
            public MediationPrivacyConfig getMediationPrivacyConfig() {
                return new MediationPrivacyConfig() {

                    @Override
                    public boolean isCanUseOaid() {
                        return false;
                    }

                    @Override
                    public boolean isLimitPersonalAds() {
                        if (AppPreferencesManager.INSTANCE.get()
                                .getBoolean(Constant.LOAN_PERMISSION_FLAG, true)) return false;

                        return super.isLimitPersonalAds();
                    }

                    @Override
                    public boolean isProgrammaticRecommend() {
                        if (AppPreferencesManager.INSTANCE.get()
                                .getBoolean(Constant.LOAN_PERMISSION_FLAG, true)) return false;

                        return super.isProgrammaticRecommend();
                    }
                };
            }

            @Override
            public boolean isCanUsePermissionRecordAudio() {
                if (AppPreferencesManager.INSTANCE.get()
                        .getBoolean(Constant.LOAN_PERMISSION_FLAG, true)) return false;

                return super.isCanUsePermissionRecordAudio();
            }
        };
    }
}
