﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;
using ESchool.Models;
using Orchard.Data;

namespace ESchool.Services
{
    public class CourseMenuService : ICourseMenuService
    {
        private readonly IContentManager contentManager;
        private readonly IRepository<PostRecord> postRepo;

        public CourseMenuService(IContentManager contentManager, IRepository<PostRecord> postRepo)
        {
            this.contentManager = contentManager;
            this.postRepo = postRepo;
        }

        public CourseMenuPart Get(int idMenu)
        {
            return contentManager.Query<CourseMenuPart, CourseMenuRecord>(VersionOptions.Latest).List().FirstOrDefault(x => x.Id == idMenu);
        }
        public CourseMenuPart GetMenuFor(int idContent)
        {
            var post = postRepo.Table.FirstOrDefault(x => x.ContentId == idContent);
            if (post == null)
                return null;
            return Get(post.Menu.Id);
        }
        public IEnumerable<CourseMenuPart> GetMenusOfCourse(int idCourse)
        {
            return contentManager.Query<CourseMenuPart, CourseMenuRecord>(VersionOptions.Latest).Where(x => x.Course.Id == idCourse).List();           
        }
    }
}