package com.kfzs.duanduan.react;

import android.content.Context;
import android.content.SharedPreferences.Editor;

import com.kfzs.duanduan.KFZSApp;

/**
 * Created by "蔡刚" on 2017/4/20.
 */
public class SharedPreferences {
    private static final String SP_NAME = "duanduan";
    public static final String KEY_REACT_ROOT_PAGE_TYPE = "react_page_type";
    public static final String REACT_ROOT_PAGE_TYPE_MAIN = "react_root_page_type_main";
    public static final String REACT_ROOT_PAGE_TYPE_SECOND = "react_root_page_type_second";
    public static final String KEY_REACT_SECOND_PAGE_TYPE = "react_second_page_type";
    public static final String KEY_REACT_SECOND_PAGE_BUNDLE = "key_react_second_page_bundle";
    public static final String KEY_REACT_MY_WALLET_BUNDLE = "key_react_my_wallet_bundle";
    public static final String KEY_REACT_MY_ORDERS_BUNDLE = "key_react_my_orders_bundle";
    public static final String KEY_REACT_MY_GAMES_BUNDLE = "key_react_my_games_bundle";
    public static final String KEY_REACT_SELLER_CENTERS_BUNDLE = "key_react_seller_centers_bundle";
    public static final String KEY_HSA_NEW_BOOK = "has_new_book";
    public static final String KEY_IS_FIRST_RUN_APP = "is_first_run_app";
    public static final String KEY_UPDATE_CONTACT_TIME = "update_contact_time";

    private static SharedPreferences instance = new SharedPreferences();
    public SharedPreferences() {
    }
    private static synchronized void syncInit() {
        if (instance == null) {
            instance = new SharedPreferences();
        }
    }
    public static SharedPreferences getInstance() {
        if (instance == null) {
            syncInit();
        }
        return instance;
    }
    private android.content.SharedPreferences getSp() {
        return KFZSApp.getInstance().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    }
    public int getInt(String key, int def) {
        try {
            android.content.SharedPreferences sp = getSp();
            if (sp != null)
                def = sp.getInt(key, def);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return def;
    }
    public void putInt(String key, int val) {
        try {
            android.content.SharedPreferences sp = getSp();
            if (sp != null) {
                Editor e = sp.edit();
                e.putInt(key, val);
                e.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public long getLong(String key, long def) {
        try {
            android.content.SharedPreferences sp = getSp();
            if (sp != null)
                def = sp.getLong(key, def);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return def;
    }
    public void putLong(String key, long val) {
        try {
            android.content.SharedPreferences sp = getSp();
            if (sp != null) {
                Editor e = sp.edit();
                e.putLong(key, val);
                e.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public String getString(String key, String def) {
        try {
            android.content.SharedPreferences sp = getSp();
            if (sp != null)
                def = sp.getString(key, def);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return def;
    }
    public void putString(String key, String val) {
        try {
            android.content.SharedPreferences sp = getSp();
            if (sp != null) {
                Editor e = sp.edit();
                e.putString(key, val);
                e.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public boolean getBoolean(String key, boolean def) {
        try {
            android.content.SharedPreferences sp = getSp();
            if (sp != null)
                def = sp.getBoolean(key, def);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return def;
    }
    public void putBoolean(String key, boolean val) {
        try {
            android.content.SharedPreferences sp = getSp();
            if (sp != null) {
                Editor e = sp.edit();
                e.putBoolean(key, val);
                e.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void remove(String key) {
        try {
            android.content.SharedPreferences sp = getSp();
            if (sp != null) {
                Editor e = sp.edit();
                e.remove(key);
                e.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


