package com.mints.goodmoney.utils;

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

import com.mints.goodmoney.manager.TrackManager;
import com.mints.goodmoney.mvp.presenters.VersionUpdatePresenter;
import com.mints.goodmoney.ui.activitys.SplashADActivity;

import java.lang.ref.WeakReference;


/**
 * 判断app在前台还是在后台
 */
public class ForegroundOrBackground implements Application.ActivityLifecycleCallbacks {

    private long leaveTime = 0;//应用切后台时间

    private static ForegroundOrBackground instance;
    public int count = 0;

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

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

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

    @Override
    public void onActivityResumed(Activity activity) {
    }

    @Override
    public void onActivityPaused(Activity activity) {
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        if (activity == null)
            return;
        String simpleName = activity.getClass().getSimpleName();
        if (TextUtils.equals(simpleName, "MainActivity")) {
            // 版本更新
            if (vup == null) {
                vup = new VersionUpdatePresenter();
            }
            if (reference == null) {
                reference = new WeakReference<>(activity);
            }
            vup.updateVersion(reference.get());
        }
    }

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

        // 每次返回应用时 都展示开屏广告
        String simpleName = activity.getClass().getSimpleName();
        if (TextUtils.equals(simpleName, "SplashYlhActivity") ||
                TextUtils.equals(simpleName, "GuideActivity") ||
                TextUtils.equals(simpleName, "AwardActivity") ||
                TextUtils.equals(simpleName, "DrawcashActivity")) {
            count++;
            return;
        }

        if (count == 0) {
//            LogUtil.d("ForegroundOrBackground", System.currentTimeMillis() - leaveTime + "   onActivityStarted");
            // 30秒后打开应用 显示开屏广告
            if (System.currentTimeMillis() - leaveTime >= 30000) {
                if (intent == null) {
                    intent = new Intent(activity, SplashADActivity.class);
                }
                activity.startActivity(intent);
            }
        }
        count++;
    }

    @Override
    public void onActivityStopped(Activity activity) {
        count--;

        if (count == 0) {
            leaveTime = System.currentTimeMillis();
            String simpleName = activity.getClass().getSimpleName();
            if (TextUtils.equals(simpleName, "SplashYlhActivity") ||
                    TextUtils.equals(simpleName, "GuideActivity") ||
                    TextUtils.equals(simpleName, "AwardActivity") ||
                    TextUtils.equals(simpleName, "DrawcashActivity")) {
                count++;
                return;
            }

            TrackManager.getInstance().offline();
        }
    }

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

    }

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

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