package org.cru.godtools.api.translations;

import org.ccci.util.time.Clock;
import org.cru.godtools.domain.languages.LanguageCode;
import org.cru.godtools.domain.packages.PageStructure;
import org.cru.godtools.domain.packages.PageStructureService;
import org.cru.godtools.domain.packages.TranslationElement;
import org.cru.godtools.domain.packages.TranslationElementService;
import org.cru.godtools.domain.translations.Translation;
import org.cru.godtools.translate.client.TranslationDownload;
import org.cru.godtools.translate.client.TranslationResults;
import org.cru.godtools.translate.client.TranslationStatus;

import javax.inject.Inject;
import java.util.List;
import java.util.UUID;

@Deprecated
public class DraftTranslation
{

	@Inject TranslationDownload translationDownload;
	@Inject TranslationElementService translationElementService;
	@Inject PageStructureService pageStructureService;
	@Inject Clock clock;

	public void updateFromTranslationTool(Integer translationProjectId,
															Translation translation,
															List<PageStructure> pageStructures,
															LanguageCode languageCode)
	{
		for(PageStructure pageStructure : pageStructures)
		{
			TranslationStatus remoteStatus = loadRemoteStatus(translationProjectId, languageCode, pageStructure);

			// this feature for optimization is removed until oneskyapp.com reviews my request
			// to have string count and word count reflect the status of the translation, not
			// the base content.  for now, all files are downloaded every time... :(
			// if(remoteStatus.differsFrom(cachedStatus))
			{
				TranslationResults downloadResults = translationDownload.doDownload(translationProjectId,
						languageCode.toString(),
						pageStructure.getFilename());

				updateLocalTranslationElements(downloadResults, translation);
				pageStructure.updateCachedStatus(remoteStatus.getPercentCompleted(),
						remoteStatus.getWordCount(),
						remoteStatus.getStringCount(),
						clock.currentDateTime());
				pageStructureService.update(pageStructure);
			}
		}
	}

	private void updateLocalTranslationElements(TranslationResults translationResults, Translation translation)
	{
		for(UUID elementId : translationResults.keySet())
		{
			TranslationElement element = translationElementService.selectyByIdTranslationId(elementId, translation.getId());
			element.setTranslatedText(translationResults.get(elementId));
			translationElementService.update(element);
		}
	}

	private TranslationStatus loadRemoteStatus(Integer projectId, LanguageCode languageCode, PageStructure pageStructure)
	{
		return translationDownload.checkTranslationStatus(projectId, languageCode.toString(), pageStructure.getFilename());
	}
}

@@ -608,16 +608,49 @@
 nStatus;
+%0Aimport org.jboss.logging.Logger;
 %0A%0Aimport
@@ -950,16 +950,97 @@
 clock;%0A%0A
+%09private static final Logger logger = Logger.getLogger(DraftTranslation.class);%0A%0A
 %09public 
@@ -2449,16 +2449,172 @@
 tId());%0A
+%09%09%09if(element == null)%0A%09%09%09%7B%0A%09%09%09%09logger.warn(String.format(%22Element %25s is missing from translation %25s%22, elementId, translation.getId()));%0A%09%09%09%09continue;%0A%09%09%09%7D%0A
 %09%09%09eleme

Add logging and skip to the next element when something in OneSky doesn't exist locally