﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.AspNetCore.Html;
using GenderPayGap.WebUI.Models.Shared;
using Microsoft.AspNetCore.Mvc;

namespace GenderPayGap.WebUI.Classes.TagHelpers
{
    [HtmlTargetElement("Pagination", TagStructure = TagStructure.NormalOrSelfClosing)]
    [RestrictChildren("Next","Back")]
    public class PaginationTagHelper : TagHelper
    {
        public PaginationTagHelper(IHtmlHelper htmlHelper)
        {
            _htmlHelper = htmlHelper;
        }

        [ViewContext]
        [HtmlAttributeNotBound]
        public ViewContext ViewContext { get; set; }

        private IHtmlHelper _htmlHelper;

        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            //Create a context for the accordion
            var accordionContext = new PaginationContext();
            context.Items.Add(typeof(PaginationContext), accordionContext);

            //Execute the sections to populate 
            await output.GetChildContentAsync();

            (_htmlHelper as IViewContextAware).Contextualize(ViewContext);

            var content = await _htmlHelper.PartialModelAsync(new Pagination(accordionContext.Pages.ToArray()));
            
            output.Content.SetHtmlContent(content);
            output.TagName = null;
        }
    }

    [HtmlTargetElement("Back", ParentTag = "Pagination", TagStructure = TagStructure.WithoutEndTag)]
    public class PaginationBackTagHelper : TagHelper
    {
        public PaginationBackTagHelper(IUrlHelper urlHelper)
        {
            _urlHelper = urlHelper;
        }
        private IUrlHelper _urlHelper;

        [ViewContext]
        [HtmlAttributeNotBound]
        public ViewContext ViewContext { get; set; }

        public string Title { get; set; }
        public string Label { get; set; }

        public string Action { get; set; }
        public string Controller { get; set; }

        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var htmlContent = await output.GetChildContentAsync();
            var accordionContext = (PaginationContext)context.Items[typeof(PaginationContext)];

            if (string.IsNullOrWhiteSpace(Controller)) Controller = ViewContext?.RouteData?.Values["Controller"]?.ToString();
            accordionContext.Pages.Add(new PaginationLink(_urlHelper.Action(Action, Controller), Title ,Label, true));

            output.SuppressOutput();
        }
    }

    [HtmlTargetElement("Next", ParentTag = "Pagination", TagStructure = TagStructure.WithoutEndTag)]
    public class PaginationNextTagHelper : TagHelper
    {
        public PaginationNextTagHelper(IUrlHelper urlHelper)
        {
            _urlHelper = urlHelper;
        }
        private IUrlHelper _urlHelper;
        [ViewContext]
        [HtmlAttributeNotBound]
        public ViewContext ViewContext { get; set; }

        public string Title { get; set; }
        public string Label { get; set; }
        public string Action { get; set; }
        public string Controller { get; set; }

        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var htmlContent = await output.GetChildContentAsync();
            var accordionContext = (PaginationContext)context.Items[typeof(PaginationContext)];
            if (string.IsNullOrWhiteSpace(Controller)) Controller = ViewContext?.RouteData?.Values["Controller"]?.ToString();
            accordionContext.Pages.Add(new PaginationLink(_urlHelper.Action(Action, Controller), Title,Label, false));
            output.SuppressOutput();
        }
    }


    public class PaginationContext
    {
        public List<PaginationLink> Pages = new List<PaginationLink>();
    }
}
