<commit_before>package eu.ydp.empiria.player.client.controller.variables.processor.module;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.inject.Inject;

import eu.ydp.empiria.player.client.controller.variables.objects.Cardinality;
import eu.ydp.empiria.player.client.controller.variables.objects.response.DtoProcessedResponse;
import eu.ydp.empiria.player.client.controller.variables.objects.response.Response;
import eu.ydp.empiria.player.client.controller.variables.processor.results.ModulesProcessingResults;
import eu.ydp.empiria.player.client.controller.variables.processor.results.model.DtoModuleProcessingResult;
import eu.ydp.empiria.player.client.controller.variables.processor.results.model.LastAnswersChanges;
import eu.ydp.empiria.player.client.controller.variables.processor.results.model.UserInteractionVariables;

public class ResponseChangesFinder {

	private final ResponseDifferenceFinder responseDifferenceFinder;
	private final OrderedResponseChangesFinder orderedResponseChangesFinder;

	@Inject
	public ResponseChangesFinder(ResponseDifferenceFinder responseDifferenceFinder, OrderedResponseChangesFinder ordererResponseChangesFinder) {
		this.responseDifferenceFinder = responseDifferenceFinder;
		this.orderedResponseChangesFinder = ordererResponseChangesFinder;
	}

	public List<DtoProcessedResponse> findChangesOfAnswers(ModulesProcessingResults processingResults, Map<String, Response> responses) {
		List<DtoProcessedResponse> changedResponses = new ArrayList<DtoProcessedResponse>();

		for (String responseIdentifier : responses.keySet()) {
			Response response = responses.get(responseIdentifier);
			DtoModuleProcessingResult previousProcessingResult = processingResults.getProcessingResultsForResponseId(responseIdentifier);

			DtoProcessedResponse changedResponse = getChangedResponseForResponseId(response, previousProcessingResult);
			changedResponses.add(changedResponse);
		}

		return changedResponses;
	}

	private DtoProcessedResponse getChangedResponseForResponseId(Response response, DtoModuleProcessingResult previousProcessingResult) {
		List<String> currentAnswers = getAnswersOrEmptyList(response);
		List<String> previousAnswers = getPreviousAnswers(previousProcessingResult);

		LastAnswersChanges changesOfAnswers;

		if (response.cardinality == Cardinality.ORDERED) {
			changesOfAnswers = orderedResponseChangesFinder.findChangesOfAnswers(previousAnswers, currentAnswers);
		} else {
			changesOfAnswers = responseDifferenceFinder.findChangesOfAnswers(previousAnswers, currentAnswers);
		}

		UserInteractionVariables userInteractionVariables = previousProcessingResult.getUserInteractionVariables();
		userInteractionVariables.setLastAnswerChanges(changesOfAnswers);

		DtoProcessedResponse changedResponse = new DtoProcessedResponse(response, previousProcessingResult, changesOfAnswers);
		return changedResponse;
	}

	private List<String> getPreviousAnswers(DtoModuleProcessingResult previousProcessingResult) {
		List<String> previousAnswers = previousProcessingResult.getGeneralVariables().getAnswers();
		return previousAnswers;
	}

	private List<String> getAnswersOrEmptyList(Response response) {
		List<String> currentAnswers = response.values;
		List<String> answers;
		if (currentAnswers == null) {<commit_msg>YPUB-5349 Zmiana klasy ResponseDifferenceFinder issue YPUB-5349<commit_after>package eu.ydp.empiria.player.client.controller.variables.processor.module;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.inject.Inject;

import eu.ydp.empiria.player.client.controller.variables.objects.Cardinality;
import eu.ydp.empiria.player.client.controller.variables.objects.response.DtoProcessedResponse;
import eu.ydp.empiria.player.client.controller.variables.objects.response.Response;
import eu.ydp.empiria.player.client.controller.variables.processor.results.ModulesProcessingResults;
import eu.ydp.empiria.player.client.controller.variables.processor.results.model.DtoModuleProcessingResult;
import eu.ydp.empiria.player.client.controller.variables.processor.results.model.LastAnswersChanges;
import eu.ydp.empiria.player.client.controller.variables.processor.results.model.UserInteractionVariables;

public class ResponseChangesFinder {

	private final ResponseDifferenceFinder responseDifferenceFinder;
	private final OrderedResponseChangesFinder orderedResponseChangesFinder;

	@Inject
	public ResponseChangesFinder(ResponseDifferenceFinder responseDifferenceFinder, OrderedResponseChangesFinder ordererResponseChangesFinder) {
		this.responseDifferenceFinder = responseDifferenceFinder;
		this.orderedResponseChangesFinder = ordererResponseChangesFinder;
	}

	public List<DtoProcessedResponse> findChangesOfAnswers(ModulesProcessingResults processingResults, Map<String, Response> responses) {
		List<DtoProcessedResponse> changedResponses = new ArrayList<DtoProcessedResponse>();

		for (String responseIdentifier : responses.keySet()) {
			Response response = responses.get(responseIdentifier);
			DtoModuleProcessingResult previousProcessingResult = processingResults.getProcessingResultsForResponseId(responseIdentifier);

			DtoProcessedResponse changedResponse = getChangedResponseForResponseId(response, previousProcessingResult);
			changedResponses.add(changedResponse);
		}

		return changedResponses;
	}

	private DtoProcessedResponse getChangedResponseForResponseId(Response response, DtoModuleProcessingResult previousProcessingResult) {
		List<String> currentAnswers = getAnswersOrEmptyList(response);
		List<String> previousAnswers = getPreviousAnswers(previousProcessingResult);

		LastAnswersChanges changesOfAnswers = getChangesOfAnswers(response, currentAnswers, previousAnswers);

		UserInteractionVariables userInteractionVariables = previousProcessingResult.getUserInteractionVariables();
		userInteractionVariables.setLastAnswerChanges(changesOfAnswers);

		DtoProcessedResponse changedResponse = new DtoProcessedResponse(response, previousProcessingResult, changesOfAnswers);
		return changedResponse;
	}

	private LastAnswersChanges getChangesOfAnswers(Response response, List<String> currentAnswers, List<String> previousAnswers) {
		LastAnswersChanges changesOfAnswers;
		if (response.cardinality == Cardinality.ORDERED) {
			changesOfAnswers = orderedResponseChangesFinder.findChangesOfAnswers(previousAnswers, currentAnswers);
		} else {
			changesOfAnswers = responseDifferenceFinder.findChangesOfAnswers(previousAnswers, currentAnswers);
		}
		return changesOfAnswers;
	}

	private List<String> getPreviousAnswers(DtoModuleProcessingResult previousProcessingResult) {
		List<String> previousAnswers = previousProcessingResult.getGeneralVariables().getAnswers();
		return previousAnswers;
	}

	private List<String> getAnswersOrEmptyList(Response response) {
		List<String> currentAnswers = response.values;
		List<String> answers;
		if (currentAnswers == null) {
{'fraction_non_alphanumeric': 0.07492180835939721, 'fraction_numerical': 0.0011373329542223485, 'mean_word_length': 8.481132075471699, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3962078', 'n_tokens_mistral': 1966, 'n_tokens_neox': 1861, 'n_words': 284}