using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GreenTree.Nachtragsmanagement.Core.Data;

namespace GreenTree.Nachtragsmanagement.Services.Site
{
    public class SiteService : ISiteService
    {
        #region Fields

        private readonly IRepository<Core.Domain.Site.Site> _siteRepository;

        #endregion

        #region Ctor

        /// <summary>
        /// Initializes a new instance of the SiteService class
        /// </summary>
        public SiteService(
            IRepository<Core.Domain.Site.Site> siteRepository)
        {
            _siteRepository = siteRepository;
        }

        #endregion

        #region Site

        /// <summary>
        /// Gets all sites
        /// </summary>
        public IList<Core.Domain.Site.Site> GetAllSites()
        {
            return _siteRepository.Table.ToList();
        }

        /// <summary>
        /// Gets all sites where the user is assigned to if the current role only allows assigned sites
        /// </summary>
        public IList<Core.Domain.Site.Site> GetAllUserAssignedSites(Core.Domain.User.User user)
        {
            if (user == null || (user != null && user.CurrentRole == null))
                return new List<Core.Domain.Site.Site>();

            if (user.CurrentRole.SeeOnlyAssigned)
            {
                return
                    _siteRepository.Table
                        .Where(s => s.Users
                            .Select(u => u.Id).Contains(user.Id))
                        .ToList();
            }
            else
                return GetAllSites();
                           
        }

        /// <summary>
        /// Gets a site by specified Id
        /// </summary>
        /// <param name="id">Site identifier.</param>
        public Core.Domain.Site.Site GetSiteById(int id)
        {
            return _siteRepository.GetById(id);
        }

        /// <summary>
        /// Gets all sites to the specified ids
        /// </summary>
        public IList<Core.Domain.Site.Site> GetSitesByIds(int[] ids)
        {
            return _siteRepository.Table
                .Where(u => ids.Contains(u.Id))
                .ToList();
        }

        /// <summary>
        /// Gets a site by specified customer number
        /// </summary>
        /// <param name="customNumber">Customer number.</param>
        public Core.Domain.Site.Site GetSiteByCustomNumber(string customNumber)
        {
            return _siteRepository
                .Table.FirstOrDefault(u => u.CustomNumber == customNumber);
        }

        /// <summary>
        /// Insert a site
        /// </summary>
        /// <param name="site">Site.</param>
        public void InsertSite(Core.Domain.Site.Site site)
        {
            _siteRepository.Insert(site);
        }

        /// <summary>
        /// Update a site
        /// </summary>
        /// <param name="site">Site.</param>
        public void UpdateSite(Core.Domain.Site.Site site)
        {
            _siteRepository.Update(site);
        }

        /// <summary>
        /// Delete a site
        /// </summary>
        /// <param name="site">Site.</param>
        public void DeleteSite(Core.Domain.Site.Site site)
        {
            _siteRepository.Delete(site);
        }

        #endregion
    }
}
