public IList<IEntryLanguage> CreateEntryLanguages(ExcelTerm excelTerm)
        {
            var result = new List<IEntryLanguage>();

            var sourceEntryLanguage = new ExcelEntryLanguage
            {
                Locale = excelTerm.SourceCulture,
                Name = excelTerm.SourceCulture.EnglishName,
                Terms = CreateEntryTerms(excelTerm.Source),
                Fields = new List<IEntryField>(),
                IsSource = true
            };

            result.Add(sourceEntryLanguage);
            if (excelTerm.Target != null)
            {
                var targetEntryLanguage = new ExcelEntryLanguage
                {
                    Locale = excelTerm.TargetCulture,
                    Name = excelTerm.TargetCulture.EnglishName,
                    Terms = CreateEntryTerms(excelTerm.Target, excelTerm.Approved),
                    Fields = new List<IEntryField>(),
                    IsSource = false
                };
                result.Add(targetEntryLanguage);
            }

            return result;
        }
        public async Task AddOrUpdateEntry(int entryId, ExcelTerm excelEntry)
        {

            if (!string.IsNullOrWhiteSpace(excelEntry.Source) && !string.IsNullOrWhiteSpace(excelEntry.Target))
            {
                await _excelTermLoaderService.AddOrUpdateTerm(entryId, excelEntry);
            }
        }
        public async Task AddOrUpdateTerm(int entryId,ExcelTerm excelTerm)
        {
            using (var excelPackage =
               new ExcelPackage(new FileInfo(_providerSettings.TermFilePath)))
            {
                var workSheet = await GetTerminologyWorksheet(excelPackage);
                var sourceColumnAddress = $"{_providerSettings.SourceColumn}{entryId}";
                var targetColumnAddress = $"{_providerSettings.TargetColumn}{entryId}";

                workSheet.SetValue(sourceColumnAddress,excelTerm.Source);
                workSheet.SetValue(targetColumnAddress, excelTerm.Target);
                if (!string.IsNullOrEmpty(_providerSettings.ApprovedColumn))
                {
                    var approvedColumnAddress = $"{_providerSettings.ApprovedColumn}{entryId}";
                    workSheet.Cells[approvedColumnAddress].Value = excelTerm.Approved;
                }
                excelPackage.Save();
            }
        }
        private async Task Save()
        {
            try
            {
                if (sourceListView.SelectedObject == null) return;
                var entry = new ExcelTerm();

                var source = (ExcelEntry) sourceListView.SelectedObject;
                var entryId = source.Id;
                entry.Source = source.SearchText;

                foreach (var cultureCast in source.Languages.Cast<ExcelEntryLanguage>())
                {
                    if (cultureCast.IsSource)
                    {
                        entry.SourceCulture = cultureCast.Locale;
                    }
                    else
                    {
                        entry.TargetCulture = cultureCast.Locale;
                    }
                }

                var targetTerms = bsTarget.DataSource as List<ExcelDataGrid>;
                if (targetTerms != null)
                {
                    var termValue = string.Join("|", targetTerms.Select(x => x.Term));
                    var approvedValue = string.Join("|", targetTerms.Select(x => x.Approved));
                    entry.Target = termValue;
                    entry.Approved = approvedValue;
                }



                var entryLanguage = _transformerService.CreateEntryLanguages(entry);
                var entryToUpdate = _terms.Find(item => item.Id == entryId);
                
                if (entryToUpdate != null)
                {

                    entryToUpdate.Languages = entryLanguage;

                }
                await _excelTermProviderService.AddOrUpdateEntry(entryId, entry);
            }
            catch (Exception ex)
            {
                TelemetryService.Instance.AddException(ex);
                throw;
            }
        }
        public void AddAndEdit(IEntry entry, ExcelDataGrid excelDataGrid)
        {
            try
            {
                var selectedTerm = _terms.FirstOrDefault(item => item.Id == entry.Id);
                var termToAdd = new EntryTerm
                {
                    Value = excelDataGrid.Term
                };

                var excelTerm = new ExcelTerm
                {
                    SourceCulture = entry.Languages[0].Locale,
                    TargetCulture = entry.Languages[1].Locale,
                    Target = excelDataGrid.Term
                };
                var source = (ExcelEntry) entry;
                excelTerm.Source = source.SearchText;

                var exist = false;
                if (selectedTerm != null)
                {
                    foreach (var term in selectedTerm.Languages[1].Terms)
                    {
                        if (term.Value == excelDataGrid.Term)
                        {
                            exist = true;
                        }


                    }

                    if (exist == false)
                    {
                        selectedTerm.Languages[1].Terms.Add(termToAdd);

                        _terms[entry.Id].Languages = selectedTerm.Languages;
                    }

                }

                JumpToTerm(entry);
                Task.Run(Save);
            }
            catch (Exception ex)
            {
                TelemetryService.Instance.AddException(ex);
                throw;
            }
        }
        private void AddTermInternal(string source, string target)
        {
            try
            {
                var excelTerm = new ExcelTerm
                {
                    SourceCulture = _providerSettings.SourceLanguage,
                    TargetCulture = _providerSettings.TargetLanguage,
                    Source = source,
                    Target = target
                };

                var entryLanguages = _transformerService.CreateEntryLanguages(excelTerm);

                var maxId = 0;
                if (_terms.Count > 0)
                {
                    maxId = _terms.Max(term => term.Id);
                }

                var excelEntry = new ExcelEntry
                {
                    Id = maxId + 1,
                    Fields = new List<IEntryField>(),
                    Languages = entryLanguages,
                    SearchText = source

                };
                
                sourceListView.AddObject(excelEntry);
                _terms.Add(excelEntry);
                JumpToTerm(excelEntry);
            }
            catch (Exception ex)
            {
                TelemetryService.Instance.AddException(ex);
                throw;
            }
        }
 public Task AddOrUpdateTerm(int entryId, ExcelTerm excelTerm)
 {
     throw new NotImplementedException();
 }
        public async Task<Dictionary<int, ExcelTerm>> GetTermsFromExcel(ExcelWorksheet worksheet)
        {
            var result = new Dictionary<int, ExcelTerm>();

            var excelRangeAddress = _providerSettings.GetExcelRangeAddress();

            foreach (var cell in worksheet.Cells[excelRangeAddress])
            {
                var excellCellAddress = new ExcelCellAddress(cell.Address);

                if (_providerSettings.HasHeader && excellCellAddress.Row == 1)
                {
                    continue;
                }
                var id = excellCellAddress.Row;
                if (!result.ContainsKey(id))
                {
                    result[id] = new ExcelTerm();
                }

                SetCellValue(result[id], cell, excellCellAddress.Column);
               
            }
            return result;
        }
        private void SetCellValue(ExcelTerm excelTerm, ExcelRangeBase cell, int columnIndex)
        {
            var columnLetter = ExcelCellAddress.GetColumnLetter(columnIndex);
            if (columnLetter == _providerSettings.SourceColumn)
            {
                excelTerm.Source = cell.Text;
                excelTerm.SourceCulture = _providerSettings.SourceLanguage;
            }

            if (columnLetter == _providerSettings.TargetColumn)
            {
                excelTerm.Target = cell.Text;
                excelTerm.TargetCulture = _providerSettings.TargetLanguage;
            }

            if (columnLetter == _providerSettings.ApprovedColumn)
            {
                excelTerm.Approved = cell.Text;
            }
        }