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

        [PrincipalPermission(SecurityAction.Demand, Role = "VIEW_EMPLOYEES")]
        public async Task<IEnumerable<EmployeeDto>> GetEmployeesAsync(FilterDto filterDto)
        {
            var retval = new List<EmployeeDto>();
            using (var employeeUnit = new UnitOfWork<Employee>(new ISDbContext()))
            {
                var filter = _mapper.Map<Filter>(filterDto);
                var employees = await employeeUnit.GetAsync(filter);

                foreach(var employee in employees)
                {
                    retval.Add(_mapper.Map<EmployeeDto>(employee));
                }
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "VIEW_EMPLOYEES")]
        public async Task<EmployeeDto> GetEmployeeByIdAsync(int id)
        {
            EmployeeDto retval;
            using (var employeeUnit = new UnitOfWork<Employee>(new ISDbContext()))
            {
                var employee = await employeeUnit.GetByIdAsync(id);

                retval = _mapper.Map<EmployeeDto>(employee);
            }

            return retval;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "EDIT_EMPLOYEES")]
        public async Task CreateEmployeeAsync(EmployeeDto employeeDto)
        {
            using (var employeeUnit = new UnitOfWork<Employee>(new ISDbContext()))
            {
                var employee = _mapper.Map<Employee>(employeeDto);
                //dont create connected entities
                employee.Department = null;
                employee.EmployeePosition = null;

                await employeeUnit.CreateAsync(employee);
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "EDIT_EMPLOYEES")]
        public async Task UpdateEmployeeAsync(EmployeeDto employeeDto)
        {
            using (var employeeUnit = new UnitOfWork<Employee>(new ISDbContext()))
            {
                var employee = _mapper.Map<Employee>(employeeDto);
                await employeeUnit.UpdateAsync(employee);
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "EDIT_EMPLOYEES")]
        public async Task DeleteEmployeeAsync(int id)
        {
            using (var employeeUnit = new UnitOfWork<Employee>(new ISDbContext()))
            {
                await employeeUnit.DeleteAsync(id);
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "VIEW_EMPLOYEES")]
        public void CheckAccessViewEmployees()
        {
            return;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "EDIT_EMPLOYEES")]
        public void CheckAccessEditEmployees()
        {
            return;
        }
    }
}
