using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models;
using StructureHelperCommon.Models.Analyses;
using StructureHelperCommon.Models.Loggers;
using StructureHelperLogic.Models.Analyses;
using StructureHelperLogics.Models.CrossSections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.DTOs
{
    public class DateVersionToDTOConvertStrategy : IConvertStrategy<DateVersionDTO, IDateVersion>
    {
        private IUpdateStrategy<IDateVersion> updateStrategy;
        private IConvertStrategy<ISaveable, ISaveable> convertStrategy;
        private DictionaryConvertStrategy<ISaveable, ISaveable> convertLogic;

        public Dictionary<(Guid id, Type type), ISaveable> ReferenceDictionary { get; set; }
        public IShiftTraceLogger TraceLogger { get; set; }

        public DateVersionToDTOConvertStrategy(
            IUpdateStrategy<IDateVersion> updateStrategy,
            IConvertStrategy<ISaveable, ISaveable> convertStrategy)
        {
            this.updateStrategy = updateStrategy;
            this.convertStrategy = convertStrategy;
        }

        public DateVersionToDTOConvertStrategy() : this (
            new DateVersionUpdateStrategy(),
            new VersionItemToDTOConvertStrategy())
        {
            
        }

        public DateVersionDTO Convert(IDateVersion source)
        {
            try
            {
                Check();
                return GetNewDateVersion(source);
            }
            catch (Exception ex)
            {
                TraceLogger?.AddMessage(LoggerStrings.LogicType(this), TraceLogStatuses.Error);
                TraceLogger?.AddMessage(ex.Message, TraceLogStatuses.Error);
                throw;
            }
        }

        private DateVersionDTO GetNewDateVersion(IDateVersion source)
        {
            TraceLogger?.AddMessage("Date version converting is started", TraceLogStatuses.Debug);
            DateVersionDTO newItem = new()
            {
                Id = source.Id
            };
            updateStrategy.Update(newItem, source);
            convertStrategy.ReferenceDictionary = ReferenceDictionary;
            convertStrategy.TraceLogger = TraceLogger;
            convertLogic = new DictionaryConvertStrategy<ISaveable, ISaveable>(this, convertStrategy);
            newItem.AnalysisVersion = convertLogic.Convert(source.AnalysisVersion);
            TraceLogger?.AddMessage("Date version converting has been finished", TraceLogStatuses.Service);
            return newItem;
        }

        private void Check()
        {
            var checkLogic = new CheckConvertLogic<DateVersionDTO, IDateVersion>(this);
            checkLogic.Check();
        }
    }
}
