
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Serilog;
using System.Collections.Generic;
using WorkFlowCheck.BL.Services.Interfaces;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.DL;
using WorkFlowCheck.DL.Entities;

namespace WorkFlowCheck.BL.Services
{
    public class SyncService : ISyncService
    {
        private AppDbContext _dbContext;
        private IMapper _mapper;

        public SyncService(AppDbContext dbContext, IMapper mapper)
        {
            _dbContext = dbContext;
            _mapper = mapper;
        }

        public async Task<List<CheckPointDTO>> GetAllCheckPointAsync()
        {
            var retVal = new List<CheckPointDTO>();

            try
            {
                var res = await _dbContext.CheckPoints
                                         .Include(i => i.CheckListTemplateRows)
                                         .AsNoTracking()
                                         .ToListAsync();
                if (res != null)
                {
                    retVal = _mapper.Map<List<CheckPointDTO>>(res);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }

            return retVal;
        }
        public async Task<CheckPointDTO> GetCheckPointAsync(int id)
        {
            var retVal = new CheckPointDTO();

            try
            {
                var res = await _dbContext.CheckPoints
                                         .Include(i => i.CheckListTemplateRows)
                                         .Where(w => w.Id == id)
                                         .AsNoTracking()
                                         .FirstOrDefaultAsync();
                if (res != null)
                {
                    retVal = _mapper.Map<CheckPointDTO>(res);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }

            return retVal;
        }
        public async Task<CheckPointDTO> UpdateCheckPointAsync(CheckPointDTO checkPointDTO)
        {
            var retVal = new CheckPointDTO();

            try
            {
                if (checkPointDTO.Id == 0) //Hozzáadás, ha Id == 0
                {
                    var checkPoint = _mapper.Map<CheckPoint>(checkPointDTO);
                    _dbContext.CheckPoints.Add(checkPoint);
                    await _dbContext.SaveChangesAsync();

                    retVal = _mapper.Map<CheckPointDTO>(checkPoint);
                }
                else
                {
                    var res = await _dbContext.CheckPoints.Where(w => w.Id == checkPointDTO.Id).FirstOrDefaultAsync();
                    if (res != null)
                    {
                        res.ShortName = checkPointDTO.ShortName;
                        res.Code = checkPointDTO.Code;

                        await _dbContext.SaveChangesAsync();

                        retVal = _mapper.Map<CheckPointDTO>(res);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }

            return retVal;
        }
        public async Task<List<LocationDTO>> GetAllLocationAsync()
        {
            var retVal = new List<LocationDTO>();

            try
            {
                var res = await _dbContext.Locations.ToListAsync();
                if (res != null)
                {
                    retVal = _mapper.Map<List<LocationDTO>>(res);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }

            return retVal;
        }
        public async Task<List<EquipmentDTO>> GetAllEquipmentAsync()
        {
            var retVal = new List<EquipmentDTO>();

            try
            {
                var res = await _dbContext.Equipments.ToListAsync();
                if (res != null)
                {
                    retVal = _mapper.Map<List<EquipmentDTO>>(res);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }

            return retVal;
        }
        public async Task<List<CheckListTemplateHeaderDTO>> GetAllCheckListTemplateAsync()
        {
            var retVal = new List<CheckListTemplateHeaderDTO>();

            try
            {
                var res = await _dbContext.CheckListTemplateHeaders.Include(i => i.CheckListTemplateRows).AsNoTracking().ToListAsync();

                if (res != null)
                {
                    retVal = _mapper.Map<List<CheckListTemplateHeaderDTO>>(res);

                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }

            return retVal;
        }

    }
}
