using System.Collections.Generic;
using System.Linq;
using SOURCE.Domaine;
using SOURCE.Domaine.Composite;
using SOURCE.Domaine.Results;
using System.Data.SqlClient;
using System.Data;
using System;
using SOURCE.Business.Depots;
using SOURCE.Infrastructure.Constant;
using System.Transactions;
using SOURCE.Data.Context;

namespace SOURCE.Data.Depots
{
    public partial class DepotProfil : IDepotProfil
    {
        private readonly IDepotTraductionDroit _trad_droit;
        private readonly IDepotLien_Profil_Droit _lien_profil_droit;
        private readonly IDepotTraduction _traduction;
        private readonly IDepotDroit _ddroits;
        private readonly IDepotGeneric _generic;

        public DepotProfil(ISourceDbContext dataContext,
            IDepotDroit ddroits,
            IDepotTraduction traduction,
            IDepotLien_Profil_Droit lien_profil_droit,
            IDepotGeneric generic,
           IDepotTraductionDroit trad_droit)
            : this(dataContext)
        {
            _trad_droit = trad_droit;
            _ddroits = ddroits;
            _traduction = traduction;
            _lien_profil_droit = lien_profil_droit;
            _generic = generic;
        }

    
        public Profil GetByCode(string code) 
        {
            return DbContext.Profils.Where(x => x.Code.Equals(code)).FirstOrDefault();
        }

        public IEnumerable<dynamic> Search(string code, string description)
        {
            List<SqlParameter> Parameters = new List<SqlParameter>();


            Parameters.Add(new SqlParameter("@Code", SqlDbType.VarChar) { Value = code ?? string.Empty });
            Parameters.Add(new SqlParameter("@Description", SqlDbType.VarChar) { Value = description ?? string.Empty });

            return DbContext.ExecuteStoredProcedureDynamic("sp_SearchProfils", Parameters);
        }

        public GetProfilsByIdComposite GetProfilInfosGenerales(long id, long langueID)
        {
            GetProfilsByIdComposite tousLesDroits = new GetProfilsByIdComposite();

            List<SqlParameter> Parameters = new List<SqlParameter>();

            Parameters.Add(new SqlParameter("@ProfilID", SqlDbType.BigInt){Value = id});
            Parameters.Add(new SqlParameter("@LangueID", SqlDbType.BigInt){Value = langueID});


            tousLesDroits.Droits = DbContext.ExecuteStoredProcedure<Lien_Profil_DroitDto>("sp_GetLiensProfilDroit", Parameters);
            

            var profil = GetById(id);

            tousLesDroits.Profil = AutoMapper.Mapper.Map<Profil, ProfilDto>(profil);

            return tousLesDroits;
        }

        public ProfilDto SaveProfilInfosGenerales(GetProfilsByIdComposite profilComposite)
        {
            Profil profil;

            if (profilComposite.Profil.ID > 0)
            {
                profil = GetById(profilComposite.Profil.ID);
            }
            else 
            {
                profil = new Profil();
            }

            profil.Code = profilComposite.Profil.Code;
            profil.Description = profilComposite.Profil.Description;
            profil.Niveau = profilComposite.Profil.Niveau;


            if (profilComposite.Profil.ID > 0)
            {
                Update();
            }
            else
            {
                Add(profil);
                _traduction.TraduireTable(Constants.GetStringValue(TableName.Profil));
            }

            var lienBdd = _lien_profil_droit.Find(x => x.ProfilID == profilComposite.Profil.ID).ToList();
            foreach (var r in lienBdd.Where(x => !profilComposite.Droits.Select(y => y.ID).Contains(x.ID)))
            {
                _lien_profil_droit.Delete(r);
            }

            foreach (var r in profilComposite.Droits)
            {
                Lien_Profil_Droit _cl;
                if (r.ID > 0)
                {
                    _cl = _lien_profil_droit.GetById(r.ID);
                }
                else
                {
                    _cl = new Lien_Profil_Droit();
                }

                _cl.ProfilID = profil.ID;
                _cl.RoleID = r.RoleID;
                _cl.DroitID = r.DroitID;
                _cl.OrganismeID = r.OrganismeID;


                if (r.ID > 0)
                {
                    _lien_profil_droit.Update();
                }
                else
                {
                    _lien_profil_droit.Add(_cl);
                }
            }

            return AutoMapper.Mapper.Map<Profil, ProfilDto>(profil);
        }
    }
}
