﻿using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace AlaCarte.WebUI.Common
{
    public static class Extensions
    {
        public static List<KeyValuePair<string, string>> ToList(this NameValueCollection collection)
        {
            return collection.Cast<string>().Select(e => new KeyValuePair<string, string>(e, collection[e])).ToList();
        }

        public static MvcHtmlString Button(this HtmlHelper helper,
                                     string innerHtml,
                                     object htmlAttributes)
        {
            return Button(helper, innerHtml,
                          HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
            );
        }

        public static MvcHtmlString Button(this HtmlHelper helper,
                                           string innerHtml,
                                           IDictionary<string, object> htmlAttributes)
        {
            var builder = new TagBuilder("button");
            builder.InnerHtml = innerHtml;
            builder.MergeAttributes(htmlAttributes);
            return MvcHtmlString.Create(builder.ToString());
        }

        public static MvcForm BeginAlaCarteForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes)
        {
            return htmlHelper.BeginForm(actionName, controllerName, new { EmployeeID = htmlHelper.ViewBag.EmployeeID, CCApp = htmlHelper.ViewBag.CCApp }, method, htmlAttributes);
        }
    }
}