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 : IPersonalEventTypeService
    {
        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task<IEnumerable<PersonalEventTypeDto>> GetPersonalEventTypesAsync(FilterDto filterDto)
        {
            var retval = new List<PersonalEventTypeDto>();
            using (var personalEventTypeUnit = new UnitOfWork<PersonalEventType>(new ISDbContext()))
            {
                var filter = _mapper.Map<Filter>(filterDto);
                var personalEventTypes = await personalEventTypeUnit.GetAsync(filter);

                foreach(var personalEventType in personalEventTypes)
                {
                    retval.Add(_mapper.Map<PersonalEventTypeDto>(personalEventType));
                }
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task<PersonalEventTypeDto> GetPersonalEventTypeByIdAsync(int id)
        {
            PersonalEventTypeDto retval;
            using (var personalEventTypeUnit = new UnitOfWork<PersonalEventType>(new ISDbContext()))
            {
                var personalEventType = await personalEventTypeUnit.GetByIdAsync(id);

                retval = _mapper.Map<PersonalEventTypeDto>(personalEventType);
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task CreatePersonalEventTypeAsync(PersonalEventTypeDto personalEventTypeDto)
        {
            using (var personalEventTypeUnit = new UnitOfWork<PersonalEventType>(new ISDbContext()))
            {
                var personalEventType = _mapper.Map<PersonalEventType>(personalEventTypeDto);
                await personalEventTypeUnit.CreateAsync(personalEventType);
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task UpdatePersonalEventTypeAsync(PersonalEventTypeDto personalEventTypeDto)
        {
            using (var personalEventTypeUnit = new UnitOfWork<PersonalEventType>(new ISDbContext()))
            {
                var personalEventType = _mapper.Map<PersonalEventType>(personalEventTypeDto);
                await personalEventTypeUnit.UpdateAsync(personalEventType);
            }
        }

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