package ai.pai.client.db;

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

/**
 * Created by chengzhenyu on 2015/8/24.
 */
public class Preferences {

    private Context context;
    private SharedPreferences mPrefs;
    private static Preferences instance;
    private static final String NullStr = "";

    private Preferences(Context context) {
        this.context = context.getApplicationContext();
        mPrefs = this.context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
    }

    public static synchronized Preferences getInstance(Context context) {

        if (instance == null) {
            instance = new Preferences(context);
        }
        return instance;
    }


    public void setLastRunDate(String date) {
        mPrefs.edit().putString("lastRunDate", date).commit();
    }

    public String getLastRunDate() {
        return mPrefs.getString("lastRunDate", "1970-01-01");
    }

    public void setUserName(String userName){
        mPrefs.edit().putString("userName",userName).commit();
    }

    public String getUserName(){
        return mPrefs.getString("userName",NullStr);
    }

    public void setTourGatherLocation(String gatherLocation){
        mPrefs.edit().putString("gatherLocation",gatherLocation).commit();
    }

    public String getTourGatherLocation(){
        return mPrefs.getString("gatherLocation",NullStr);
    }


    public void setTourGuidePhone(String phone){
        mPrefs.edit().putString("tour_phone",phone).commit();
    }

    public String getTourGuidePhone(){
        return mPrefs.getString("tour_phone",NullStr);
    }

    public void setGatherTime(long gatherTime){
        mPrefs.edit().putLong("gatherTime",gatherTime).commit();
    }

    public long getGatherTime(){
        return mPrefs.getLong("gatherTime",0);
    }


    public void setTourEndTime(long tourEndTime){
        mPrefs.edit().putLong("tourEndTime",tourEndTime).commit();
    }

    public long getTourEndTime(){
        return mPrefs.getLong("tourEndTime",0);
    }

    public boolean isTourMode(){
        return mPrefs.getBoolean("isTourMode",false);
    }

    public void setTourMode(boolean isTourMode){
        mPrefs.edit().putBoolean("isTourMode",isTourMode).commit();
    }

    public void setUserPhone(String phone){
        mPrefs.edit().putString("phone",phone).commit();
    }

    public String getUserPhone(){
        return mPrefs.getString("phone",NullStr);
    }

    public void setNickName(String nickName){
        mPrefs.edit().putString("nickName",nickName).commit();
    }

    public String getNickName(){
        return mPrefs.getString("nickName",NullStr);
    }

    public void setUserAvatar(String avatar){
        mPrefs.edit().putString("avatar",avatar).commit();
    }

    public String getUserAvatar(){
        return mPrefs.getString("avatar",NullStr);
    }

    public void setUserId(String userId){
        mPrefs.edit().putString("userId",userId).commit();
    }

    public String getUserId(){
        return mPrefs.getString("userId",NullStr);
    }

    public void setGuestLogin(boolean isGuestLogin){
        mPrefs.edit().putBoolean("isGuestLogin",isGuestLogin).commit();
    }

    public boolean isGuestLogin(){
        return mPrefs.getBoolean("isGuestLogin",false);
    }

    public boolean isPermissionGranted(){
        return mPrefs.getBoolean("isPermissionGranted",true);
    }

    public void setPermissionGranted(boolean isPermissionGranted){
        mPrefs.edit().putBoolean("isPermissionGranted",isPermissionGranted).commit();
    }

    public void clearPrefs(){
        mPrefs.edit().clear().commit();
    }
}
