package com.rthoni.camotion.dbo;

import com.luticate.auth.dbo.LuLoginDbo;
import com.luticate.utils.dbo.LuDataAccessConfigDbo;
import com.luticate.utils.dbo.LuDbo;

import org.json.JSONException;
import org.json.JSONObject;

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

/**
 * Created by robin on 11/28/15.
 */
public class LocationDbo extends LuDbo {

    private String _name;

    private String _domain;

    private String _username;

    private String _password;

    private boolean _https;

    public String getName() {
        return _name;
    }

    public void setName(String name) {
        _name = name;
    }

    public String getDomain() {
        return _domain;
    }

    public void setDomain(String domain) {
        _domain = domain;
    }

    public String getUsername() {
        return _username;
    }

    public void setUsername(String username) {
        _username = username;
    }

    public String getPassword() {
        return _password;
    }

    public void setPassword(String password) {
        _password = password;
    }

    public boolean isHttps() {
        return _https;
    }

    public void setHttps(boolean https) {
        _https = https;
    }

    public String getBaseUrl()
    {
        return (_https ? "https" : "http") + "://" + _domain;
    }

    @Override
    public void fromJson(JSONObject json) throws JSONException {
        _name = json.getString("Name");
        _domain = json.getString("Domain");
        _username = json.getString("Username");
        _password = json.getString("Password");
        _https = json.getBoolean("Https");
    }

    @Override
    public HashMap<String, Object> toArray() {
        HashMap<String, Object> array = new HashMap<>();
        array.put("Name", _name);
        array.put("Domain", _domain);
        array.put("Username", _username);
        array.put("Password", _password);
        array.put("Https", _https);
        return array;
    }

    public LuDataAccessConfigDbo getConfig(final LuLoginDbo loginDbo)
    {
        LuDataAccessConfigDbo config = new LuDataAccessConfigDbo()
        {
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> map = super.getHeaders();
                if (loginDbo != null) {
                    map.put("X-Authorization", loginDbo.getToken());
                }
                return map;
            }
        };
        config.setBaseUrl(getBaseUrl());
        return config;
    }

    public String getPseudoMail()
    {
        return getUsername() + "@" + getName();
    }
}
