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 StreamLogic : IStreamLogic
    {
        private readonly IStreamStorage _streamStorage;

        public StreamLogic(IStreamStorage streamStorage)
        {
            _streamStorage = streamStorage;
        }

        public bool Create(StreamBindingModel model)
        {
            CheckModel(model);
            if (_streamStorage.Insert(model) == null)
            {
                return false;
            }
            return true;
        }
        public bool Update(StreamBindingModel model)
        {
            CheckModel(model, false);
            if (_streamStorage.Update(model) == null)
            {
                return false;
            }
            return true;
        }
        public bool Delete(StreamBindingModel model)
        {
            CheckModel(model, false);
            if (_streamStorage.Delete(model) == null)
            {
                return false;
            }
            return true;
        }
        public StreamViewModel? ReadElement(StreamSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            var stream = _streamStorage.GetElement(model);
            if (stream == null)
            {
                return null;
            }
            return stream;
        }

        public List<StreamViewModel>? ReadList(StreamSearchModel? model)
        {
            var list = model == null ? _streamStorage.GetFullList() : _streamStorage.GetFilteredList(model);
            if (list == null)
            {
                return null;
            }
            return list;
        }

        private void CheckModel(StreamBindingModel 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));
            }
        }
    }
}
