using ShipyardContracts.BindingModels;
using ShipyardContracts.SearchModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using ShipyardListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShipyardListImplement.Implements
{
    public class ShipStorage:IShipStorage
    {
        private readonly DataListSingleton _source;
        public ShipStorage()
        {
            _source = DataListSingleton.GetInstance();
        }
        public List<ShipViewModel> GetFullList()
        {
            var result = new List<ShipViewModel>();
            foreach (var ship in _source.Ships)
            {
                result.Add(ship.GetViewModel);
            }
            return result;
        }
        public List<ShipViewModel> GetFilteredList(ShipSearchModel model)
        {
            var result = new List<ShipViewModel>();
            if (string.IsNullOrEmpty(model.ShipName))
            {
                return result;
            }
            foreach (var ship in _source.Ships)
            {
                if (ship.ShipName.Contains(model.ShipName))
                {
                    result.Add(ship.GetViewModel);
                }
            }
            return result;
        }
        public ShipViewModel? GetElement(ShipSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ShipName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var ship in _source.Ships)
            {
                if ((!string.IsNullOrEmpty(model.ShipName) &&
               ship.ShipName == model.ShipName) ||
                (model.Id.HasValue && ship.Id == model.Id))
                {
                    return ship.GetViewModel;
                }
            }
            return null;
        }
        public ShipViewModel? Insert(ShipBindingModel model)
        {
            model.Id = 1;
            foreach (var ship in _source.Ships)
            {
                if (model.Id <= ship.Id)
                {
                    model.Id = ship.Id + 1;
                }
            }
            var newShip = Ship.Create(model);
            if (newShip == null)
            {
                return null;
            }
            _source.Ships.Add(newShip);
            return newShip.GetViewModel;
        }
        public ShipViewModel? Update(ShipBindingModel model)
        {
            foreach (var ship in _source.Ships)
            {
                if (ship.Id == model.Id)
                {
                    ship.Update(model);
                    return ship.GetViewModel;
                }
            }
            return null;
        }
        public ShipViewModel? Delete(ShipBindingModel model)
        {
            for (int i = 0; i < _source.Ships.Count; ++i)
            {
                if (_source.Ships[i].Id == model.Id)
                {
                    var element = _source.Ships[i];
                    _source.Ships.RemoveAt(i);
                    return element.GetViewModel;
                }
            }
            return null;
        }
    }
}
