package com.dragonssh.xhttpdemo;

import android.app.Application;
import android.content.SharedPreferences;

import com.dragonssh.xhttpdemo.core.XHttpSshCore;
import com.dragonssh.xhttpdemo.core.config.Settings;

public final class DemoApplication extends Application {
    private static final String BOOTSTRAP_PREFS = "demo_bootstrap";
    private static final String DEFAULTS_CREATED = "defaults_created";

    private volatile String savedPassword = "";
    private Settings settings;

    @Override
    public void onCreate() {
        super.onCreate();
        SharedPreferences prefs = getSharedPreferences(BOOTSTRAP_PREFS, MODE_PRIVATE);
        if (!prefs.getBoolean(DEFAULTS_CREATED, false)) {
            Settings.setDefaultConfig(this);
            prefs.edit().putBoolean(DEFAULTS_CREATED, true).apply();
        }
        settings = new Settings(this);
        savedPassword = settings.getPrivString(Settings.SSH_PASSWORD_KEY);
        XHttpSshCore.init(this);
    }

    String getSessionPassword() {
        return savedPassword;
    }

    void setSessionPassword(String password) {
        savedPassword = password == null ? "" : password;
        settings.getPrefsPrivate().edit()
                .putString(Settings.SSH_PASSWORD_KEY, savedPassword)
                .apply();
    }

    boolean hasSessionPassword() {
        return savedPassword != null && !savedPassword.isEmpty();
    }
}
