using DressAtelierContracts.BindingModels;
using DressAtelierContracts.SearchModels;
using DressAtelierContracts.StorageContracts;
using DressAtelierContracts.ViewModels;
using DressAtelierListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DressAtelierListImplement.Implements
{
    public class EmployeeStorage : IEmployeeStorage
    {
        private readonly DataListSingleton _source;

        public EmployeeStorage()
        {
            _source = DataListSingleton.GetInstance();
        }

        public EmployeeViewModel? Insert(EmployeeBindingModel model)
        {
            model.ID = 1;
            foreach (var employee in _source.Employees)
            {
                if (model.ID <= employee.ID)
                {
                    model.ID = employee.ID + 1;
                }
            }

            var newEmployee = Employee.Create(model);

            if (newEmployee == null)
            { 
                return null; 
            }
            _source.Employees.Add(newEmployee);
            return newEmployee.GetViewModel;
        }

        public EmployeeViewModel? Update(EmployeeBindingModel model)
        {
            foreach (var employee in _source.Employees)
            {
                if (employee.ID == model.ID)
                {
                    employee.Update(model);
                    return employee.GetViewModel;
                }
            }
            return null;
        }

        public EmployeeViewModel? Delete(EmployeeBindingModel model)
        {
            for (int i = 0; i < _source.Employees.Count; i++)
            {
                if (model.ID == _source.Employees[i].ID)
                {
                    var element = _source.Employees[i];
                    _source.Employees.RemoveAt(i);
                    return element.GetViewModel;
                }
            }
            return null;
        }

        public EmployeeViewModel? GetElement(EmployeeSearchModel model)
        {
            if (string.IsNullOrEmpty(model.FullName) && !model.ID.HasValue)
            {
                return null;
            }

            foreach (var employee in _source.Employees)
            {
                if ((!string.IsNullOrEmpty(model.FullName) && model.FullName == employee.FullName) || (model.ID.HasValue && model.ID.Value == employee.ID))
                {
                    return employee.GetViewModel;
                }
            }
            return null;
        }

        public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
        {
            var result = new List<EmployeeViewModel>();
            if (string.IsNullOrEmpty(model.FullName))
            {
                return result;
            }
            foreach (var employee in _source.Employees)
            {
                if (employee.FullName.Contains(model.FullName))
                {
                    result.Add(employee.GetViewModel);
                }
            }
            return result;
        }

        public List<EmployeeViewModel> GetFullList()
        {
            var result = new List<EmployeeViewModel>();
            foreach (var employee in _source.Employees)
            {
                result.Add(employee.GetViewModel);
            }
            return result;
        }
    }
}
