using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RenovationWorkContracts.BindingModels;
using RenovationWorkContracts.SearchModels;
using RenovationWorkContracts.StoragesContracts;
using RenovationWorkContracts.ViewModels;
using RenovationWorkListImplement.Models;

namespace RenovationWorkListImplement.Implements
{
    public class RepairStorage : IRepairStorage
    {
        private readonly DataListSingleton _source;
        public RepairStorage()
        {
            _source = DataListSingleton.GetInstance();
        }
        public List<RepairViewModel> GetFullList()
        {
            var result = new List<RepairViewModel>();
            foreach (var repair in _source.Repairs)
            {
                result.Add(repair.GetViewModel);
            }
            return result;
        }
        public List<RepairViewModel> GetFilteredList(RepairSearchModel model)
        {
            var result = new List<RepairViewModel>();
            if (string.IsNullOrEmpty(model.RepairName))
            {
                return result;
            }
            foreach (var repair in _source.Repairs)
            {
                if (repair.RepairName.Contains(model.RepairName))
                {
                    result.Add(repair.GetViewModel);
                }
            }
            return result;
        }
        public RepairViewModel? GetElement(RepairSearchModel model)
        {
            if (string.IsNullOrEmpty(model.RepairName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var repair in _source.Repairs)
            {
                if ((!string.IsNullOrEmpty(model.RepairName) && repair.RepairName == model.RepairName) || (model.Id.HasValue && repair.Id == model.Id))
                {
                    return repair.GetViewModel;
                }
            }
            return null;
        }
        public RepairViewModel? Insert(RepairBindingModel model)
        {
            model.Id = 1;
            foreach (var repair in _source.Repairs)
            {
                if (model.Id <= repair.Id)
                {
                    model.Id = repair.Id + 1;
                }
            }
            var newRepair = Repair.Create(model);
            if (newRepair == null)
            {
                return null;
            }
            _source.Repairs.Add(newRepair);
            return newRepair.GetViewModel;
        }
        public RepairViewModel? Update(RepairBindingModel model)
        {
            foreach (var repair in _source.Repairs)
            {
                if (repair.Id == model.Id)
                {
                    repair.Update(model);
                    return repair.GetViewModel;
                }
            }
            return null;
        }
        public RepairViewModel? Delete(RepairBindingModel model)
        {
            for (int i = 0; i < _source.Repairs.Count; ++i)
            {
                if (_source.Repairs[i].Id == model.Id)
                {
                    var element = _source.Repairs[i];
                    _source.Repairs.RemoveAt(i);
                    return element.GetViewModel;
                }
            }
            return null;
        }
    }
}

