package org.cloudfoundry.community.servicebroker.model;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;


/**
 * An instance of a ServiceDefinition.
 * 
 * @author sgreenberg@gopivotal.com
 *
 */
@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE)
public class ServiceInstance {

	@JsonSerialize
	@JsonProperty("service_instance_id")
	private String serviceInstanceId;
	
	@JsonSerialize
	@JsonProperty("service_id")
	private String serviceDefinitionId;
	
	@JsonSerialize
	@JsonProperty("plan_id")
	private String planId;
	
	@JsonSerialize
	@JsonProperty("organization_guid")
	private String organizationGuid;
	
	@JsonSerialize
	@JsonProperty("space_guid")
	private String spaceGuid;
	
	@JsonSerialize
	@JsonProperty("dashboard_url")
	private String dashboardUrl;

	@JsonSerialize
	@JsonProperty("last_operation")
	private ServiceInstanceLastOperation lastOperation; 
	
	@JsonIgnore
	private boolean async;
	
	@SuppressWarnings("unused")
	private ServiceInstance() {}
	
	/**
	 * Create a ServiceInstance from a create request. If fields 
	 * are not present in the request they will remain null in the  
	 * ServiceInstance.
	 * @param request containing details of ServiceInstance
	 */
	public ServiceInstance(CreateServiceInstanceRequest request) {
		this.serviceDefinitionId = request.getServiceDefinitionId();
		this.planId = request.getPlanId();
		this.organizationGuid = request.getOrganizationGuid();
		this.spaceGuid = request.getSpaceGuid();
		this.serviceInstanceId = request.getServiceInstanceId();
	}
	
	/**
	 * Create a ServiceInstance from a delete request. If fields 
	 * are not present in the request they will remain null in the 
	 * ServiceInstance.
	 * @param request containing details of ServiceInstance
	 */
	public ServiceInstance(DeleteServiceInstanceRequest request) { 
		this.serviceInstanceId = request.getServiceInstanceId();
		this.planId = request.getPlanId();
		this.serviceDefinitionId = request.getServiceId();
	}
	
	/**
	 * Create a service instance from a delete request. If fields 
	 * are not present in the request they will remain null in the 
	 * ServiceInstance.
	 * @param request containing details of ServiceInstance
	 */
	public ServiceInstance(UpdateServiceInstanceRequest request) { 
		request.getPlanId();
		request.getServiceInstanceId();
	}
	
	public ServiceInstance withDashboardUrl(String dashboardUrl) { 
		this.dashboardUrl = dashboardUrl;
		return this;
	}
	
	public ServiceInstance isAsync(boolean b) { 
		this.async = b; 
		return this;
	}
	
	public String getServiceInstanceId() {
		return serviceInstanceId;
	}

	public String getServiceDefinitionId() {
		return serviceDefinitionId;
	}

	public String getPlanId() {
		return planId;
	}

	public String getOrganizationGuid() {
		return organizationGuid;
	}

	public String getSpaceGuid() {
		return spaceGuid;
	}

	public String getDashboardUrl() {
		return dashboardUrl;
	}

	public boolean isAsync() {
		return async;
	}

	public ServiceInstance and() {
		return this;
	}

	public ServiceInstance withLastOperation(ServiceInstanceLastOperation lastOperation) {
		this.lastOperation = lastOperation;
		return this;
	}

	public ServiceInstanceLastOperation getServiceInstanceLastOperation() {
		return lastOperation;
	}
	
}

@@ -1569,35 +1569,136 @@
 ceInstanceId();%0A
+%09%09this.lastOperation = new ServiceInstanceLastOperation(%22Provisioning%22, OperationState.IN_PROGRESS);%0A
 %09%7D%0A
-
 %09%0A%09/**%0A%09 * Creat
@@ -2109,24 +2109,128 @@
 erviceId();%0A
+%09%09this.lastOperation = new ServiceInstanceLastOperation(%22Deprovisioning%22, OperationState.IN_PROGRESS);%0A%0A
 %09%7D%0A%09%0A%09/**%0A%09 
@@ -2527,16 +2527,16 @@
 anId();%0A
-
 %09%09reques
@@ -2561,16 +2561,113 @@
 ceId();%0A
+%09%09this.lastOperation = new ServiceInstanceLastOperation(%22Updating%22, OperationState.IN_PROGRESS);%0A
 %09%7D%0A%09%0A%09pu

Set last operation state in service instance object to IN_PROGRESS by default