using FSI.BT.IR.Plc.Settings.Log.Data;
using FSI.BT.IR.Plc.Settings.Log.Models;
using Microsoft.EntityFrameworkCore;

namespace FSI.BT.IR.Plc.Settings.Log
{
    public class DataManager
    {
        private readonly ApplicationDbContext _context;

        public DataManager(ApplicationDbContext context)
        {
            _context = context;
        }

        private ApplicationDbContext Context { get { return _context; } }

        #region Organization

        public async Task<List<Organization>> LoadOrganizationsAsync()
        {
            await UpdateOrganizationFullShortNameAsync(await _context.Organizations.ToListAsync()); // Kurznamen aktualisieren

            var task = Task.Factory.StartNew(() =>
            {

                return _context.Organizations.Include(x => x.Parent).ToList();
            });
            var result = await task;
            
            return result;
        }

        public async Task UpdateOrganizationFullShortNameAsync(List<Organization> organizations)
        {

            foreach (var organization in organizations)
            {
                foreach (var item in organizations)
                {
                    item.FullShortName = GetOrganizationFullShortName(organizations, item.ParentId, item.ShortName);
                    _context.Update(item);
                }
            }
            await _context.SaveChangesAsync();

        }

        public string GetOrganizationFullShortName(List<Organization> items, int? parentId, string? name)
        {
            foreach (var item in items)
            {
                if (item.Id == parentId)
                {
                    if (item.ParentId.HasValue)
                    {
                        return GetOrganizationFullShortName(items, item.ParentId.Value, item.ShortName + " " + name);
                    }
                    else
                    {
                        return item.ShortName + name + " ";
                    }
                }
            }

            return name;
        }

        #endregion

        #region Plc

        public async Task<List<Models.Plc>> LoadPlcsAsync()
        {
         
            var task = Task.Factory.StartNew(() =>
            {
                return _context.Plcs.Include(x => x.Organization).ToList();
            });
            var result = await task;

            return result;
        }

        #endregion

        #region Parameter

        public async Task<List<Parameter>> LoadParameterAsync()
        {

            var task = Task.Factory.StartNew(() =>
            {
                return _context.Parameters.Include(x => x.Organization).Include(x => x.Plc).ToList();
            });
            var result = await task;

            return result;
        }

        #endregion

        #region Logging

        public async Task<List<Logging>> LoadLggingsAsync()
        {

            var task = Task.Factory.StartNew(() =>
            {
                return _context.Logging
                .Include(x => x.Parameter).ThenInclude( y => y.Organization)
                .Include(x => x.Parameter).ThenInclude(y => y.Plc)
                .ToList();
            });
            var result = await task;

            return result;
        }

        #endregion

    }

}

