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

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task<IEnumerable<AttendanceRecordDto>> GetAttendanceRecordsAsync(FilterDto filterDto)
        {
            var retval = new List<AttendanceRecordDto>();
            using (var attendanceRecordUnit = new UnitOfWork<AttendanceRecord>(new ISDbContext()))
            {
                var filter = _mapper.Map<Filter>(filterDto);
                var attendanceRecords = await attendanceRecordUnit.GetAsync(filter);

                foreach(var attendanceRecord in attendanceRecords)
                {
                    retval.Add(_mapper.Map<AttendanceRecordDto>(attendanceRecord));
                }
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task<AttendanceRecordDto> GetAttendanceRecordByIdAsync(int id)
        {
            AttendanceRecordDto retval;
            using (var attendanceRecordUnit = new UnitOfWork<AttendanceRecord>(new ISDbContext()))
            {
                var attendanceRecord = await attendanceRecordUnit.GetByIdAsync(id);

                retval = _mapper.Map<AttendanceRecordDto>(attendanceRecord);
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task CreateAttendanceRecordAsync(AttendanceRecordDto attendanceRecordDto)
        {
            using (var attendanceRecordUnit = new UnitOfWork<AttendanceRecord>(new ISDbContext()))
            {
                var attendanceRecord = _mapper.Map<AttendanceRecord>(attendanceRecordDto);
                // dont create new entities
                attendanceRecord.AttendanceEventType = null;
                attendanceRecord.Employee = null;

                await attendanceRecordUnit.CreateAsync(attendanceRecord);
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "BASIC_ACCESS")]
        public async Task UpdateAttendanceRecordAsync(AttendanceRecordDto attendanceRecordDto)
        {
            using (var attendanceRecordUnit = new UnitOfWork<AttendanceRecord>(new ISDbContext()))
            {
                var attendanceRecord = _mapper.Map<AttendanceRecord>(attendanceRecordDto);
                await attendanceRecordUnit.UpdateAsync(attendanceRecord);
            }
        }

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