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 : IPersonalEventService
    {
        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task<IEnumerable<PersonalEventDto>> GetPersonalEventsAsync(FilterDto filterDto)
        {
            var retval = new List<PersonalEventDto>();
            using (var personalEventUnit = new UnitOfWork<PersonalEvent>(new ISDbContext()))
            {
                var filter = _mapper.Map<Filter>(filterDto);
                var personalEvents = await personalEventUnit.GetAsync(filter);

                foreach(var personalEvent in personalEvents)
                {
                    retval.Add(_mapper.Map<PersonalEventDto>(personalEvent));
                }
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task<PersonalEventDto> GetPersonalEventByIdAsync(int id)
        {
            PersonalEventDto retval;
            using (var personalEventUnit = new UnitOfWork<PersonalEvent>(new ISDbContext()))
            {
                var personalEvent = await personalEventUnit.GetByIdAsync(id);

                retval = _mapper.Map<PersonalEventDto>(personalEvent);
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task CreatePersonalEventAsync(PersonalEventDto personalEventDto)
        {
            using (var personalEventUnit = new UnitOfWork<PersonalEvent>(new ISDbContext()))
            {
                var personalEvent = _mapper.Map<PersonalEvent>(personalEventDto);
                personalEvent.PersonalEventType = null;
                personalEvent.Employee = null;
                await personalEventUnit.CreateAsync(personalEvent);
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task UpdatePersonalEventAsync(PersonalEventDto personalEventDto)
        {
            using (var personalEventUnit = new UnitOfWork<PersonalEvent>(new ISDbContext()))
            {
                var personalEvent = _mapper.Map<PersonalEvent>(personalEventDto);
                await personalEventUnit.UpdateAsync(personalEvent);
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task DeletePersonalEventAsync(int id)
        {
            using (var personalEventUnit = new UnitOfWork<PersonalEvent>(new ISDbContext()))
            {
                await personalEventUnit.DeleteAsync(id);
            }
        }

    }
}
