package com.client.core.resttrigger.controller.jobsubmission;

import com.bullhornsdk.data.model.entity.core.standard.Candidate;
import com.bullhornsdk.data.model.entity.core.standard.JobOrder;
import com.bullhornsdk.data.model.entity.core.standard.JobSubmission;
import com.client.core.base.model.relatedentity.JobSubmissionRelatedEntity;
import com.client.core.base.tools.web.JsonConverter;
import com.client.core.base.workflow.node.TriggerValidator;
import com.client.core.resttrigger.controller.AbstractRestTriggerController;
import com.client.core.resttrigger.model.api.RestTriggerRequest;
import com.client.core.resttrigger.model.api.RestTriggerResponse;
import com.client.core.resttrigger.model.helper.impl.JobSubmissionRestTriggerHelper;
import com.client.core.resttrigger.workflow.traversing.JobSubmissionRestTriggerTraverser;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
@RequestMapping("/resttrigger/jobsubmission/*")
public class JobSubmissionRestTriggerController extends AbstractRestTriggerController<JobSubmission, JobSubmissionRestTriggerHelper, JobSubmissionRestTriggerTraverser> {

    private final Logger log = Logger.getLogger(JobSubmissionRestTriggerController.class);
    private final JsonConverter jsonConverter;

    @Autowired
    public JobSubmissionRestTriggerController(Optional<List<TriggerValidator<JobSubmission, JobSubmissionRestTriggerHelper, JobSubmissionRestTriggerTraverser>>> triggerValidators, JsonConverter jsonConverter) {
        super(JobSubmission.class, triggerValidators, JobSubmissionRelatedEntity.values());
        this.jsonConverter = jsonConverter;
    }

    /**
     * Called when job is added.
     *
     * @param body
     * @return the json parsed form response message
     */
    @RequestMapping(value = {"add"}, method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    @ResponseBody
    public RestTriggerResponse addEntity(@RequestBody String body) {
        log.info("---------------------------- Starting Job Submission Validation Add Process From Rest Trigger ----------------------------------------");

        Map<String, Object> valuesChanges = (Map<String, Object>) convertToMap(body).get("data");

        RestTriggerRequest<JobSubmission> restTriggerRequest = convertToObject(body);

        Integer entityID = restTriggerRequest.getMeta().getEntityId();
        Integer updatingUserID = restTriggerRequest.getMeta().getUserId();


        String jobOrdersString = jsonConverter.convertEntityToJsonString(valuesChanges.get("jobOrder"));

        if (!jobOrdersString.contains("[")) {
            jobOrdersString = "[" + jobOrdersString + "]";
        }

        String candidatesString = jsonConverter.convertEntityToJsonString(valuesChanges.get("candidate"));

        List<JobOrder> jobOrderList = Arrays.asList(jsonConverter.convertJsonStringToEntity(jobOrdersString, JobOrder[].class));
        List<Candidate> candidateList = Arrays.asList(jsonConverter.convertJsonStringToEntity(candidatesString, Candidate[].class));

        Map<String, Object> newValueChanges = valuesChanges.entrySet().stream().filter(entry -> {
            return !entry.getKey().equals("jobOrder") && !entry.getKey().equals("candidate");
        }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));


        List<JobSubmissionRestTriggerTraverser> traversers = jobOrderList.stream().flatMap(jobOrder -> {
            Stream<JobSubmissionRestTriggerTraverser> stream = candidateList.stream().map(candidate -> {
                JobSubmissionRestTriggerTraverser traverser = new JobSubmissionRestTriggerTraverser(entityID, newValueChanges, updatingUserID, false, getRelatedEntityFields());
                traverser.getTriggerHelper().getNewEntity().setCandidate(candidate);
                traverser.getTriggerHelper().getNewEntity().setJobOrder(jobOrder);
                return traverser;
            });
            return stream;
        }).collect(Collectors.toList());

        return handleRequests(traversers, valuesChanges);

    }

    /**
     * Called when job is edited.
     *
     * @param body contains all the relevant data from the call
     * @return the json parsed form response message
     */
    @RequestMapping(value = {"edit"}, method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    @ResponseBody
    public RestTriggerResponse editEntity(@RequestBody String body) {
        log.info("---------------------------- Starting Job Submission Validation Edit Process From Rest Trigger ----------------------------------------");

        Map<String, Object> valuesChanges = (Map<String, Object>) convertToMap(body).get("data");

        RestTriggerRequest<JobSubmission> restTriggerRequest = convertToObject(body);

        Integer entityID = restTriggerRequest.getMeta().getEntityId();
        Integer updatingUserID = restTriggerRequest.getMeta().getUserId();

        JobSubmissionRestTriggerTraverser traverser = new JobSubmissionRestTriggerTraverser(entityID, valuesChanges, updatingUserID, true, getRelatedEntityFields());

        return handleRequest(traverser, valuesChanges);
    }

}

@@ -825,24 +825,64 @@
 rTraverser;%0A
+import com.google.common.collect.Lists;%0A
 import org.a
@@ -1325,86 +1325,9 @@
 til.
-Arrays;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Optional
+*
 ;%0Aim
@@ -5429,24 +5429,566 @@
 tUserId();%0A%0A
+        if (entityID.equals(-1) && valuesChanges.containsKey(%22ids%22)) %7B%0A%0A            List%3CInteger%3E ids = Lists.newArrayList((ArrayList%3CInteger%3E) valuesChanges.get(%22ids%22));%0A%0A            List%3CJobSubmissionRestTriggerTraverser%3E traversers =%0A                    ids.stream().map(entityId -%3E new JobSubmissionRestTriggerTraverser(entityId, valuesChanges, updatingUserID, true, getRelatedEntityFields()))%0A                            .collect(Collectors.toList());%0A%0A            return handleRequests(traversers, valuesChanges);%0A%0A        %7D else %7B%0A    
         JobS
@@ -6142,32 +6142,36 @@
 ds());%0A%0A        
+    
 return handleReq
@@ -6198,21 +6198,31 @@
 esChanges);%0A
+        %7D%0A
     %7D%0A%0A%7D%0A

Update Job Submission Rest Trigger controller to handle entity save when product pass entityID=-1