using RenovationWorkContracts.BindingModels;
using RenovationWorkContracts.SeatchModels;
using RenovationWorkContracts.StorageContracts;
using RenovationWorkContracts.ViewModels;
using RenovationWorkListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RenovationWorkListImplement.Implements
{
    public class RepairStorage : IRepairStorage
    {
        private readonly DataListSingleton _source;

        public RepairStorage()
        {
            _source = DataListSingleton.GetInstance();
        }
        public RepairViewModel? Delete(RepairBindingModel model)
        {
            for (int i = 0; i < _source.Repair.Count; ++i)
            {
                if (_source.Repair[i].Id == model.Id)
                {
                    var element = _source.Repair[i];
                    _source.Repair.RemoveAt(i);
                    return element.GetViewModel;
                }
            }

            return null;
        }
        public RepairViewModel? GetElement(RepairSearchModel model)
        {
            if (string.IsNullOrEmpty(model.RepairName) && !model.Id.HasValue)
            {
                return null;
            }

            foreach (var repair in _source.Repair)
            {
                if ((!string.IsNullOrEmpty(model.RepairName) && repair.RepairName == model.RepairName) || (model.Id.HasValue && repair.Id == model.Id))
                {
                    return repair.GetViewModel;
                }
            }

            return null;
        }
        public List<RepairViewModel> GetFilteredList(RepairSearchModel model)
        {
            var result = new List<RepairViewModel>();

            if (string.IsNullOrEmpty(model.RepairName))
            {
                return result;
            }

            foreach (var reinforced in _source.Repair)
            {
                if (reinforced.RepairName.Contains(model.RepairName))
                {
                    result.Add(reinforced.GetViewModel);
                }
            }

            return result;
        }

        public List<RepairViewModel> GetFullList()
        {
            var result = new List<RepairViewModel>();

            foreach (var repair in _source.Repair)
            {
                result.Add(repair.GetViewModel);
            }

            return result;
        }

        public RepairViewModel? Insert(RepairBindingModel model)
        {
            model.Id = 1;

            foreach (var repair in _source.Repair)
            {
                if (model.Id <= repair.Id)
                {
                    model.Id = repair.Id + 1;
                }
            }

            var newRepair = Repair.Create(model);

            if (newRepair == null)
            {
                return null;
            }

            _source.Repair.Add(newRepair);

            return newRepair.GetViewModel;
        }

        public RepairViewModel? Update(RepairBindingModel model)
        {
            foreach (var repair in _source.Repair)
            {
                if (repair.Id == model.Id)
                {
                    repair.Update(model);
                    return repair.GetViewModel;
                }
            }

            return null;
        }
    }
}
