package de.beyondsoft.ownchat.utils;

import de.beyondsoft.ownchat.data.AppPrefsConstants;
import de.beyondsoft.ownchat.data.prefs.BooleanPreference;
import de.beyondsoft.ownchat.di.scopes.ActivityScope;
import de.beyondsoft.ownchat.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;
    }
}