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

namespace lkfxdok.LKF.KA
{
    public class Lines
    {
        LineTypeKasKore _kasKore;
        LineTypeBericht _bericht;
        LineTypeGG _gg;
        LineTypeStamm _stamm;
        LineTypeOther _other;

        public Lines (ContextWithData context)
        {
            uint i, count = context.CD.AdditionalLines;

            _kasKore = new LineTypeKasKore();
            _gg = new LineTypeGG();
            _bericht = new LineTypeBericht();
            _stamm = new LineTypeStamm();
            _other = new LineTypeOther();

            for (i = 0; i < count; i++)
            {
                var line = context.CD.GetAdditionalLine(i);

                if (line == null)
                    continue;

                char type;
                int number;

                if (line.Length >= 3)
                {
                    type = line[0];
                    try
                    {
                        number = int.Parse(line.Substring(1, 2));
                    }
                    catch
                    {
                        number = -1;
                    }
                }
                else if (line.Length >= 1)
                {
                    type = line[0];
                    number = -1;
                }
                else
                {
                    type = ' ';
                    number = -1;
                }

                switch (type)
                {
                    case 'K':
                        _kasKore.GetSatzartInternal(number).Add(line);
                        break;
                    case 'G':
                        _gg.GetSatzartInternal(number).Add(line);
                        break;
                    case 'B':
                        _bericht.GetSatzartInternal(number).Add(line);
                        break;
                    case 'L':
                        _stamm.GetSatzartInternal(number).Add(line);
                        break;
                    default:
                        _other.GetSatzartInternal(-1).Add(line);
                        break;
                }
            }
        }

        public LineTypeKasKore KASKORE
        {
            get
            {
                return _kasKore;
            }
        }

        public LineTypeBericht Bericht
        {
            get
            {
                return _bericht;
            }
        }

        public LineTypeGG GG
        {
            get
            {
                return _gg;
            }
        }

        public LineTypeStamm Stamm
        {
            get
            {
                return _stamm;
            }
        }

        public LineTypeOther Other
        {
            get
            {
                return _other;
            }
        }

        public IEnumerable<LKFLine> PrefixLKFLines => _kasKore.K01.Lines;
        public IEnumerable<String> PrefixLines => this.PrefixLKFLines.Select(x => x.Line);

        public IEnumerable<LKFLine> SuffixLKFLines
        {
            get
            {
                foreach (var l in _kasKore.SuffixLines) yield return l;
                foreach (var l in _gg.All) yield return l;
                foreach (var l in _bericht.All) yield return l;
                foreach (var l in _stamm.All) yield return l;
                foreach (var l in _other.All) yield return l;
            }
        }

        public IEnumerable<String> SuffixLines => this.SuffixLKFLines.Select(l => l.Line);
    }
}
