using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GreenTree.Nachtragsmanagement.Services.Configuration;
using GreenTree.Nachtragsmanagement.Services.Test;
using GreenTree.Nachtragsmanagement.Services.User;
using GreenTree.Nachtragsmanagement.Web.Framework;
using Newtonsoft.Json;
using GreenTree.Nachtragsmanagement.Core.Plugins;
using GreenTree.Nachtragsmanagement.Core;
using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
using GreenTree.Nachtragsmanagement.Core.Authentication;
using GreenTree.Nachtragsmanagement.Web.Models.Home;
using GreenTree.Nachtragsmanagement.Core.Domain.User;

namespace GreenTree.Nachtragsmanagement.Web.Controllers
{
    [FunctionAuthorize(false)]
    public class HomeController : Controller
    {
        private readonly IDbRelationService _dbRelationService;
        private readonly IConfigurationService _configurationService;
        private readonly IUserService _userService;
        private readonly IUserHelper _userHelper;
        private readonly IWebHelper _webHelper;

        public HomeController(
            IDbRelationService dbRelationService,
            IConfigurationService configurationService,
            IUserService userService,
            IUserHelper userHelper,
            IWebHelper webHelper)
        {
            _dbRelationService = dbRelationService;
            _configurationService = configurationService;
            _userService = userService;
            _userHelper = userHelper;
            _webHelper = webHelper;
        }

        // GET: Home
        public ActionResult Index()
        {
            var cookieUser = _userHelper.FromCookiesOrSession();
            var dbUser = _userService.GetUserById(cookieUser.Id);

            if (dbUser == null) return new RedirectResult("~/login");

            if (dbUser.CurrentRole == null || (dbUser.CurrentRole != null && dbUser.CurrentRole.Id != cookieUser.CurrentRole.Id))
                dbUser.CurrentRole = cookieUser.CurrentRole;

            var userFunctions = dbUser.CurrentRole.Functions
                .Where(f => f.IsMenuMember.HasValue && f.IsMenuMember.Value)
                .Distinct();

            var groups = userFunctions
                .GroupBy(g => g.GroupName)
                .Select(g => g.Key);

            var dict = new Dictionary<Function, List<Function>>();

            foreach (var group in groups)
            {
                var keyFunction = userFunctions
                    .FirstOrDefault(f => f.Name == group);

                if (keyFunction == null)
                    continue;

                var subFunctions = userFunctions
                    .Where(f => f.GroupName == group)
                    .ToList();

                dict.Add(keyFunction, subFunctions);
            }

            var model = new HomeModel
            {
                AvailableFunctions = dict
            };

            return View("~/Views/Home/Index.cshtml", model);
        }
    }
}