package com.leanote.android.util;

import android.app.Application;

import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.util.UUID;

/**
 * Created by fish on 9/20/15.
 */
public class AppContext extends Application {
    private static AppContext instance;
    private static String APP_CONFIG_UNIQUE_ID = "app_config_unique_id";
    private static String COMMON_CONFIG = "common_config";

    public String ConfigKey = "";

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        init();
    }

    private void init() {

    }

    public static AppContext getInstance(){
        return instance;
    }

    public PackageInfo getPackageInfo() {
        PackageInfo info = null;
        try {
            info = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace(System.err);
        }

        if (info == null) {
            info = new PackageInfo();
        }

        return info;
    }

    public String getCommonString(String key) {
        SharedPreferences preferences = getSharedPreferences(COMMON_CONFIG, 0);
        return preferences.getString(key, "");
    }

    public void setCommonString(String key, String value) {
        SharedPreferences preferences = getSharedPreferences(COMMON_CONFIG, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public boolean getBoolean(String key) {
        SharedPreferences preferences = getSharedPreferences(ConfigKey, 0);
        return preferences.getBoolean(key, false);
    }

    public void setBoolean(String key, boolean value) {
        SharedPreferences preferences = getSharedPreferences(ConfigKey, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public String getString(String key) {
        SharedPreferences preferences = getSharedPreferences(ConfigKey, 0);
        return preferences.getString(key, "");
    }

    public void setString(String key, String value) {
        SharedPreferences preferences = getSharedPreferences(ConfigKey, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public int getInt(String key) {
        SharedPreferences preferences = getSharedPreferences(ConfigKey, 0);
        return preferences.getInt(key, 0);
    }

    public void setInt(String key, int value) {
        SharedPreferences preferences = getSharedPreferences(ConfigKey, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public float getFloat(String key) {
        SharedPreferences preferences = getSharedPreferences(ConfigKey, 0);
        return preferences.getFloat (key, 0.0f);
    }

    public void setFloat(String key, float value) {
        SharedPreferences preferences = getSharedPreferences(ConfigKey, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putFloat(key, value);
        editor.commit();
    }

    public String getAppId() {
        String uniqueId = getString(APP_CONFIG_UNIQUE_ID);
        if (StringUtils.isNullOrEmpty(uniqueId)) {
            uniqueId = UUID.randomUUID().toString();
            setString(APP_CONFIG_UNIQUE_ID, uniqueId);
        }

        return uniqueId;
    }
}

