using AutoMapper;
using DataAccessLayer;
using DataAccessLayer.Entities;
using DataAccessLayer.Filtering;
using DataAccessLayer.Units;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using WcfServices.Contract;
using WcfServices.Dto;
using WcfServices.Mapping;

namespace WcfServices.Services
{
    public partial class ISService : IRequestService
    {

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task<IEnumerable<Dto.RequestDto>> GetRequestsAsync(FilterDto filterDto)
        {
            var retval = new List<Dto.RequestDto>();
            using (var vacationRequestUnit = new UnitOfWork<DataAccessLayer.Entities.Request>(new ISDbContext()))
            {
                var filter = _mapper.Map<Filter>(filterDto);
                var vacationRequests = await vacationRequestUnit.GetAsync(filter);

                foreach(var vacationRequest in vacationRequests)
                {
                    retval.Add(_mapper.Map<Dto.RequestDto>(vacationRequest));
                }
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task<Dto.RequestDto> GetRequestByIdAsync(int id)
        {
            Dto.RequestDto retval;
            using (var vacationRequestUnit = new UnitOfWork<DataAccessLayer.Entities.Request>(new ISDbContext()))
            {
                var vacationRequest = await vacationRequestUnit.GetByIdAsync(id);

                retval = _mapper.Map<Dto.RequestDto>(vacationRequest);
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task CreateRequestAsync(Dto.RequestDto vacationRequestDto)
        {
            using (var vacationRequestUnit = new UnitOfWork<DataAccessLayer.Entities.Request>(new ISDbContext()))
            {
                var vacationRequest = _mapper.Map<DataAccessLayer.Entities.Request>(vacationRequestDto);
                vacationRequest.Supervisor = null;
                vacationRequest.Employee = null;
                await vacationRequestUnit.CreateAsync(vacationRequest);
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task UpdateRequestAsync(Dto.RequestDto vacationRequestDto)
        {
            using (var vacationRequestUnit = new UnitOfWork<DataAccessLayer.Entities.Request>(new ISDbContext()))
            {
                var vacationRequest = _mapper.Map<DataAccessLayer.Entities.Request>(vacationRequestDto);
                await vacationRequestUnit.UpdateAsync(vacationRequest);
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task DeleteRequestAsync(int id)
        {
            using (var vacationRequestUnit = new UnitOfWork<DataAccessLayer.Entities.Request>(new ISDbContext()))
            {
                await vacationRequestUnit.DeleteAsync(id);
            }
        }

    }
}
