package oracle.fusion.pts.restService;

import com.oracle.xmlns.apps.cdm.foundation.parties.locationservice.Location;

import com.oracle.xmlns.apps.crmcommon.salesparties.accountservice.Account;

import java.io.InputStream;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

import javax.xml.bind.JAXBException;

import oracle.demo.pts.fusionservices.FusionAccountService;

import oracle.fusion.pts.restService.exceptions.FusionGenericException;
import oracle.fusion.pts.restService.types.RESTAccountList;
import oracle.fusion.pts.restService.utils.RESTfulFusionService;

@Path("/account")
public class RESTfulAccountService extends RESTfulFusionService {
    FusionAccountService objectService = new FusionAccountService();
    String fusionEndpointName="fusionCRMSalesCloudAccountEndpoint";
    
    @POST
    @Path("/xmlquery")
    @Produces({"application/xml", "application/json"})
    public RESTAccountList findXMLQuery(@Context HttpHeaders pHttpHeaders,
                                                      InputStream pInputStream
                                                      ) throws JAXBException
    {
        return (find(pHttpHeaders,"","","",pInputStream));                                           
    }
                                                          
                                                          
                                                          
    @GET
    @Produces({"application/xml", "application/json"})
    public RESTAccountList find(@Context HttpHeaders pHttpHeaders, 
                                                      @QueryParam("attributes") String pAttributes,
                                                      @QueryParam("query") String pQuery,@QueryParam("conjunction") String pConjunction,
                                                      InputStream pInputStream
                                                      ) throws JAXBException
    {
        String xmlQuery=convertStreamToString(pInputStream);                                                     
        // Initialise the connections
        processRESTServiceHeaders(pHttpHeaders);
        objectService.initialiseConnection(this.getFusionJWTToken(), this.getFusionUsername(), this.getFusionPassword(), this.getFusionEndpointURL(fusionEndpointName), this.getFetchSize(), this.getFetchStart());
        
        List<Account> result = null;
        try {
            result=objectService.find(xmlQuery,pAttributes,pQuery,pConjunction);
        } 
        catch (com.sun.xml.ws.client.ClientTransportAccessException ctae) {
            throw new WebApplicationException(Response.Status.UNAUTHORIZED);
        }
        catch (Exception e) {
            logMessage("exception @ findAccount");
            e.printStackTrace();
            throw new FusionGenericException(e.getLocalizedMessage());    
        }
        logMessage("Number of Account found " + result.size());
      
        RESTAccountList ro2=new RESTAccountList();
        ro2.setAccountList(result);
        return ro2;

    }


    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public RESTAccountList getAccount( @Context HttpHeaders pHttpHeaders, 
                                         @PathParam("id") Long pId, 
                                         @QueryParam("attributes") String pAttributes
                                         ) throws JAXBException {
        // Initialise the connections
        processRESTServiceHeaders(pHttpHeaders);
        objectService.initialiseConnection(this.getFusionJWTToken(), this.getFusionUsername(), this.getFusionPassword(), this.getFusionEndpointURL(fusionEndpointName), this.getFetchSize(), this.getFetchStart());
        // Find specific opportunity
        List<Account> result = null;
        try {
            String seperator=objectService.getDefaultMinorQuerySeperator();
            result = objectService.find("", pAttributes, 
                                "PartyId"+seperator+"="+seperator+pId, "");
                
        } catch (NumberFormatException e) {
            // Error in opportunityid?
            throw new FusionGenericException("NumberFormatException, received " +
                                      pId + " for Account");
        }
            catch (com.sun.xml.ws.client.ClientTransportAccessException ctae) {
                throw new WebApplicationException(Response.Status.UNAUTHORIZED);
            }
            catch (Exception e) {
                logMessage("exception @ findLocation");
                e.printStackTrace();
                throw new FusionGenericException(e.getLocalizedMessage());
                
            }
        if (result.size()<1) {
            // No Location found
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
        logMessage("Result of findLocation (Address1)"+result.get(0));
        // Wrap it up so JAX-RS is happy
      
        RESTAccountList rol2 = new RESTAccountList();
        rol2.setAccountList(result);
        
        return rol2;
    }
    
    /*
    // TODO
    @POST
    @Path("simplecreate")
    @Produces( { "application/xml", "application/json" })
    public RESTAccountList createAccountSimple(@Context
        HttpHeaders pHttpHeaders
        ) {
        // TODO
        return new RESTAccountList();
    }
    
    // TODO
    @POST
    @Produces( { "application/xml", "application/json" })
    public RESTAccountList createAccountXML(@Context
        HttpHeaders pHttpHeaders
        ) 
    {
        // TODO 
        return new RESTAccountList();
    }
    */
    
    
    
    
}
