using FSI.BT.IR.Organization.Db.Data;
using Microsoft.EntityFrameworkCore;

namespace FSI.BT.IR.Organization.Db.Services
{
    public class OrganizationService : IOrganizationService
    {
        private readonly AppDbContext _context;

        public OrganizationService(AppDbContext context)
        {
            _context = context;
        }

        public async Task<Models.Organization> GetDataByIdAsync(int id)
        {
            return await _context.Organizations.FindAsync(id);
        }

        public async Task<List<Models.Organization>> GetDatasAsync()
        {
            return await _context.Organizations.ToListAsync();
        }

        public async Task<Models.Organization> SetFullShortName(Models.Organization organization)
        {
            organization.FullShortName = await GetFullShortName(organization.ParentId, organization.ShortName);
            return organization;
        }

        public async Task AddAsync(Models.Organization organization)
        {
            organization.FullShortName = await GetFullShortName(organization.ParentId, organization.ShortName); // Kürzel updaten
            _context.Add(organization);
            await _context.SaveChangesAsync();
        }

        public async Task UpdateAsync(Models.Organization organization)
        {          
          
            _context.Update(organization);
            await _context.SaveChangesAsync();

            organization.FullShortName = await GetFullShortName(organization.ParentId, organization.ShortName);
            _context.Update(organization);
            await _context.SaveChangesAsync();

        }

        public async Task RemoveAsync(Models.Organization organization)
        {
            _context.Remove(organization);
            await _context.SaveChangesAsync();
        }

        public async Task SetParentIdNullAsync(int id)
        {
            _context.Organizations.Where(x => x.ParentId == id).ForEachAsync(x => x.ParentId = null);
        }


        #region Methods
        private async Task UpdateAllFullShortName()
        {
            List<Models.Organization> organizations = await GetDatasAsync();

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

            _context.SaveChanges(); 

        }
        
        private async Task<string> GetFullShortName(int? parentId, string? shortName)
        {
            
            foreach (var item in _context.Organizations.ToList())
            {
                if (item.Id == parentId)
                {
                    if (item.ParentId.HasValue)
                    {
                        return await GetFullShortName(item.ParentId.Value, item.ShortName + " " + shortName);
                    }
                    else
                    {
                        return item.ShortName + shortName + " ";
                    }
                }
            }

            return shortName;
        }

        #endregion
    }
}
