package com.p3.bewoplaner.utils;

import com.p3.bewoplaner.data.AppPrefsConstants;
import com.p3.bewoplaner.data.prefs.BooleanPreference;
import com.p3.bewoplaner.di.InjectionHelper;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

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

/**
 * Service should be started only when "Password Save"
 * is not checked in {@link com.p3.bewoplaner.page.login.LoginActivity};
 */
public class AppStoppedService extends Service {

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

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

    @Inject
    NotificationManager mNotificationManager;

    @Override
    public void onCreate() {
        super.onCreate();
        InjectionHelper.getMainComponent(this).inject(this);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        mAppKilledPreference.set(true);
        if (!mSavedPasswordPref.get()) {
            mNotificationManager.cancelAll();
        }
        stopSelf();
    }
}