package com.nashlincoln.blink.content;

import com.nashlincoln.blink.model.Attribute;
import com.nashlincoln.blink.model.AttributeType;
import com.nashlincoln.blink.model.Device;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by nash on 10/18/14.
 */
public class Command {
    private static final String ADD = "add";
    private static final String REMOVE = "remove";
    private static final String UPDATE = "update";
    private static final String SET_NAME = "set-name";
    public long id;
    public String action;
    public String name;
    public String type;
    public List<Update> updates;
    public transient Device device;

    public static Command add(Device device) {
        Command command = new Command();
        command.device = device;
        command.action = ADD;
        command.type = device.getInterconnect().toLowerCase();
        return command;
    }

    public static Command remove(Device device) {
        Command command = new Command();
        command.device = device;
        command.action = REMOVE;
        command.id = device.getId();
        return command;
    }

    public static Command update(Device device) {
        Command command = new Command();
        command.device = device;
        command.action = UPDATE;
        command.id = device.getId();
        command.updates = new ArrayList<>();
        for (Attribute attribute : device.getAttributes()) {
            if (attribute.isChanged()
                    && (attribute.getAttributeTypeId() == AttributeType.ATTRIBUTE_On_Off || device.isOn())) {
                command.updates.add(new Update(attribute));
            }
        }
        return command;
    }

    public static Command setName(Device device) {
        Command command = new Command();
        command.device = device;
        command.action = SET_NAME;
        command.id = device.getId();
        command.name = device.getName();
        return command;
    }

    public static class Update {
        public long id;
        public String value;

        public Update(Attribute attribute) {
            id = attribute.getAttributeTypeId();
            value = attribute.getValueLocal();
        }
    }
}

@@ -1465,115 +1465,8 @@
 ed()
-%0A                    && (attribute.getAttributeTypeId() == AttributeType.ATTRIBUTE_On_Off %7C%7C device.isOn())
 ) %7B%0A

remove buggy optimization