using AutoMapper;
using DataAccessLayer;
using DataAccessLayer.Entities;
using DataAccessLayer.Filtering;
using DataAccessLayer.Units;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
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 : IEmployeePositionService
    {
        [PrincipalPermission(SecurityAction.Demand, Role = "VIEW_POSITIONS")]
        public async Task<IEnumerable<EmployeePositionDto>> GetEmployeePositionsAsync(FilterDto filterDto)
        {
            var retval = new List<EmployeePositionDto>();
            using (var employeePositionUnit = new UnitOfWork<EmployeePosition>(new ISDbContext()))
            {
                var filter = _mapper.Map<Filter>(filterDto);
                var employeePositions = await employeePositionUnit.GetAsync(filter);

                foreach(var employeePosition in employeePositions)
                {
                    retval.Add(_mapper.Map<EmployeePositionDto>(employeePosition));
                }
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "VIEW_POSITIONS")]
        public async Task<EmployeePositionDto> GetEmployeePositionByIdAsync(int id)
        {
            EmployeePositionDto retval;
            using (var employeePositionUnit = new UnitOfWork<EmployeePosition>(new ISDbContext()))
            {
                var employeePosition = await employeePositionUnit.GetByIdAsync(id);

                retval = _mapper.Map<EmployeePositionDto>(employeePosition);
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "EDIT_POSITIONS")]
        public async Task CreateEmployeePositionAsync(EmployeePositionDto employeePositionDto)
        {
            using (var employeePositionUnit = new UnitOfWork<EmployeePosition>(new ISDbContext()))
            {
                var employeePosition = _mapper.Map<EmployeePosition>(employeePositionDto);
                await employeePositionUnit.CreateAsync(employeePosition);
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "EDIT_POSITIONS")]
        public async Task UpdateEmployeePositionAsync(EmployeePositionDto employeePositionDto)
        {
            using (var employeePositionUnit = new UnitOfWork<EmployeePosition>(new ISDbContext()))
            {
                var employeePosition = _mapper.Map<EmployeePosition>(employeePositionDto);
                await employeePositionUnit.UpdateAsync(employeePosition);
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "EDIT_POSITIONS")]
        public async Task DeleteEmployeePositionAsync(int id)
        {
            using (var employeePositionUnit = new UnitOfWork<EmployeePosition>(new ISDbContext()))
            {
                var filter = new FilterDto()
                {
                    Conditions = new List<ConditionItemDto>()
                     {
                          new ConditionItemDto()
                          {
                               Column = nameof(EmployeeDto.EmployeePositionId),
                               Type = ConditionTypeEnum.Equal,
                               Value = id.ToString()
                          }
                     }
                };

                var positionEmployees = await GetEmployeesAsync(filter);

                if (positionEmployees != null && positionEmployees.Any())
                    throw new ValidationException();

                await employeePositionUnit.DeleteAsync(id);
            }
        }

    }
}
