using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;
using System.IO;
using System.Reflection;

[assembly: TagPrefix("Westwind.Web.Controls", "ww")]

[assembly: WebResource("Westwind.Web.Controls.Resources.wwscriptlibrary.js", "application/x-javascript")]
[assembly: WebResource("Westwind.Web.Controls.Resources.jquery.js", "application/x-javascript")]
[assembly: WebResource("Westwind.Web.Controls.Resources.warning.gif", "image/gif")]
[assembly: WebResource("Westwind.Web.Controls.Resources.info.gif", "image/gif")]
[assembly: WebResource("Westwind.Web.Controls.Resources.loading.gif", "image/gif")]
[assembly: WebResource("Westwind.Web.Controls.Resources.loading_small.gif", "image/gif")]
[assembly: WebResource("Westwind.Web.Controls.Resources.close.gif", "image/gif")]

[assembly: WebResource("Westwind.Web.Controls.Resources.calendar.gif", "image/gif")]
[assembly: WebResource("Westwind.Web.Controls.Resources.ui.datepicker.js", "text/javascript")]
[assembly: WebResource("Westwind.Web.Controls.Resources.ui.datepicker.css", "text/css")]

namespace Westwind.Web.Controls
{ 
    /// <summary>
    /// Class is used as to consolidate access to resources
    /// </summary>
    public class ControlResources
    {
        // *** Main Script Resource Strings
        public const string SCRIPTLIBRARY_SCRIPT_RESOURCE = "Westwind.Web.Controls.Resources.wwscriptlibrary.js";
        public const string JQUERY_SCRIPT_RESOURCE = "Westwind.Web.Controls.Resources.jquery.js";    

        // *** Icon Resource Strings
        public const string INFO_ICON_RESOURCE = "Westwind.Web.Controls.Resources.info.gif";
        public const string WARNING_ICON_RESOURCE = "Westwind.Web.Controls.Resources.warning.gif";
        public const string CLOSE_ICON_RESOURCE = "Westwind.Web.Controls.Resources.close.gif";
        public const string LOADING_ICON_RESOURCE = "Westwind.Web.Controls.Resources.loading.gif";
        public const string LOADING_SMALL_ICON_RESOURCE = "Westwind.Web.Controls.Resources.loading_small.gif";

        // *** Calendar Resource Strings
        public const string CALENDAR_ICON_RESOURCE = "Westwind.Web.Controls.Resources.calendar.gif";
        public const string JQUERY_CALENDAR_SCRIPT_RESOURCE = "Westwind.Web.Controls.Resources.ui.datepicker.js";
        public const string JQUERY_CALENDAR_CSS_RESOURCE = "Westwind.Web.Controls.Resources.ui.datepicker.css";

        public const string STR_JsonContentType = "application/json";
        public const string STR_JavaScriptContentType = "application/x-javascript";
        public const string STR_UrlEncodedContentType = "application/x-www-form-urlencoded";

        /// <summary>
        /// Embeds the client script library into the page as a Resource
        /// </summary>
        /// <param name="page"></param>
        public static void LoadwwScriptLibrary(Control control)
        {
            // *** Register jQuery first since we need it - not yet - next rev
            // LoadjQuery(control);

            ClientScriptProxy p = ClientScriptProxy.Current;
            //p.RegisterClientScriptResourceInHeader(control, typeof(ControlResources), ControlResources.SCRIPTLIBRARY_SCRIPT_RESOURCE,"wwScriptLibrary");
            p.RegisterClientScriptResource(control, typeof(ControlResources), ControlResources.SCRIPTLIBRARY_SCRIPT_RESOURCE);
        }

        /// <summary>
        /// Loads the appropriate jScript library out of the scripts directory
        /// </summary>
        /// <param name="control"></param>
        public static void LoadjQuery(Control control, string jQueryUrl)
        {
            ClientScriptProxy p = ClientScriptProxy.Current;

            if (!string.IsNullOrEmpty(jQueryUrl))
                p.RegisterClientScriptInclude(control.Page, typeof(ControlResources), JQUERY_SCRIPT_RESOURCE, jQueryUrl);                
            else
                p.RegisterClientScriptResource(control, typeof(ControlResources), ControlResources.JQUERY_SCRIPT_RESOURCE);
            
            return;

#if (false)
            p.RegisterClientScriptResourceInHeader(control.Page, typeof(ControlResources), JQUERY_SCRIPT_RESOURCE, "jQuery");
#endif
        }
        
        /// <summary>
        /// Loads the jQuery component uniquely into the page
        /// </summary>
        /// <param name="control"></param>
        /// <param name="jQueryUrl">Optional Url to the jQuery Library. NOTE: Should also have a .min version in place</param>
        public static void LoadjQuery(Control control)
        {
            LoadjQuery(control,null);
        }

        /// <summary>
        /// Simplified Helper function that is used to add script files to the page
        /// This version adds scripts to the top of the page in the 'normal' position
        /// immediately following the form tag.
        /// </summary>
        /// <param name="script"></param>
        public static void IncludeScriptFile(Control control, string scriptFile)
        {
            scriptFile = control.ResolveUrl(scriptFile);
            ClientScriptProxy.Current.RegisterClientScriptInclude(control, typeof(ControlResources), 
                                          Path.GetFileName(scriptFile).ToLower(), 
                                          scriptFile);            
        }

        /// <summary>
        /// Simplified Helper function that is used to add script files to the page
        /// This version adds scripts to the bottom of the page just before the
        /// Form tag is ended. This ensures that other libraries have loaded
        /// earlier in the page.
        /// 
        /// This may be required for 'manually' adding script code that relies
        /// on other dependencies. For example a jQuery library that depends
        /// on jQuery or wwScriptLIbrary that is implicitly loaded.
        /// </summary>
        /// <param name="control"></param>
        /// <param name="script"></param>
        public static void IncludeScriptFileBottom(Control control, string scriptFile)
        {
            scriptFile = control.ResolveUrl(scriptFile);
            ClientScriptProxy.Current.RegisterClientScriptBlock(control, typeof(ControlResources),
                            Path.GetFileName(scriptFile).ToLower(),
                            "<script src='" + scriptFile + "' type='text/javascript'></script>",
                            false);            
        }


        /// <summary>
        /// Returns a string resource from a given assembly.
        /// </summary>
        /// <param name="assembly">Assembly reference (ie. typeof(ControlResources).Assembly) </param>
        /// <param name="ResourceName">Name of the resource to retrieve</param>
        /// <returns></returns>
        public static string GetStringResource(Assembly assembly, string ResourceName)
        {            
            Stream st = assembly.GetManifestResourceStream(ControlResources.SCRIPTLIBRARY_SCRIPT_RESOURCE);
            StreamReader sr = new StreamReader(st);
            string content = sr.ReadToEnd();            
            st.Close();
            return content;
        }

        /// <summary>
        /// Returns a string resource from the from the ControlResources Assembly
        /// </summary>
        /// <param name="ResourceName"></param>
        /// <returns></returns>
        public static string GetStringResource(string ResourceName)
        {
            return GetStringResource(typeof(ControlResources).Assembly, ResourceName);
        }

    }    

    

}
