﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Cresce.WebApp.Views.HtmlHelpers.HtmlModels;

namespace Cresce.WebApp.Views.HtmlHelpers
{
    public static class FormsHelper
    {
        private static readonly HtmlInputBuilders Builders = new HtmlInputBuilders();

        private static void AddCssClass(this TagBuilder tagBuilder, params string[] classes)
        {
            foreach (var cssClass in classes)
            {
                tagBuilder.AddCssClass(cssClass);
            }
        }

        private static void AddAllColumnSizes(this TagBuilder tagBuilder, int columnSize)
        {
            var cssClasses = new[] { "col-md-", "col-sm-", "col-xs-" };

            var classes = cssClasses
                .Reverse()
                .Select(cssClass => cssClass + columnSize)
                .ToArray();

            tagBuilder.AddCssClass(classes);
        }


        public static MvcHtmlString RadioButtonWithImage(string id, string name, string field)
        {
            var html = new ResourceWithImageHtmlRadioButton(id, name, field).ToString();
            return MvcHtmlString.Create(html);
        }

        public static MvcHtmlString RadioButton(string id, string name, string field)
        {
            var html = new ResourceHtmlRadioButton(id, name, field).ToString();
            return MvcHtmlString.Create(html);
        }

        public static MvcHtmlString RadioButtons<T>(IEnumerable<T> data, string field)
        {
            var div = new TagBuilder("div")
            {
                InnerHtml = MakeRadioButtons(data, Builders.Get<T>(), field)
            };

            div.MergeAttribute("data-toggle", "buttons");
            div.AddAllColumnSizes(12);
            div.AddCssClass("btn-group");

            var htmlString = div.ToString();

            return MvcHtmlString.Create(htmlString);
        }

        private static string MakeRadioButtons<T>(IEnumerable<T> data, IHtmlInputBuilder<T> builder, string field)
        {
            return data.Select(element => builder.Build(element, field))
                .Aggregate(new StringBuilder(), (sb, elemHtml) => sb.AppendLine(elemHtml.ToString()))
                .ToString();
        }

        public static MvcHtmlString MakeNavBar(MenuType menu = 0)
        {
            var ul = new TagBuilder("ul")
            {
                InnerHtml = MakeNavBarMenu(menu)
            };

            ul.AddCssClass("nav nav-pills nav-justified");

            return new MvcHtmlString(ul.ToString());
        }

        private static string MakeNavBarMenu(MenuType menu)
        {
            switch (menu)
            {
                case MenuType.Evaluations: return MakeNavBarItemsForEvaluations();
                default: return MakeNavBarItemsForSessions();
            }
        }

        private static string MakeNavBarItemsForSessions()
        {
            var items = new[]
            {
                MakeNavBarItem("Nova Sessão", "/"),
                MakeNavBarItem("Consultar Sessões", "/Appointments/TodaysAppointments"),
                MakeNavBarItem("Criar Contrato", "/Contract"),
                MakeNavBarItem("Consultar Contratos", "/Contract/GetAllContracts"),
            };

            return items
                .Aggregate(new StringBuilder(), (builder, item) => builder.AppendLine(item))
                .ToString();
        }

        private static string MakeNavBarItemsForEvaluations()
        {
            var items = new[]
            {
                MakeNavBarItem("Nova Avaliação", "/Goals/QualityAssurance"),
                MakeNavBarItem("Objectivos", "/Goals/Goals"),
                MakeNavBarItem("Avaliações", "/Goals/Evaluation"),
            };

            return items
                .Aggregate(new StringBuilder(), (builder, item) => builder.AppendLine(item))
                .ToString();
        }

        private static string MakeNavBarItem(string label, string uri)
        {
            var anchor = new TagBuilder("a");
            anchor.MergeAttribute("href", uri);
            anchor.SetInnerText(label);

            var li = new TagBuilder("li")
            {
                InnerHtml = anchor.ToString()
            };

            li.MergeAttribute("role", "presentation");

            if (GetCurrentWebsiteRoot().EndsWith(uri))
            {
                li.AddCssClass("active");
            }
            return li.ToString();
        }

        public static MvcHtmlString MakeButton(string label, string uri)
        {
            if (GetCurrentWebsiteRoot().EndsWith(uri))
            {
                return MakeActiveButton(label, uri);
            }

            return MakeDefaultButton(label, uri);
        }

        private static MvcHtmlString MakeDefaultButton(string label, string uri)
        {
            var anchor = new TagBuilder("a");
            anchor.AddCssClass("btn-lg");
            anchor.AddCssClass("btn-default");
            anchor.AddCssClass("btn");

            anchor.MergeAttribute("role", "button");
            anchor.MergeAttribute("href", uri);
            anchor.SetInnerText(label);

            var htmlString = anchor.ToString();

            return MvcHtmlString.Create(htmlString);
        }

        public static string GetCurrentWebsiteRoot()
        {
            return HttpContext.Current.Request.Url.AbsolutePath;
        }

        public static MvcHtmlString MakeActiveButton(string label, string uri)
        {
            var anchor = new TagBuilder("a");
            anchor.AddCssClass("active");
            anchor.AddCssClass("btn-lg");
            anchor.AddCssClass("btn-primary");
            anchor.AddCssClass("btn");

            anchor.MergeAttribute("role", "button");
            anchor.MergeAttribute("href", uri);
            anchor.SetInnerText(label);

            var htmlString = anchor.ToString();

            return MvcHtmlString.Create(htmlString);
        }
    }

    public enum MenuType
    {
        Sessions = 0,
        Evaluations = 1
    }
}