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 : IAttendanceEventTypeService
    {

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task<IEnumerable<AttendanceEventTypeDto>> GetAttendanceEventTypesAsync(FilterDto filterDto)
        {
            var retval = new List<AttendanceEventTypeDto>();
            using (var attendanceEventTypeUnit = new UnitOfWork<AttendanceEventType>(new ISDbContext()))
            {
                var filter = _mapper.Map<Filter>(filterDto);
                var attendanceEventTypes = await attendanceEventTypeUnit.GetAsync(filter);

                foreach(var attendanceEventType in attendanceEventTypes)
                {
                    retval.Add(_mapper.Map<AttendanceEventTypeDto>(attendanceEventType));
                }
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task<AttendanceEventTypeDto> GetAttendanceEventTypeByIdAsync(int id)
        {
            AttendanceEventTypeDto retval;
            using (var attendanceEventTypeUnit = new UnitOfWork<AttendanceEventType>(new ISDbContext()))
            {
                var attendanceEventType = await attendanceEventTypeUnit.GetByIdAsync(id);

                retval = _mapper.Map<AttendanceEventTypeDto>(attendanceEventType);
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task CreateAttendanceEventTypeAsync(AttendanceEventTypeDto attendanceEventTypeDto)
        {
            using (var attendanceEventTypeUnit = new UnitOfWork<AttendanceEventType>(new ISDbContext()))
            {
                var attendanceEventType = _mapper.Map<AttendanceEventType>(attendanceEventTypeDto);
                await attendanceEventTypeUnit.CreateAsync(attendanceEventType);
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task UpdateAttendanceEventTypeAsync(AttendanceEventTypeDto attendanceEventTypeDto)
        {
            using (var attendanceEventTypeUnit = new UnitOfWork<AttendanceEventType>(new ISDbContext()))
            {
                var attendanceEventType = _mapper.Map<AttendanceEventType>(attendanceEventTypeDto);
                await attendanceEventTypeUnit.UpdateAsync(attendanceEventType);
            }
        }

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

        IMapper _mapper = AutoMapperHelper.Config.CreateMapper();
    }
}
