/**
 * Copyright (C) 2016 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.redhat.ipaas.rest.v1.handler.integration;

import com.redhat.ipaas.core.Tokens;
import com.redhat.ipaas.dao.manager.DataManager;
import com.redhat.ipaas.model.Kind;
import com.redhat.ipaas.model.integration.Integration;
import com.redhat.ipaas.rest.v1.handler.BaseHandler;
import com.redhat.ipaas.rest.v1.operations.*;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Component;

import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import java.math.BigInteger;
import java.util.Date;
import java.util.Optional;

@Path("/integrations")
@Api(value = "integrations")
@Component
public class IntegrationHandler extends BaseHandler implements Lister<Integration>, Getter<Integration>, Creator<Integration>, Deleter<Integration>, Updater<Integration> {

    public IntegrationHandler(DataManager dataMgr) {
        super(dataMgr);
    }

    @Override
    public Kind resourceKind() {
        return Kind.Integration;
    }

    @PUT
    @Path(value = "/{id}/activate")
    @Consumes("application/json")
    public void activate(@PathParam("id") String id) {
        updateDesiredStatus(id, Integration.Status.Activated);
    }

    @PUT
    @Path(value = "/{id}/deactivate")
    @Consumes("application/json")
    public void deactivate(@PathParam("id") String id) {
        updateDesiredStatus(id, Integration.Status.Deactivated);
    }

    @Override
    public Integration get(String id) {
        Integration integration = Getter.super.get(id);

        //fudging the timesUsed for now
        if (integration.getCurrentStatus().equals(Integration.Status.Activated)) {
            Integration updatedIntegration = integration = new Integration.Builder()
                    .createFrom(integration)
                    .timesUsed(BigInteger.valueOf(new Date().getTime()/1000000))
                    .build();
            return updatedIntegration;
        } else {
            return integration;
        }
    }

    @Override
    public Integration create(Integration integration) {
        Date rightNow = new Date();
        Integration updatedIntegration = new Integration.Builder()
            .createFrom(integration)
            .token(Tokens.getAuthenticationToken())
            .currentStatus(Integration.Status.Draft)
            .desiredStatus(Integration.Status.Activated)
            .statusMessage(Optional.empty())
            .lastUpdated(rightNow)
            .createdDate(rightNow)
            .build();

        return Creator.super.create(updatedIntegration);
    }

    private void updateDesiredStatus(String id, Integration.Status desiredStatus) {
        this.update(id, new Integration.Builder()
            .createFrom(get(id))
            .token(Tokens.getAuthenticationToken())
            .desiredStatus(desiredStatus)
            // Set the current status to 'pending' immediately when
            // a status change is requested.
            // This status will be later changed by the activation handlers.
            // This is not the best place to set but should be done by the IntegrationController
            // However because of how the Controller works (i.e. that any change to the integration
            // within the controller will trigger an event again), the initial status must be set
            // from the outside for the moment.
            .currentStatus(Integration.Status.Pending)
            .build());
    }


    @Override
    public void update(String id, Integration integration) {
        Integration updatedIntegration = new Integration.Builder()
            .createFrom(integration)
            .token(Tokens.getAuthenticationToken())
            .lastUpdated(new Date())
            .build();

        Updater.super.update(id, updatedIntegration);
    }
}

@@ -2707,24 +2707,132 @@
 new Date();%0A
+        Integration.Status desiredStatus = integration.getDesiredStatus().orElse(Integration.Status.Draft);%0A
         Inte
@@ -2992,46 +2992,108 @@
    .
-currentStatus(Integration.Status.Draft
+statusMessage(Optional.empty())%0A            .lastUpdated(rightNow)%0A            .createdDate(rightNow
 )%0A  
@@ -3095,32 +3095,46 @@
 w)%0A            .
+currentStatus(
 desiredStatus(In
@@ -3126,25 +3126,28 @@
 esiredStatus
-(
+ == 
 Integration.
@@ -3162,17 +3162,18 @@
 ctivated
-)
+ ?
 %0A       
@@ -3181,109 +3181,111 @@
     
-.statusMessage(Optional.empty())%0A            .lastUpdated(rightNow)%0A            .createdDate(rightNow
+                   Integration.Status.Pending :%0A                               Integration.Status.Draft
 )%0A  
@@ -3296,33 +3296,32 @@
       .build();%0A
-%0A
         return C

Set proper initial state depending on the requested state