using System.Collections.Generic;
using System.Collections.ObjectModel;
using Database.Models;
using Database.Repository;

namespace Pos.Service
{
    public static class CacheService
    {

        private static bool _invalidEmployee = false;
        private static bool _invalidProductGroup = false;
        private static List<EmployeeEntity> _employees;
        private static List<ProductGroupEntity> _productGroups;


        public static void Invalidate()
        {
            _invalidEmployee = true;
            _invalidProductGroup = true;
        }

        public static ObservableCollection<EmployeeEntity> GetEmployee()
        {
            ObservableCollection<EmployeeEntity> obsEmployees = new ObservableCollection<EmployeeEntity>();
            if (_invalidEmployee)
            {
                EmployeeRepository employeeRepository = new EmployeeRepository();
                _employees = employeeRepository.GetAll();
                foreach (EmployeeEntity employee in _employees)
                {
                    obsEmployees.Add(employee);
                }
                _invalidEmployee = false;
            }

            return obsEmployees;    
        }

        public static ObservableCollection<ProductGroupEntity> GetProductGroupsIncludeProducts()
        {
            ObservableCollection<ProductGroupEntity> obsProductGroups = new ObservableCollection<ProductGroupEntity>();
            if(_invalidProductGroup)
            {
                ProductGroupRepository productGroupRepository = new ProductGroupRepository();
                _productGroups = productGroupRepository.GetAll();
                _invalidProductGroup = false;
                foreach (ProductGroupEntity productGroup in _productGroups)
                {
                    obsProductGroups.Add(productGroup);
                }
            }

            return obsProductGroups;
        }
    }
}
