using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElectronicsShopBusinessLogic.BusinessLogic {
    public class EmployeeLogic : IEmployeeLogic {
        private readonly ILogger _logger;
        private readonly IEmployeeStorage _storage;

        public EmployeeLogic(ILogger<EmployeeLogic> loger, IEmployeeStorage storage) {
            _logger = loger;
            _storage = storage;
        }

        public bool Create(EmployeeBindingModel model) {
            CheckModel(model);
            if (_storage.Insert(model) == null) {
                _logger.LogWarning("Create operation failed");
                return false;
            }
            return true;
        }

        public bool Delete(EmployeeBindingModel model) {
            CheckModel(model, false);
            _logger.LogInformation($"Delete.ID:{model.ID}");
            if (_storage.Delete(model) == null) {
                _logger.LogWarning("Delete operation failed");
                return false;
            }
            return true;
        }

        public EmployeeViewModel? ReadElement(EmployeeSearchModel model) {
            if (model == null) {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation($"ReadElement.Login:{model.Login}.ID:{model.ID}");
            var element = _storage.GetElement(model);
            if (element == null) {
                _logger.LogWarning("ReadElement. element not fount");
                return null;
            }
            _logger.LogInformation($"ReadElement.find.ID:{element.ID}");
            return element;
        }

        public List<EmployeeViewModel>? ReadList(EmployeeSearchModel model) {
            _logger.LogInformation($"ReadList. ClientID:{model?.ID}");
            var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ;
            if (list == null) {
                _logger.LogWarning("ReadList. return null list");
                return null;
            }
            _logger.LogInformation($"ReadList.Count:{list.Count}");
            return list;
        }

        public bool Update(EmployeeBindingModel model) {
            CheckModel(model);
            if (_storage.Update(model) == null) {
                _logger.LogWarning("Update operation failed");
                return false;
            }
            return true;
        }
        private void CheckModel(EmployeeBindingModel model, bool withParams = true) {
            if (model == null) {
                throw new ArgumentNullException(nameof(model));
            }
            if (!withParams) {
                return;
            }
            if (string.IsNullOrEmpty(model.Login)) {
                throw new ArgumentNullException("Нет логина сотрудника", nameof(model.Login));
            }
            if (string.IsNullOrEmpty(model.EmployeeFIO)) {
                throw new ArgumentNullException("Нет ФИО  сотрудника", nameof(model.EmployeeFIO));
            }
            if (string.IsNullOrEmpty(model.Password)) {
                throw new ArgumentNullException("Нет пароля  сотрудника", nameof(model.Password));
            }
            _logger.LogInformation($"Client. ID:{model.ID}.ClientFIO:{model.EmployeeFIO}.Password:{model.Password}.Email:{model.Login}.");

            var element = _storage.GetElement(new EmployeeSearchModel {
                Login = model.Login
            });
            if (element != null && element.Login == model.Login) {
                throw new InvalidOperationException("Сотрудник с таким логином уже есть");
            }
        }

    }
}