package it.integry.integrywmsnative;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.text.SpannableString;

import com.crashlytics.android.Crashlytics;
import com.google.firebase.perf.FirebasePerformance;
import com.google.firebase.perf.metrics.Trace;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;

import io.fabric.sdk.android.Fabric;
import it.integry.integrywmsnative.core.REST.watcher.ServerStatusChecker;
import it.integry.integrywmsnative.core.barcode_reader.BarcodeManager;
import it.integry.integrywmsnative.core.exception.ExceptionsHandler;
import it.integry.integrywmsnative.core.settings.SettingsManager;
import it.integry.integrywmsnative.core.settings.Stash;
import it.integry.integrywmsnative.core.update.UpdatesManager;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.core.utility.UtilityResources;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.view.dialogs.DialogSimpleMessageHelper;
import it.integry.integrywmsnative.view.dialogs.exception.DialogException;

public class MainApplication extends Application {

    public static Resources res;
    public static Context Context;


    // Called when the application is starting, before any other application objects have been created.
    // Overriding this method is totally optional!
    @Override
    public void onCreate() {
        super.onCreate();

        if(!BuildConfig.DEBUG) {
            Fabric.with(this, new Crashlytics());

            if(SettingsManager.i() != null &&
                    (SettingsManager.i().userSession != null && !UtilityString.isNullOrEmpty(SettingsManager.i().userSession.profileDB)) &&
                    (SettingsManager.i().user != null && !UtilityString.isNullOrEmpty(SettingsManager.i().user.fullname))) {

                Crashlytics.setString("user_fullname", SettingsManager.i().user.fullname);
                Crashlytics.setString("profile_db", SettingsManager.i().userSession.profileDB);
            }
        } else {
            if(SettingsManager.i() != null &&
                    (SettingsManager.i().userSession != null && !UtilityString.isNullOrEmpty(SettingsManager.i().userSession.profileDB)) &&
                    (SettingsManager.i().user != null && !UtilityString.isNullOrEmpty(SettingsManager.i().user.fullname))) {

                Crashlytics.setString("user_fullname", SettingsManager.i().user.fullname);
                Crashlytics.setString("profile_db", "[DEBUG] " + SettingsManager.i().userSession.profileDB);
            }
        }

        Stash.init(this);

        SettingsManager.init(this);
        ServerStatusChecker.init();
        BarcodeManager.init(this);

//        UtilityExceptions.init(this);
        UtilityResources.init(this);

        Logger.addLogAdapter(new AndroidLogAdapter());

        res = getResources();
        Context = this;
    }

    // Called by the system when the device configuration changes while your component is running.
    // Overriding this method is totally optional!
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    // This is called when the overall system is running low on memory,
    // and would like actively running processes to tighten their belts.
    // Overriding this method is totally optional!
    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }
}
