using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Configuration;

namespace Platnosci.Helpers
{
    public static class helper
    {
        public static string ImageTag(this HtmlHelper helper, string src, string alt)
        {
            TagBuilder 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();
                                    
            UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
            string src = urlHelper.UrlImage(filename);
            string imgtag = htmlHelper.ImageTag(src, alt);
            
            RouteValueDictionary tab = new RouteValueDictionary();

            string 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);

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

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

            TagBuilder 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)
        {
            string url = urlHelper.Home() + "Content/" + fileName;
            return urlHelper.Content(url);
        }
        public static string UrlImage(this UrlHelper urlHelper, string fileName)
        {
            string url = urlHelper.Home() + "Images/" + fileName;            
            return urlHelper.Content(url);
        }
        public static string FileUrl(this UrlHelper urlHelper, string fileName)
        {
            string url = urlHelper.Home() + "Images/";
            if (ConfigurationManager.AppSettings["Css"] == "truck") url += "truck/";
                else if(ConfigurationManager.AppSettings["Css"] == "admoto") url += "admoto/";
            url += fileName;
            return urlHelper.Content(url);
        }
        public static string MainCss(this HtmlHelper htmlHelper, string filename)
        {
            UrlHelper urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
            string url = urlHelper.Css(filename);
            string strCss = htmlHelper.LinkTag(url, "stylesheet", "text/css");
            return strCss;
        }
        public static string LinkTag(this HtmlHelper htmlHelper, string url, string rel, string type)
        {
            TagBuilder 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)
        {
            string filename = "";
            if (ConfigurationManager.AppSettings["Css"] == "truck") filename = "truck.css" ;   
                else if(ConfigurationManager.AppSettings["Css"] == "admoto") filename = "admoto.css";
            
            string css = htmlHelper.MainCss(filename);
            return css;
        }
        public static string Favicon(this HtmlHelper htmlHelper){
            string url = "";
            string str = "";
            if (ConfigurationManager.AppSettings["Css"] == "admoto"){
                UrlHelper urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
                url = urlHelper.FileUrl("favicon.ico");
                str = htmlHelper.LinkTag(url,"shortcut icon","");
            }
            return str;
        }
        public static string Logo(this HtmlHelper htmlHelper)
        {
            string rowspan = "";
            if (ConfigurationManager.AppSettings["Css"] == "truck") rowspan = "";
                else if (ConfigurationManager.AppSettings["Css"] == "admoto") rowspan = "2";
            string str = htmlHelper.TdTag("logo", rowspan);
            return str;
        }
        public static string TdTag(this HtmlHelper helper, string tdClass, string tdRowspan)
        {
            UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
            string url = urlHelper.FileUrl("logo.gif");
            string logotag = helper.ImageTag(url, "logo");
            string labeltag = "";

            TagBuilder tb = new TagBuilder("td");
            tb.Attributes.Add("class", tdClass);
            if (tdRowspan != "") tb.Attributes.Add("rowspan", tdRowspan);
        
            if (ConfigurationManager.AppSettings["Css"] == "admoto")
            {
                TagBuilder lab = new TagBuilder("asp:Label");
                lab.Attributes.Add("ID", "Label1");
                lab.Attributes.Add("runat", "server");
                string txt = HttpContext.GetGlobalResourceObject("tlumaczenia", "tytul").ToString();
                lab.Attributes.Add("Text", txt);
                labeltag = lab.ToString();                
            }
            tb.InnerHtml = logotag + labeltag;
            return tb.ToString();
        }
    }    
}
