using System;
using System.Web.Mvc;
using System.Web.Routing;
using System.Configuration;

namespace adMoto.Payments.Web.Helpers
{
    public static class WebHelper
    {
        public static string ImageTag(this HtmlHelper helper, string src, string alt)
        {
            var tb = new TagBuilder("img");
            tb.Attributes.Add("src", helper.Encode(src));
            tb.Attributes.Add("alt", helper.Encode(alt));
            return tb.ToString(TagRenderMode.SelfClosing);
        }
        public static string ImageLink(this HtmlHelper htmlHelper, string filename, string alt, string querystring)
        {
            
            var action = htmlHelper.CurrentAction();
            var controller = htmlHelper.CurrentController();
                                    
            var urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
            var src = urlHelper.UrlImage(filename);
            var imgtag = htmlHelper.ImageTag(src, alt);
            
            var tab = new RouteValueDictionary();

            var id = "";
            if (htmlHelper.ViewContext.RouteData.Values["id"] != null) id = htmlHelper.ViewContext.RouteData.Values["id"].ToString();
            tab.Add("language", alt);
            if (id != null) tab.Add("id", id);

            var url = urlHelper.Action(action, controller, tab);

            var param = querystring.Split(("&").ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            for (var i = 0; i < param.Length; i++ )
            {
                if (i == 0 ) url += "?" + param[i];
                else url += "&" + param[i];
            }

            var link = new TagBuilder("a");
            link.Attributes.Add("href", url);
            link.InnerHtml = imgtag;
            return link.ToString();
        }
        public static string CurrentController(this HtmlHelper htmlHelper)
        {
            return htmlHelper.ViewContext.RouteData.Values["controller"].ToString();
        }
        public static string CurrentAction(this HtmlHelper htmlHelper)
        {
            return htmlHelper.ViewContext.RouteData.Values["action"].ToString();
        }  
        public static string Home(this UrlHelper urlHelper)
        {
            return urlHelper.Content("~/");
        }
        public static string Css(this UrlHelper urlHelper, string fileName)
        {
            var url = urlHelper.Home() + "Content/" + fileName;
            return urlHelper.Content(url);
        }
        public static string UrlImage(this UrlHelper urlHelper, string fileName)
        {
            var url = urlHelper.Home() + "Images/" + fileName;            
            return urlHelper.Content(url);
        }
        public static string FileUrl(this UrlHelper urlHelper, string fileName)
        {
            var url = urlHelper.Home() + "Images/";
            switch (ConfigurationManager.AppSettings["Css"])
            {
                case "truck":
                    url += "truck/";
                    break;
                case "admoto":
                    url += "admoto/";
                    break;
            }
            url += fileName;
            return urlHelper.Content(url);
        }
        public static string MainCss(this HtmlHelper htmlHelper, string filename)
        {
            var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
            var url = urlHelper.Css(filename);
            var strCss = htmlHelper.LinkTag(url, "stylesheet", "text/css");
            return strCss;
        }
        public static string LinkTag(this HtmlHelper htmlHelper, string url, string rel, string type)
        {
            var linkcss = new TagBuilder("link");
            linkcss.Attributes.Add("href", url);
            linkcss.Attributes.Add("rel", rel);
            if (type != "" ) linkcss.Attributes.Add("type", type);
            return linkcss.ToString(TagRenderMode.SelfClosing);
        }
        public static string AddCssToPortal(this HtmlHelper htmlHelper)
        {
            var filename = "";
            switch (ConfigurationManager.AppSettings["Css"])
            {
                case "truck":
                    filename = "truck.css" ;
                    break;
                case "admoto":
                    filename = "admoto.css";
                    break;
            }
            
            var css = htmlHelper.MainCss(filename);
            return css;
        }
        
        public static string Logo(this HtmlHelper helper)
        {
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
            
            var logo = ConfigurationManager.AppSettings["Css"] == "admoto" ? "logo.png" : "logo.gif";

            var url = urlHelper.FileUrl(logo);
            var logotag = helper.ImageTag(url, "logo");
           
            var tb = new TagBuilder("td");
            tb.Attributes.Add("class", "logo");

            tb.InnerHtml = logotag; 
            return tb.ToString();
        }
    }
}