using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;

namespace UniversityBusinessLogic.BusinessLogics
{
    public class StudentLogic : IStudentLogic
    {
        private readonly IStudentStorage _studentStorage;

        public StudentLogic(IStudentStorage studentStorage)
        {
            _studentStorage = studentStorage;
        }

        public bool Create(StudentBindingModel model)
        {
            CheckModel(model);
            if (_studentStorage.Insert(model) == null)
            {
                return false;
            }
            return true;
        }
        public bool Update(StudentBindingModel model)
        {
            CheckModel(model, false);
            if (_studentStorage.Update(model) == null)
            {
                return false;
            }
            return true;
        }
        public bool Delete(StudentBindingModel model)
        {
            CheckModel(model, false);
            if (_studentStorage.Delete(model) == null)
            {
                return false;
            }
            return true;
        }
        public StudentViewModel? ReadElement(StudentSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            var student = _studentStorage.GetElement(model);
            if (student == null)
            {
                return null;
            }
            return student;
        }

        public List<StudentViewModel>? ReadList(StudentSearchModel? model)
        {
            var list = model == null ? _studentStorage.GetFullList() : _studentStorage.GetFilteredList(model);
            if (list == null)
            {
                return null;
            }
            return list;
        }

        private void CheckModel(StudentBindingModel model, bool withParams = true)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (!withParams)
            {
                return;
            }
            if (string.IsNullOrEmpty(model.Name))
            {
                throw new ArgumentNullException("Нет названия документа", nameof(model.Name));
            }
            if (model.StudentCard <= 0)
            {
                throw new ArgumentNullException("Неверно указан номер студенческого билета", nameof(model.Name));
            }

            var student = _studentStorage.GetElement(new StudentSearchModel
            {
                StudentCard = model.StudentCard,
            });
            if (student != null && student.Id != model.Id)
            {
                throw new InvalidOperationException("Приказ с таким названием уже есть");
            }
        }
    }
}
