package kz.arta.synergy.workflow.mobile.android.common.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Build;
import android.os.StrictMode;
import android.support.multidex.MultiDexApplication;

import java.lang.reflect.Method;
import java.util.Locale;

import kz.arta.synergy.workflow.mobile.android.common.SettingsConst;

/**
 * Created by neox on 5/9/14.
 */
public class MyApplication extends MultiDexApplication {

    private static String lang;
    private Activity mCurrentActivity = null;

    public static String getLang() {
        if (lang != null) {
            return lang;
        } else {
            return "ru";
        }
    }

    public static void setLang(String lang) {
        MyApplication.lang = lang;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        getLocale();
        if (Build.VERSION.SDK_INT >= 24) {
            try {
                Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
                m.invoke(null);
            } catch (Exception ignored) {}
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        getLocale();
    }

    private void getLocale() {
        SharedPreferences preferences = getSharedPreferences(SettingsConst.DEFAULT_NAME_SETTINGS, Context.MODE_PRIVATE);
        lang = preferences.getString("lang_settings", "ru");
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, null);
    }

    public Activity getCurrentActivity() {
        return mCurrentActivity;
    }

    public void setCurrentActivity(Activity mCurrentActivity) {
        this.mCurrentActivity = mCurrentActivity;
    }
}