package com.duben.happyplaylet.utils;

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

import com.duben.happyplaylet.ad.splash.SplashManager;
import com.duben.happyplaylet.common.AppConfig;
import com.duben.happyplaylet.manager.ActivityPageManager;
import com.duben.happyplaylet.manager.TrackManager;
import com.duben.happyplaylet.mvp.presenters.VersionUpdatePresenter;
import com.duben.happyplaylet.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++;
            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++;
    }

    @Override
    public void onActivityStopped(Activity activity) {
        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);
    }
}