package com.duben.microtribe.utils;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.duben.microtribe.ad.splash.SplashManager;
import com.duben.microtribe.common.AppConfig;
import com.duben.microtribe.manager.ActivityPageManager;
import com.duben.microtribe.manager.TrackManager;
import com.duben.microtribe.mvp.presenters.VersionUpdatePresenter;
import com.duben.microtribe.ui.activitys.SplashForeAdActivity;

import java.lang.ref.WeakReference;


/**
 * 判断app在前台还是在后台
 */
public class ForegroundOrBackground implements Application.ActivityLifecycleCallbacks {
    private static ForegroundOrBackground instance;
    private int count = 0;
    private long activityStoppedTime = 0;

    //获取当前最上层的activity
    private static WeakReference<Activity> app_activity;
    private VersionUpdatePresenter vup;

    public static ForegroundOrBackground init(Application application) {
        if (instance == null) {
            instance = new ForegroundOrBackground();
            application.registerActivityLifecycleCallbacks(instance);
        }
        return instance;
    }

    public static Activity getTopActivity() {
        return app_activity.get();
    }

    @Override
    public void onActivityResumed(Activity activity) {
        if (activity == null)
            return;

        String simpleName = activity.getClass().getSimpleName();
        if (TextUtils.equals(simpleName, "MainActivity")) {
            // 版本更新
            if (vup == null) {
                vup = new VersionUpdatePresenter();
            }
            if (app_activity != null) {
                vup.updateVersion(app_activity.get());
            }
        }
    }

    @Override
    public void onActivityPaused(Activity activity) {
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        app_activity = new WeakReference<>(activity);
        ActivityPageManager.getInstance().addActivity(activity);
    }

    @Override
    public void onActivityStarted(Activity activity) {
        if (activity == null)
            return;

        String simpleName = activity.getClass().getSimpleName();
        if (TextUtils.equals(simpleName, "SplashActivity") ||
                TextUtils.equals(simpleName, "SplashAdActivity") ||
                TextUtils.equals(simpleName, "SplashForeAdActivity") ||
                TextUtils.equals(simpleName, "VipActivity")) {
            count++;
            System.out.println("mcg __>>>>>>>:: onActivityStarted simpleName="+simpleName+"  进入 count=" + count);

            return;
        }

        if (count == 0) {
            long currentTimeMillis = System.currentTimeMillis();
            long diff = currentTimeMillis - activityStoppedTime;
            long diffInSeconds = diff / 1000;
            System.out.println("mcg -->>>>>::  diffInSeconds=" + diffInSeconds + "      adSuccessStatus=" + SplashManager.INSTANCE.isSuccessStatus());
            if (diffInSeconds >= 10 && SplashManager.INSTANCE.isSuccessStatus()) {
                activity.startActivity(new Intent(activity, SplashForeAdActivity.class));
            }
        }
        count++;

        System.out.println("mcg __>>>>>>>:: onActivityStarted simpleName="+simpleName+"  进入 count=" + count);
    }

    @Override
    public void onActivityStopped(Activity activity) {
        String simpleName = activity.getClass().getSimpleName();

        count--;
        System.out.println("mcg __>>>>>>>:: onActivityStopped simpleName="+simpleName+"  关闭 count=" + count);
        if (count < 0) {
            count = 0;
        }
        if (count == 0) {
            System.out.println("mcg -->>>>>::  onActivityStopped");
            activityStoppedTime = System.currentTimeMillis();
            SplashManager.INSTANCE.preLoadAd(activity);
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        try {
            String simpleName = activity.getClass().getSimpleName();
            if (TextUtils.equals(simpleName, "MainActivity")) {
                if (vup != null) {
                    vup.dispose();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        ActivityPageManager.getInstance().removeActivity(activity);
    }
}