package elements.messages;

import elements.exceptions.NoChatNameMessageException;
import elements.exceptions.NoUsernameMessageException;
import org.json.JSONObject;

/**
 * Created by Guillermo Serrahima on 10/9/16.
 */
public class PrivateMessage extends Message {

    public PrivateMessage(JSONObject message) {
        super(message);

        fullName = message.getJSONObject("from").getString("first_name");
        uid = message.getJSONObject("from").getInt("id");

        //Check for user last name
        if (message.getJSONObject("from").has("last_name"))
            fullName += " " + message.getJSONObject("from").getString("last_name");

        //Check for user username
        if (message.getJSONObject("from").has("username"))
            userName = message.getJSONObject("from").getString("username");
        else userName = null;

        //Get group chat name
        if (message.getJSONObject("chat").has("title"))
            chatName = message.getJSONObject("chat").getString("title");
        else chatName = message.getJSONObject("from").getString("first_name");

        if (message.has("text"))
            text = message.getString("text");
        else text = null;

        consoleLog = message.toString();
    }

    @Override
    public boolean hasAuthor() {
        return true;
    }

    @Override
    public int getUid() {
        return uid;
    }

    @Override
    public String getAuthor() {
        return fullName;
    }

    @Override
    public String getUsername() throws NoUsernameMessageException {
        if(!hasUsername()) throw new NoUsernameMessageException();
        return userName;
    }

    @Override
    public boolean isBasicGroup() {
        return false;
    }

    @Override
    public boolean isSupergroup() {
        return false;
    }

    @Override
    public boolean isChannel() {
        return false;
    }

    @Override
    public boolean isPrivate() {
        return true;
    }

    @Override
    public String getChatName() {
        return chatName;
    }
}
