package com.tcl.ah.module.serverinfo;

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

import com.tcl.ah.module.serverinfo.data.ServerInfo;
import com.tcl.ah.module.serverinfo.data.ServerInfoIni;

import java.util.Set;

public class MyServerInfoProvider {
    private Context mContext;
    SharedPreferences mSharedPreferences;
    private final String name = "server_info.xml";
    private final int mode = Context.MODE_PRIVATE;

    public MyServerInfoProvider(Context context) {
        mContext = context;
        mSharedPreferences = mContext.getSharedPreferences(name, mode);
    }

    //<editor-fold desc="set and get">
    private boolean set(String key, String val) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key, val);
        return editor.commit();
    }

    private boolean set(String key, boolean val) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean(key, val);
        return editor.commit();
    }

    private boolean set(String key, int val) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key, val);
        return editor.commit();
    }

    private boolean set(String key, float val) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putFloat(key, val);
        return editor.commit();
    }

    private boolean set(String key, long val) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putLong(key, val);
        return editor.commit();
    }

    private boolean set(String key, Set<String> val) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putStringSet(key, val);
        return editor.commit();
    }

    private String get(String key, String def) {
        return mSharedPreferences.getString(key, def);
    }

    private boolean get(String key, boolean def) {
        return mSharedPreferences.getBoolean(key, def);
    }

    private int get(String key, int def) {
        return mSharedPreferences.getInt(key, def);
    }

    private Float get(String key, float def) {
        return mSharedPreferences.getFloat(key, def);
    }

    private long get(String key, long def) {
        return mSharedPreferences.getLong(key, def);
    }

    private Set<String> get(String key, Set<String> def) {
        return mSharedPreferences.getStringSet(key, def);
    }
    //</editor-fold>

    public String getApiHost() {
        return get(ServerInfoIni.Prop.API_HOST, "");
    }

    public boolean setApiHost(String apiHost){
        return  set(ServerInfoIni.Prop.API_HOST,apiHost);
    }

    public String getMqttHost() {
        return get(ServerInfoIni.Prop.MQTT_HOST, "");
    }

    public int getMqttPort() {
        return get(ServerInfoIni.Prop.MQTT_PORT, ServerInfoIni.MQTT_PORT);
    }

    public String getMqttUser() {
        return get(ServerInfoIni.Prop.MQTT_USER, "");
    }

    public String getMqttPassword() {
        return get(ServerInfoIni.Prop.MQTT_PWD, "");
    }

    public int getHeartbeatLive() {
        return get(ServerInfoIni.Prop.HEARTBEAT_LIVE, ServerInfoIni.HEARTBEAT_LIVE);
    }

    public int getExpire() {
        return get(ServerInfoIni.Prop.EXPIRE, ServerInfoIni.EXPIRE);
    }

    public boolean setServerInfo(String aipHost, String mqttHost, int mqttPort, String mqttUser, String mqttPassword, int heartbeatLive, int expire) {
        return mSharedPreferences.edit()
                .putString(ServerInfoIni.Prop.API_HOST, aipHost)
                .putString(ServerInfoIni.Prop.MQTT_HOST, mqttHost)
                .putInt(ServerInfoIni.Prop.MQTT_PORT, mqttPort)
                .putString(ServerInfoIni.Prop.MQTT_USER, mqttUser)
                .putString(ServerInfoIni.Prop.MQTT_PWD, mqttPassword)
                .putInt(ServerInfoIni.Prop.HEARTBEAT_LIVE, heartbeatLive)
                .putInt(ServerInfoIni.Prop.EXPIRE, expire)
                .putLong(ServerInfoIni.Prop.TIMESTAMP, System.currentTimeMillis())
                .commit();
    }

    public ServerInfo getServerInfo(){
        ServerInfo serverInfo = new ServerInfo();
        serverInfo.httpInfo.apiHost=getApiHost();

        serverInfo.mqttInfo.host=getMqttHost();
        serverInfo.mqttInfo.port=getMqttPort();
        serverInfo.mqttInfo.user=getMqttUser();
        serverInfo.mqttInfo.password=getMqttPassword();

        serverInfo.mqttInfo.heartbeatLive=getHeartbeatLive();
        serverInfo.mqttInfo.expire=getExpire();

        return serverInfo;
    }

}
