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

using lkfxdok.c;

namespace lkfxdok.LKF.stat
{
    public enum StatType : uint
    {
        KA = 0,
        Abt = 1,
        Alter = 2,
        Bundesland = 3
    }

    public enum StatField : int
    {
        Belagstage = 0,
        Aufnahmen = 1,
        Verblieben = 2,
        Entlassung = 3,
        Verstorben = 4,
        Verbleibend = 5,
        GanzJahr = 6,
        Eintag = 7,
        Male = 8,
        Female = 9,
        AufCount = 10,
        PflegeTage = 11,
        Amb = 12,
        Divers = 13
    }


    public class StatContext
    {
        private readonly LKFStatContext _statContext;
        public readonly StatAltersgruppe[] Altersgruppen;
        public readonly StatBundesland[] Bundeslaender;
        public readonly StatKA KA;
        public readonly SortedDictionary<String, StatAbteilung> Abteilungen;

        internal StatContext (ContextWithData context, LKFStatContext statContext)
        {
            _statContext = statContext;

            uint i;
            var count = GetLineCount(StatType.Alter);
            Altersgruppen = new StatAltersgruppe[count];

            for (i = 0; i < count; i++)
            {
                Altersgruppen[i] = new StatAltersgruppe(this, i);
            }

            KA = new StatKA(context, this);

            count = GetLineCount(StatType.Bundesland);
            Bundeslaender = new StatBundesland[count];
            for (i = 0; i < count; i++)
            {
                Bundeslaender[i] = new StatBundesland(this, i);
            }

            count = GetLineCount(StatType.Abt);
            Abteilungen = new SortedDictionary<string, StatAbteilung>();
            for (i = 0; i < count; i++)
            {
                var abteilung = new StatAbteilung(this, i);
                Abteilungen[abteilung.ABTFC] = abteilung;
            }
        }

        public uint GetLineCount (StatType type)
        {
            return _statContext.GetStatCount((uint)type);
        }

        public string GetFieldValue (StatType type, uint index, StatField field)
        {
            return _statContext.GetStatField((uint)type, index, (int)field);
        }

        public string GetLineName (StatType type, uint index)
        {
            return _statContext.GetStatLineName((uint)type, index);
        }

        public int GetIntValue (StatType type, uint index, StatField field)
        {
            var s = _statContext.GetStatField((uint)type, index, (int)field);
            if (string.IsNullOrEmpty(s))
                return 0;
            try
            {
                return int.Parse(s);
            }
            catch
            {
                return 0;
            }
        }
    }
}
