﻿using SOURCE.Business.Depots;
using SOURCE.Data.Context;
using SOURCE.Domaine;
using SOURCE.Domaine.Composite;
using SOURCE.Domaine.Results;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace SOURCE.Data.Depots
{
    public partial class DepotIntervenantCertification : IDepotIntervenantCertification
    {
        private readonly IDepotIntervenantRole _roles;
        private readonly IDepotIntervenant _intervenants;

        public DepotIntervenantCertification(ISourceDbContext dataContext,
            IDepotIntervenantRole roles,
            IDepotIntervenant intervenants
            )
            : this(dataContext)
        {
            _roles = roles;
            _intervenants = intervenants;
        }


        public IEnumerable<IntervenantCertificationResult> GetIntervenantCertificationsByIntervenant(long utilisateurID, long intervenantID) 
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@UtilisateurID", SqlDbType.BigInt) { Value = utilisateurID});
            parameters.Add(new SqlParameter("@IntervenantID", SqlDbType.BigInt) { Value = intervenantID });

            return DbContext.ExecuteStoredProcedure<IntervenantCertificationResult>("sp_GetIntervenantCertificationByIntervenant", parameters);
        }

        public void SaveInfosGenerales(IntervenantCertificationInfosGeneralesComposite composite) 
        {
            //Role
            Intervenant _intervenant = _intervenants.GetById(composite.IntervenantID);

            _intervenant.CommentaireCertifications = composite.Commentaire;

            _intervenants.Update();

            //Certifications
            var certifsBdd = this.Find(x => x.IntervenantID == composite.IntervenantID).ToList();
            foreach (var c in certifsBdd.Where(x => !composite.Prestations.Select(y => y.ID).Contains(x.ID))) 
            {
                this.Delete(c);
            }

            foreach (var c in composite.Prestations) 
            {
                IntervenantCertification certification;

                if (c.ID > 0)
                {
                    certification = GetById(c.ID);
                }
                else 
                {
                    certification = new IntervenantCertification();
                }

                certification.IntervenantID = composite.IntervenantID;
                certification.CompetenceID = c.CompetenceID;
                certification.Organisme = c.Organisme;
                certification.DateInitiale = c.DateInitiale;
                certification.DateRenouvellement = c.DateRenouvellement;
                certification.DateEcheance = c.DateEcheance;
                certification.EtatCompetenceID = c.EtatCompetenceID;
                certification.Archive = c.Archive;
                certification.IsEditable = c.IsEditable;
                certification.TypeCertificationID = c.TypeCertificationID;
                if (c.ID > 0)
                {
                    Update();
                }
                else
                {
                    Add(certification);
                }
            }
        }
    }
}
