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

namespace Platnosci.Helpers
{
    public static class UrlImage
    {
        public static string Image(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 src, string alt, string querystring)
        {
            
            var action = htmlHelper.CurrentAction();
            var controller = htmlHelper.CurrentController();
                                    
            UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
            string imgtag = htmlHelper.Image(src, alt);
            
            RouteValueDictionary tab = new RouteValueDictionary();
            string id = htmlHelper.ViewContext.RouteData.Values["id"].ToString();
            tab.Add("language", alt);
            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();
        }       
    }    
}
