/*
 * Copyright 2015 the original author or authors.
 *
 * 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 org.openehealth.ipf.commons.ihe.fhir.iti83;

import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Operation;
import ca.uhn.fhir.rest.annotation.OperationParam;
import ca.uhn.fhir.rest.api.server.RequestDetails;
import ca.uhn.fhir.rest.param.TokenParam;
import ca.uhn.fhir.rest.param.UriParam;
import org.hl7.fhir.r4.model.*;
import org.openehealth.ipf.commons.ihe.fhir.AbstractPlainProvider;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.openehealth.ipf.commons.ihe.fhir.Constants.SOURCE_IDENTIFIER_NAME;
import static org.openehealth.ipf.commons.ihe.fhir.Constants.TARGET_SYSTEM_NAME;

/**
 * According to the PIXM specification, this resource provider must handle requests in the form
 * GET [base]/Patient/$ihe-pix?sourceIdentifier=[token]]{&targetSystem=[uri]}{&_format=[mime-type]}
 *
 * @author Christian Ohr
 * @since 3.6
 */
public class Iti83ResourceProvider extends AbstractPlainProvider {

    /**
     * Handles the PIXm Query
     *
     * @param resourceId          resource ID, optional
     * @param sourceIdentifierParam Identifier to search for. Should be an {@link Identifier}, but obviously
     *                              non-primitive types are forbidden in GET operations
     * @param targetSystemParam     target system URI
     * @return {@link Parameters} containing found identifiers
     */
    @SuppressWarnings("unused")
    @Operation(name = Iti83Constants.PIXM_OPERATION_NAME, type = Patient.class, idempotent = true, returnParameters = {@OperationParam(name = "return", type = Identifier.class, max = 100)})
    public Parameters pixmQuery(
            @IdParam(optional = true) IdType resourceId,
            @OperationParam(name = SOURCE_IDENTIFIER_NAME) TokenParam sourceIdentifierParam,
            @OperationParam(name = TARGET_SYSTEM_NAME) UriParam targetSystemParam,
            RequestDetails requestDetails,
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse) {

        var sourceIdentifier = new Identifier();

        if (resourceId == null) {
            sourceIdentifier.setSystem(sourceIdentifierParam.getSystem())
                    .setValue(sourceIdentifierParam.getValue());
        } else {
            sourceIdentifier.setValue(resourceId.getIdPart());
        }
        var targetUri = targetSystemParam == null ? null : new UriType(targetSystemParam.getValue());

        var inParams = new Parameters();
        inParams.addParameter().setName(SOURCE_IDENTIFIER_NAME).setValue(sourceIdentifier);
        inParams.addParameter().setName(TARGET_SYSTEM_NAME).setValue(targetUri);

        // Run down the route
        return requestResource(inParams, null, Parameters.class,
                httpServletRequest, httpServletResponse, requestDetails);
    }
}

@@ -675,273 +675,441 @@
 ort 
-ca.uhn
+static org.openehealth.ipf.commons.ihe
 .fhir.
-re
+Con
 st
-.
 an
-notation.IdParam;%0Aimport ca.uhn
+ts.SOURCE_IDENTIFIER_NAME;%0Aimport static org.openehealth.ipf.commons.ihe
 .fhir.
-re
+Con
 st
-.
 an
-notation.Operation;%0Aimport ca.uhn.fhir.rest.annotation.OperationParam;%0Aimport ca.uhn.fhir.rest.api.server.RequestDetails;%0Aimport ca.uhn.fhir.rest.param.TokenParam;%0Aimport ca.uhn.fhir.rest.param.UriParam
+ts.TARGET_SYSTEM_NAME;%0A%0Aimport java.util.List;%0A%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0Aimport org.hl7.fhir.r4.model.IdType;%0Aimport org.hl7.fhir.r4.model.Identifier;%0Aimport org.hl7.fhir.r4.model.Parameters;%0Aimport org.hl7.fhir.r4.model.Patient
 ;%0Aim
@@ -1135,17 +1135,23 @@
 4.model.
-*
+UriType
 ;%0Aimport
@@ -1223,259 +1223,273 @@
 ort 
-javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0Aimport static org.openehealth.ipf.commons.ihe.fhir.Constants.SOURCE_IDENTIFIER_NAME;%0Aimport static org.openehealth.ipf.commons.ihe.fhir.Constants.TARGET_SYSTEM_NAME
+ca.uhn.fhir.rest.annotation.IdParam;%0Aimport ca.uhn.fhir.rest.annotation.Operation;%0Aimport ca.uhn.fhir.rest.annotation.OperationParam;%0Aimport ca.uhn.fhir.rest.api.server.RequestDetails;%0Aimport ca.uhn.fhir.rest.param.TokenParam;%0Aimport ca.uhn.fhir.rest.param.UriParam
 ;%0A%0A/
@@ -2684,24 +2684,29 @@
 M_NAME) 
+List%3C
 UriParam
  targetS
@@ -2693,24 +2693,25 @@
 ist%3CUriParam
+%3E
  targetSyste
@@ -2712,24 +2712,28 @@
 tSystemParam
+List
 ,%0A          
@@ -3188,110 +3188,8 @@
    %7D
-%0A        var targetUri = targetSystemParam == null ? null : new UriType(targetSystemParam.getValue());
 %0A%0A  
@@ -3319,32 +3319,222 @@
 ifier);%0A        
+%0A        if (targetSystemParamList!=null) %7B%0A          for(var targetSystemParam : targetSystemParamList) %7B%0A            var targetUri = new UriType(targetSystemParam.getValue());%0A            
 inParams.addPara
@@ -3590,16 +3590,37 @@
 etUri);%0A
+          %7D%0A        %7D
 %0A       

change targetSystem to 0..n