using lkfxdok.LKF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test.LKF
{
    internal class LKFContextComparer
    {
        ContextWithData _source;

        Dictionary<String, Aufenthalt> _sourceAufenthalte;

        public LKFContextComparer(ContextWithData source)
        {
            _source = source;
        }

        public void Compare(ContextWithData target, string targetDescription = null)
        {
            string headline = null;


            if (targetDescription != null)
            {
                headline = $"differences to {targetDescription}";
            }

            if (_source == null)
            {
                throw new NullReferenceException("source context");
            }

            if (_sourceAufenthalte == null)
            {
                _sourceAufenthalte = new Dictionary<string, Aufenthalt>();
                foreach (var item in _source.Aufenthalte)
                {
                    _sourceAufenthalte[item.Id] = item;
                }
            }

            var targetAufenthalte = new Dictionary<String, Aufenthalt>();
            foreach (var item in target.Aufenthalte)
            {
                targetAufenthalte[item.Id] = item;
            }

            Aufenthalt targetAufenthalt;

            foreach (var item in _sourceAufenthalte)
            {
                if (targetAufenthalte.TryGetValue(item.Key, out targetAufenthalt))
                {
                    var sourceResult = item.Value.ResultLine;
                    var targetResult = targetAufenthalt.ResultLine;

                    if (sourceResult == null)
                    {
                        Console.WriteLine($"{item.Key}: no result line");
                        continue;
                    }
                    if (targetResult == null)
                    {
                        Console.WriteLine($"{item.Key}: no result line to compare");
                        continue;
                    }


                    var sourceResultLine = sourceResult.Line;
                    var targetResultLine = targetResult.Line;

                    if (sourceResultLine != targetResultLine)
                    {
                        if (headline != null)
                        {
                            Console.WriteLine(headline);
                            headline = null;
                        }
                        Console.WriteLine($"{item.Key}: difference {sourceResultLine} vs {targetResultLine}");
                    }
                }
                else
                {
                    if (headline != null)
                    {
                        Console.WriteLine(headline);
                        headline = null;
                    }
                    Console.WriteLine($"{item.Key}: only in source");
                }
            }

            foreach (var t in targetAufenthalte.Keys)
            {
                if (!_sourceAufenthalte.ContainsKey(t))
                {
                    if (headline != null)
                    {
                        Console.WriteLine(headline);
                        headline = null;
                    }
                    Console.WriteLine($"{t}: only in target");

                }
            }

            if (headline != null)
            {
                Console.WriteLine("no " + headline);
            }

        }
    }

}
