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 org.jboss.logging.Logger;

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;

	private static final Logger logger = Logger.getLogger(DraftTranslation.class);

	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());
			if(element == null)
			{
				logger.warn(String.format("Element %s is missing from translation %s", elementId, translation.getId()));
				continue;
			}
			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());
	}
}

@@ -1912,16 +1912,39 @@
 Results,
+ pageStructure.getId(),
  transla
@@ -1947,16 +1947,24 @@
 nslation
+.getId()
 );%0A%09%09%09%09p
@@ -2280,27 +2280,42 @@
 esults, 
-Translation
+UUID pageStructureId, UUID
  transla
@@ -2318,16 +2318,18 @@
 nslation
+Id
 )%0A%09%7B%0A%09%09f
@@ -2376,32 +2376,38 @@
 eySet())%0A%09%09%7B%0A%09%09%09
+final 
 TranslationEleme
@@ -2488,32 +2488,26 @@
  translation
-.getId()
+Id
 );%0A%09%09%09if(ele
@@ -2601,64 +2601,236 @@
 n %25s
-%22, elementId, translation.getId()));%0A%09%09%09%09continue;%0A%09%09%09%7D%0A
+ b/c this draft didn't exist when phrase was added.%22, elementId, translationId));%0A%09%09%09%09translationElementService.insert(createTranslationElement(translationResults, pageStructureId, translationId, elementId));%0A%09%09%09%7D%0A%09%09%09else%0A%09%09%09%7B%0A%09
 %09%09%09e
@@ -2893,16 +2893,17 @@
 d));%0A%09%09%09
+%09
 translat
@@ -2939,17 +2939,455 @@
 ent);%0A%09%09
-%7D
+%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09private TranslationElement createTranslationElement(TranslationResults translationResults, UUID pageStructureId, UUID translationId, UUID elementId)%0A%09%7B%0A%09%09TranslationElement newElement = new TranslationElement();%0A%09%09newElement.setId(elementId);%0A%09%09newElement.setTranslationId(translationId);%0A%09%09newElement.setPageStructureId(pageStructureId);%0A%09%09newElement.setTranslatedText(translationResults.get(elementId));%0A%0A%09%09return newElement;
 %0A%09%7D%0A%0A%09pr

Create elements that are missing when page is requested. - it's a little unfortunate the base text isn't known but there's no negative impact to that that i can see - it's not clear why these weren't created in the first place, as the add phrase endpoint should create them in the latest version regardless of status for every language