package org.ow2.proactive.procci.model.occi.infrastructure.mixin;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.ow2.proactive.procci.model.cloud.automation.Model;
import org.ow2.proactive.procci.model.exception.ClientException;
import org.ow2.proactive.procci.model.exception.MissingAttributesException;
import org.ow2.proactive.procci.model.occi.infrastructure.constants.Attributes;
import org.ow2.proactive.procci.model.occi.infrastructure.constants.Identifiers;
import org.ow2.proactive.procci.model.occi.infrastructure.constants.InfrastructureKinds;
import org.ow2.proactive.procci.model.occi.metamodel.Action;
import org.ow2.proactive.procci.model.occi.metamodel.Attribute;
import org.ow2.proactive.procci.model.occi.metamodel.Entity;
import org.ow2.proactive.procci.model.occi.metamodel.Kind;
import org.ow2.proactive.procci.model.occi.metamodel.Mixin;
import org.ow2.proactive.procci.model.occi.metamodel.MixinBuilder;

/**
 * Created by the Activeeon Team on 2/25/16.
 */

/**
 * Indicated the data that will be supplied to the compute
 */
public class Contextualization extends Mixin {

    private String userdata;


    /**
     * Constructor with all parameters
     *
     * @param userdata Contextualization data(e.g., script executable) that the client supplies once and only once. It cannot be updated
     * @param entities is the set of resource instances
     */
    public Contextualization(String title, List<Mixin> depends, List<Entity> entities, String userdata) {
        super(Identifiers.COMPUTE_SCHEME, Identifiers.CONTEXTUALIZATION, title,
                createAttributeSet(), new ArrayList<Action>(), depends, setApplies(),
                entities);
        this.userdata = userdata;
    }

    private static List<Kind> setApplies() {
        List<Kind> applies = new ArrayList<>();
        applies.add(InfrastructureKinds.COMPUTE);
        return applies;
    }

    private static Set<Attribute> createAttributeSet() {
        Set<Attribute> attributes = new HashSet<>();
        attributes.add(Attributes.USERDATA);
        return attributes;
    }

    public String getUserdata() {
        return userdata;
    }


    @Override
    public Model.Builder toCloudAutomationModel(Model.Builder cloudAutomation) {
        cloudAutomation.addVariable(Attributes.USERDATA_NAME, this.userdata);
        return cloudAutomation;
    }

    public static class Builder extends MixinBuilder {

        private String userdata;

        public Builder() {
            super(Identifiers.COMPUTE_SCHEME, Identifiers.CONTEXTUALIZATION);
        }

        @Override
        public Contextualization.Builder attributes(Map attributesMap) throws ClientException {
            super.attributes(attributesMap);
            this.userdata = Optional.ofNullable(
                            convertAttributeInString(attributesMap.get(Attributes.USERDATA_NAME)))
                            .orElseThrow(() -> new MissingAttributesException(Attributes.USERDATA_NAME,
                                    Attributes.USERDATA.getName()));
            return this;
        }

        @Override
        public Contextualization build(){
            return new Contextualization(this.getTitle(), this.getDepends(), this.getEntities(),this.userdata);
        }

    }
}

@@ -1235,16 +1235,18 @@
 data;%0A%0A%0A
+%0A%0A
     /**%0A
@@ -1257,45 +1257,215 @@
  * C
-onstructor with all parameters%0A     *
+reate a Contextualization mixin%0A     *%0A     * @param title is the mixin name%0A     * @param depends is a list of mixin related to this instance%0A     * @param entities entities is the set of resource instances
 %0A   
@@ -1484,16 +1484,25 @@
 serdata 
+userdata 
 Contextu
@@ -1611,64 +1611,8 @@
 ted%0A
-     * @param entities is the set of resource instances%0A
     

update the Contextualization mixin documentation