<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using LiftIO.Validation;
using Palaso.DictionaryServices.Lift;
using Palaso.DictionaryServices.Model;
using Palaso.Progress.LogBox;
using SolidGui.Engine;
using SolidGui.Filter;
using SolidGui.Model;
using System.Linq;

namespace SolidGui.Export
{
	public class ExportLift : IExporter
	{

		public void Export(IEnumerable<Record> sfmLexEntries, SolidSettings solidSettings, string outputFilePath, IProgress outerProgress)
		{
			
			var liftLexEntries = new List<SfmLiftLexEntryAdapter>();
			var logPath = outputFilePath + ".exportErrors.txt";
			if (File.Exists(outputFilePath))
			{
				File.Delete(outputFilePath);
			}
			if (File.Exists(logPath))
			{
				File.Delete(logPath);
			}

			var stringBuilderProgress = new StringBuilderProgress();
			//our overal progress reports both to the UI and to this file we want to write out
			var progress = new MultiProgress(new IProgress[] { outerProgress, stringBuilderProgress });

			using (var dm = new LiftDataMapper(outputFilePath))
			{
				// first pass
				var index = ConvertAllEntriesToLift(sfmLexEntries, dm, solidSettings, liftLexEntries, progress);

				// second pass
				ResolveTargetsOfRelations(liftLexEntries, progress, index);
				dm.SaveItems(from x in dm.GetAllItems() select dm.GetItem(x));
			}

			if (!string.IsNullOrEmpty(stringBuilderProgress.Text))
			{
				using (var log = File.AppendText(logPath))
				{
					log.WriteLine(stringBuilderProgress.Text);
				}
			}
			outerProgress.WriteMessage("");
			outerProgress.WriteMessage("Done");
			Validator.CheckLiftWithPossibleThrow(outputFilePath);
		}

	    private Dictionary<string, SfmLiftLexEntryAdapter> ConvertAllEntriesToLift(IEnumerable<Record> sfmLexEntries, LiftDataMapper dm, SolidSettings solidSettings, List<SfmLiftLexEntryAdapter> liftLexEntries, MultiProgress progress)
	    {
            var index = new Dictionary<string, SfmLiftLexEntryAdapter>();
	        foreach (var sfmLexEntry in sfmLexEntries)
	        {
	            try
	            {
	                var adaptedEntry = new SfmLiftLexEntryAdapter(dm, sfmLexEntry.LexEntry, solidSettings);<commit_msg>On LIFT export, delay saying "done" until validation is finished.<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using LiftIO.Validation;
using Palaso.DictionaryServices.Lift;
using Palaso.DictionaryServices.Model;
using Palaso.Progress.LogBox;
using SolidGui.Engine;
using SolidGui.Filter;
using SolidGui.Model;
using System.Linq;

namespace SolidGui.Export
{
	public class ExportLift : IExporter
	{

		public void Export(IEnumerable<Record> sfmLexEntries, SolidSettings solidSettings, string outputFilePath, IProgress outerProgress)
		{
			
			var liftLexEntries = new List<SfmLiftLexEntryAdapter>();
			var logPath = outputFilePath + ".exportErrors.txt";
			if (File.Exists(outputFilePath))
			{
				File.Delete(outputFilePath);
			}
			if (File.Exists(logPath))
			{
				File.Delete(logPath);
			}

			var stringBuilderProgress = new StringBuilderProgress();
			//our overal progress reports both to the UI and to this file we want to write out
			var progress = new MultiProgress(new IProgress[] { outerProgress, stringBuilderProgress });

			using (var dm = new LiftDataMapper(outputFilePath))
			{
				// first pass
				var index = ConvertAllEntriesToLift(sfmLexEntries, dm, solidSettings, liftLexEntries, progress);

				// second pass
				ResolveTargetsOfRelations(liftLexEntries, progress, index);
				dm.SaveItems(from x in dm.GetAllItems() select dm.GetItem(x));
			}

			if (!string.IsNullOrEmpty(stringBuilderProgress.Text))
			{
				using (var log = File.AppendText(logPath))
				{
					log.WriteLine(stringBuilderProgress.Text);
				}
			}
			outerProgress.WriteMessage("");
			outerProgress.WriteMessage("Checking result to make sure it is valid LIFT XML...");
            var result = Validator.GetAnyValidationErrors(outputFilePath);
            if(!string.IsNullOrEmpty(result))
                outerProgress.WriteError(result);
            outerProgress.WriteMessage("Done");
		}

	    private Dictionary<string, SfmLiftLexEntryAdapter> ConvertAllEntriesToLift(IEnumerable<Record> sfmLexEntries, LiftDataMapper dm, SolidSettings solidSettings, List<SfmLiftLexEntryAdapter> liftLexEntries, MultiProgress progress)
	    {
            var index = new Dictionary<string, SfmLiftLexEntryAdapter>();
	        foreach (var sfmLexEntry in sfmLexEntries)
	        {
	            try
	            {
	                var adaptedEntry = new SfmLiftLexEntryAdapter(dm, sfmLexEntry.LexEntry, solidSettings);
{'fraction_non_alphanumeric': 0.08959044368600683, 'fraction_numerical': 0.0, 'mean_word_length': 4.0365198711063375, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '1863229', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1311, 'n_words': 316}