package elements.messages;

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

/**
 * Created by Guillermo Serrahima on 10/8/16.
 */
public class GroupMessage extends Message {
    //Event ID constants
    private static final int USER_JOINED = 1;
    private static final int USER_LEFT = 2;
    private static final int CHAT_NAME_CHANGED = 3;

    private Long cid;         //chat id
    private Integer mid;      //message id
    private Integer uid;      //user id
    private String chatType;  //chat type
    private String fullName;  //author name
    private String userName;  //username
    private String chatName;  //chat name
    private String text;      //the actual message
    private String consoleLog;
    private Integer event;

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

        fullName = message.getJSONObject("from").getString("first_name");
        uid = message.getJSONObject("from").getInt("id");
        event = 0;
        text = null;

        //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 = null;

        if (message.has("text"))
            text = message.getString("text");
        else {
            if (message.has("new_chat_participant")) {
                event = USER_JOINED;
                if(message.getJSONObject("new_chat_participant").has("username"))
                    text = "@" + message.getJSONObject("new_chat_participant").getString("username");
                else text = message.getJSONObject("new_chat_participant").getString("first_name");
            }

            else if (message.has("left_chat_participant")) {
                event = USER_LEFT;
                if(message.getJSONObject("left_chat_participant").has("username"))
                    text = "@" + message.getJSONObject("left_chat_participant").getString("username");
                else text = message.getJSONObject("left_chat_participant").getString("first_name");
            }
            else if(message.has("new_chat_title")) {
                event = CHAT_NAME_CHANGED;
                text = message.getString("new_chat_title");
            }
        }

        consoleLog = message.toString();
    }

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

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

    public String getAuthor() {
        return fullName;
    }

    public boolean hasUsername() {
        return userName != null;
    }

    public String getUsername() throws NoUsernameMessageException {
        if(userName == null) throw new NoUsernameMessageException();
        return userName;
    }

    public boolean hasText() {
        return event != 0 || text != null;
    }

    public String getText() throws NoTextMessageException {
        if(text == null) throw new NoTextMessageException();
        return text;
    }

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

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

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

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

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

    public boolean isEvent() {
        System.out.println("Event is " + event);
        try {
            return event.intValue() != 0;
        } catch (NullPointerException e) {
            return false;
        }
    }

    public boolean userLeft() {
        return event == USER_LEFT;
    }

    public boolean userJoined() {
        return event == USER_JOINED;
    }

    public boolean chatNameChanged() { return event == CHAT_NAME_CHANGED; }
}
