package com.p3.bewoplaner.utils;

import com.p3.bewoplaner.data.AppPrefsConstants;
import com.p3.bewoplaner.data.prefs.BooleanPreference;
import com.p3.bewoplaner.di.scopes.ActivityScope;
import com.p3.bewoplaner.page.login.LoginPresenter;

import android.app.NotificationManager;
import android.content.Context;
import android.provider.Settings;

import javax.inject.Inject;
import javax.inject.Named;

@ActivityScope
public class LoggedOutUtils {

    @Inject
    @Named(AppPrefsConstants.APP_KILLED)
    BooleanPreference mAppKilledPreference;

    @Inject
    @Named(AppPrefsConstants.SAVE_PASSWORD)
    BooleanPreference mSavePasswordPreference;

    @Inject
    NotificationManager mNotificationManager;

    @Inject
    LoginPresenter mLoginPresenter;

    @Inject
    Context mContext;

    @Inject
    LoggedOutUtils() {
    }

    /**
     * Check if the user is still logged in.
     */
    public boolean checkLoggedOutPrefs() {
        if (!mSavePasswordPreference.get() && mAppKilledPreference.get()) {
            mNotificationManager.cancelAll();
            mLoginPresenter.logout(Settings.Secure.getString(mContext.getContentResolver(),
                    Settings.Secure.ANDROID_ID), true, null);
            return true;
        }
        return false;
    }
}