package com.sipai.jspatrol.utils;

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

import java.util.HashMap;
import java.util.Map;

/**
 * created by lvliming on 2019/9/17
 */
public class SharedPreferencesData {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private String ip_in_plant = "", ip_off_site = "";//厂内，厂外ip地址
    private String mServer = "";
    private String mServerIp = "";
    private String mUsername;
    private String mPassword;
    private String mUserid;
    private String mCaption;
    private String mTotaltime;
    private String mLastlogintime;


    public SharedPreferencesData(Context context){
        sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    public void removePwd(){
        editor.putString("password","");
        editor.commit();
    }

    public void saveInfo(Map<String, String> map){
        if (map!=null){
            for (String key:map.keySet()){
                editor.putString(key,map.get(key));
            }
            editor.commit();
        }
    }

    public HashMap<String, String> getAll(){
        if (sharedPreferences.getString("ip_in_plant", "")!=null) {
            ip_in_plant = sharedPreferences.getString("ip_in_plant", "");
        }
        if (sharedPreferences.getString("ip_off_site", "")!=null) {
            ip_off_site=sharedPreferences.getString("ip_off_site", "");
        }
        if (!sharedPreferences.getString("server", "").equals("")) {
            mServer = sharedPreferences.getString("server", "");
        } else {
            mServer = "";
        }
        if (!sharedPreferences.getString("serverip", "").equals("")) {
            mServerIp = sharedPreferences.getString("serverip", "");
        } else {
            mServerIp = "";
        }
        mUsername=sharedPreferences.getString("username", "");
        mPassword=sharedPreferences.getString("password", "");
        mUserid=sharedPreferences.getString("userid", "");
        mCaption=sharedPreferences.getString("caption", "");
        mTotaltime=sharedPreferences.getString("totaltime", "");
        mLastlogintime=sharedPreferences.getString("lastlogintime", "");
        HashMap<String, String> map = new HashMap<>();
        map.put("ip_in_plant",ip_in_plant);
        map.put("ip_off_site",ip_off_site);
        map.put("mServer",mServer);
        map.put("mServerIp",mServerIp);
        map.put("mUsername",mUsername);
        map.put("mPassword",mPassword);
        map.put("mUserid",mUserid);
        map.put("mCaption",mCaption);
        map.put("mTotaltime",mTotaltime);
        map.put("mLastlogintime",mLastlogintime);
        return map;
    }


}
