﻿using Castle.Core.Logging;
using SOURCE.BackOffice.Web.ViewModels.Pages.Administration.Competences.Details;
using SOURCE.Domaine;
using SOURCE.Domaine.Results;
using SOURCE.Infrastructure.Constant;
using SOURCE.Infrastructure.Services.Depots;
using SOURCE.Infrastructure.Services.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SOURCE.BackOffice.Web.ViewModels.ModelBuilders.Administration.Competences
{
    public class CompetenceModelBuilder : ModelBuilderBase, ICompetenceModelBuilder
    {
        public CompetenceModelBuilder(ILogger logger, IDepotFactory depotFactory, ICaching cachingService)
            : base(logger, depotFactory, cachingService)
        { }


        public CompetencesViewModel BuildCompetencesViewModel(Context context, long id) 
        {
            if (id <= 0) 
            {
                return new CompetencesViewModel();
            }

            var competence = _depotFactory.CompetenceDepot.GetWithParent(context.User.ID, id);

            var roles = _depotFactory.CompetenceRoleDepot.Find(x => x.CompetenceID == id);

            return new CompetencesViewModel 
            {
               CompetenceID = id,
               TimeStamp = _depotFactory.GenericDepot.GetTimeStamp(context.User.ID, (long)VueEnum.AdministrationCompetencesCompetences, id),
               Competence = AutoMapper.Mapper.Map<AdministrationCompetenceResult, CompetenceViewModel>(competence),
               Roles = AutoMapper.Mapper.Map<IEnumerable<CompetenceRole>, IEnumerable<RoleViewModel>>(roles)
            };
        }

        public CompetencesLieesViewModel BuildCompetencesLieesViewModel(Context context, long id)
        {
            var competenceLiees = _depotFactory.CompetenceDepot.GetCompetenceLieesByCompetence(context.User.ID, id);

            return new CompetencesLieesViewModel 
            {
                LieCompetenceID = id,
                TimeStamp = _depotFactory.GenericDepot.GetTimeStamp(context.User.ID, (long)VueEnum.AdministrationCompetencesCompetencesLiees, id),
                CompetenceLiees = AutoMapper.Mapper.Map<IEnumerable<CompetenceLieeResult>, IEnumerable<CompetenceLieeViewModel>>(competenceLiees)
            };
        }
 
    }
}
