package com.openxc.messages;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import android.os.Parcel;

import com.google.common.base.Objects;
import com.google.gson.annotations.SerializedName;

import com.openxc.messages.Command.CommandType;

public class CommandResponse extends KeyedMessage {

    public static final String COMMAND_RESPONSE_KEY = "command_response";
    public static final String MESSAGE_KEY = "message";

    public static final String[] sRequiredFieldsValues = new String[] {
            COMMAND_RESPONSE_KEY };
    public static final Set<String> sRequiredFields = new HashSet<String>(
            Arrays.asList(sRequiredFieldsValues));

    @SerializedName(COMMAND_RESPONSE_KEY)
    private CommandType mCommand;

    // Message is optional
    @SerializedName(MESSAGE_KEY)
    private String mMessage;

    public CommandResponse(CommandType command, String message) {
        mMessage = message;
        mCommand = command;
    }

    public CommandResponse(CommandType command) {
        this(command, null);
    }

    public boolean hasMessage() {
        return mMessage != null;
    }

    public String getMessage() {
        return mMessage;
    }

    public CommandType getCommand() {
        return mCommand;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null || !super.equals(obj)) {
            return false;
        }

        final CommandResponse other = (CommandResponse) obj;
        return Objects.equal(mCommand, other.mCommand) &&
                Objects.equal(mMessage, other.mMessage);
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
            .add("timestamp", getTimestamp())
            .add("command", getCommand())
            .add("message", getMessage())
            .add("extras", getExtras())
            .toString();
    }

    @Override
    public MessageKey getKey() {
        HashMap<String, Object> key = new HashMap<>();
        key.put(COMMAND_RESPONSE_KEY, getCommand());
        return new MessageKey(key);
    }

    public static boolean containsRequiredFields(Set<String> fields) {
        return fields.containsAll(sRequiredFields);
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
        out.writeSerializable(getCommand());
        out.writeString(getMessage());
    }

    @Override
    protected void readFromParcel(Parcel in) {
        super.readFromParcel(in);
        mCommand = (CommandType) in.readSerializable();
        mMessage = in.readString();
    }

    protected CommandResponse(Parcel in)
            throws UnrecognizedMessageTypeException {
        readFromParcel(in);
    }

    protected CommandResponse() { }
}

@@ -2032,31 +2032,30 @@
 ey.put(C
-OMMAND_RESPONSE
+ommand.COMMAND
 _KEY, ge

Make command keys match command responses. Keys for commands and command responses only match if they have the same pair, and the key of a CommandResponse has in its pair a key of 'command_response' instead of 'command' before this fix.