/*
 * Copyright © 2010 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.redhat.rhevm.api.mock.resource;

import java.util.concurrent.Executor;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import com.redhat.rhevm.api.model.Action;
import com.redhat.rhevm.api.model.ActionsBuilder;
import com.redhat.rhevm.api.model.Host;
import com.redhat.rhevm.api.resource.HostResource;
import com.redhat.rhevm.api.common.util.JAXBHelper;


public class MockHostResource extends AbstractMockResource<Host> implements HostResource {
    /* FIXME: would like to do:
     * private @Context UriInfo uriInfo;
     */

    /**
     * Package-protected ctor, never needs to be instantiated by JAX-RS framework.
     *
     * @param host     encapsulated host
     * @param executor executor used for asynchronous actions
     */
    MockHostResource(String id, Executor executor) {
        super(id, executor);
    }

    // FIXME: this needs to be atomic
    public void updateModel(Host host) {
        // update writable fields only
        getModel().setName(host.getName());
    }

    public Host addLinks(UriBuilder uriBuilder) {
        Host host = JAXBHelper.clone(OBJECT_FACTORY.createHost(getModel()));

        host.setHref(uriBuilder.build().toString());

        ActionsBuilder actionsBuilder = new ActionsBuilder(uriBuilder, HostResource.class);
        host.setActions(actionsBuilder.build());

        return host;
    }

    /* FIXME: kill uriInfo param, make href auto-generated? */
    @Override
    public Host get(UriInfo uriInfo) {
        return addLinks(uriInfo.getRequestUriBuilder());
    }

    @Override
    public Host update(HttpHeaders headers, UriInfo uriInfo, Host host) {
        validateUpdate(host, headers);
        updateModel(host);
        return addLinks(uriInfo.getRequestUriBuilder());
    }

    @Override
    public Response approve(UriInfo uriInfo, Action action) {
        return doAction(uriInfo, new DoNothingTask(action));
    }

    @Override
    public Response fence(UriInfo uriInfo, Action action) {
        return doAction(uriInfo, new DoNothingTask(action));
    }

    @Override
    public Response activate(UriInfo uriInfo, Action action) {
        return doAction(uriInfo, new DoNothingTask(action));
    }

    @Override
    public Response deactivate(UriInfo uriInfo, Action action) {
        return doAction(uriInfo, new DoNothingTask(action));
    }
}

@@ -1167,16 +1167,62 @@
 l.Host;%0A
+import com.redhat.rhevm.api.model.HostStatus;%0A
 import c
@@ -3108,36 +3108,54 @@
 fo, new 
-DoNothingTask(action
+HostStatusSetter(action, HostStatus.UP
 ));%0A    
@@ -3273,39 +3273,406 @@
 fo, new 
-DoNothingTask(action));
+HostStatusSetter(action, HostStatus.MAINTENANCE));%0A    %7D%0A%0A    private class HostStatusSetter extends AbstractActionTask %7B%0A        private HostStatus status;%0A        HostStatusSetter(Action action, HostStatus status) %7B%0A            super(action);%0A            this.status = status;%0A        %7D%0A        public void run() %7B%0A            MockHostResource.this.getModel().setStatus(status);%0A        %7D
 %0A    %7D%0A%7D

Add host status to mock impl