// Copyright (c) 2010-2016 Quadralay Corporation.  All rights reserved.
//

using System;
using System.IO;
using System.Text;

namespace WebWorks.Help
{
  public class WWHelpAPI
  {
    #region Const

    static private readonly string  smSmartURLPrefix = "javascript:x=unescape('";
    static private readonly string  smSmartURLSuffix = "');if(typeof(window.WWHFrame)!='undefined'){window.WWHFrame.WWHHelp.fSetContextDocument(x);}";

    static private readonly string  smDefault = "";
    static private readonly string  smContents = "contents";
    static private readonly string  smIndex = "index";
    static private readonly string  smSearch = "search";
    static private readonly string  smFavorites = "favorites";

    #endregion

    #region Static

    static internal string  BuildSmartURL(string  locationURL)
    {
      string  result = locationURL;

      if ( ! String.IsNullOrEmpty(locationURL))
      {
        string  escapedLocationURL;

        // Escape the given URL
        //
        escapedLocationURL = Uri.EscapeUriString(locationURL);
        result = smSmartURLPrefix + escapedLocationURL + smSmartURLSuffix;
      }

      return result;
    }

    static internal bool  WriteRedirectHTMLFile(string  htmlFilePath,
                                                string  locationURL)
    {
      bool  result = false;

      try
      {
        using(FileStream  htmlFileStream = new FileStream(htmlFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read, 8192))
        using(StreamWriter  htmlTextStream = new StreamWriter(htmlFileStream, Encoding.UTF8))
        {
          htmlTextStream.Write("<html>\n <head>\n  <title>Redirect</title>\n\n  <script type=\"text/javascript\" language=\"JavaScript1.2\">\n   <!--\n    function  WWHRedirect()\n    {\n      location.replace(\"");
          htmlTextStream.Write(locationURL);
          htmlTextStream.Write("\");\n    }\n   // -->\n  </script>\n </head>\n <body onload=\"WWHRedirect();\">\n </body>\n</html>");
        }

        // Success!
        //
        result = true;
      }
      catch
      {
        // Ignore!
        //
      }

      return result;
    }

    #endregion

    #region Fields

    private Uri mBaseUri;

    #endregion

    #region Constructors

    public WWHelpAPI(Uri  baseUri)
    {
      this.mBaseUri = baseUri;
    }

    #endregion

    #region Members

    public Uri BaseUri
    {
      get { return this.mBaseUri; }
    }

    #endregion

    #region Private Methods

    /// <summary>
    /// Displays the supplied context and topic in the default browser with
    /// single topic display set as the parameter specifies.
    /// </summary>
    /// <param name="context"></param>
    /// <param name="topic"></param>
    /// <param name="display_topic_without_navigation"></param>
    /// <param name="initial_tab"></param>
    /// <returns></returns>
    private bool  DisplayHelp(string  context,
                              string  topic,
                              bool    display_topic_without_navigation,
                              string  initial_tab)
    {
      bool  result = false;

      if (this.mBaseUri != null)
      {
        string  parameters = String.Empty;
        string  helpUriAsString = null;

        // Context and topic defined?
        //
        if (
            (context.Length > 0)
             &&
            (topic.Length > 0)
           )
        {
          parameters += "context=" + context;
          parameters += "&topic=" + topic;
        }

        // Display topic without navigation?
        //
        if (display_topic_without_navigation)
        {
          if (parameters.Length > 0)
          {
            parameters += "&";
          }
          parameters += "single=true";
        }

        // Initial tab requested?
        //
        if (initial_tab.Length > 0)
        {
          if (parameters.Length > 0)
          {
            parameters += "&";
          }
          parameters += "tab=" + initial_tab;
        }

        // Create help URI
        //
        try
        {
          Uri helpUri;

          helpUri = new Uri(this.mBaseUri.AbsoluteUri + "/wwhelp/wwhimpl/api.htm");
          helpUriAsString = helpUri.AbsoluteUri + "?" + parameters;
        }
        catch
        {
          // Problem creating help URI
          //
          helpUriAsString = null;
        }

        // Display help URI
        //
        if (helpUriAsString != null)
        {
          BrowserProxy  browserProxy;

          browserProxy = new BrowserProxy(this.mBaseUri);
          browserProxy.DisplayURL(helpUriAsString);

          // Success!
          //
          result = true;
        }
      }

      return result;
    }

    #endregion


    #region Public Methods

    public bool  DisplayHelp(string  context,
                             string  topic)
    {
      bool  result;

      result = DisplayHelp(context, topic, true, smDefault);

      return result;
    }

    public bool  DisplayHelpWithNavigation(string  context,
                                           string  topic)
    {
      bool  result;

      result = DisplayHelp(context, topic, false, smDefault);

      return result;
    }

    public bool  DisplayHelpWithContents(string  context,
                                         string  topic)
    {
      bool  result;

      result = DisplayHelp(context, topic, false, smContents);

      return result;
    }

    public bool  DisplayHelpWithIndex(string  context,
                                      string  topic)
    {
      bool  result;

      result = DisplayHelp(context, topic, false, smIndex);

      return result;
    }

    public bool  DisplayHelpWithSearch(string  context,
                                       string  topic)
    {
      bool  result;

      result = DisplayHelp(context, topic, false, smSearch);

      return result;
    }

    public bool  DisplayHelpWithFavorites(string  context,
                                          string  topic)
    {
      bool  result;

      result = DisplayHelp(context, topic, false, smFavorites);

      return result;
    }

    public bool  DisplayHelpNavigation()
    {
      bool  result;

      result = DisplayHelp(String.Empty, String.Empty, false, smDefault);

      return result;
    }

    public bool  DisplayHelpContents()
    {
      bool  result;

      result = DisplayHelp(String.Empty, String.Empty, false, smContents);

      return result;
    }

    public bool  DisplayHelpIndex()
    {
      bool  result;

      result = DisplayHelp(String.Empty, String.Empty, false, smIndex);

      return result;
    }

    public bool  DisplayHelpSearch()
    {
      bool  result;

      result = DisplayHelp(String.Empty, String.Empty, false, smSearch);

      return result;
    }

    public bool  DisplayHelpFavorites()
    {
      bool  result;

      result = DisplayHelp(String.Empty, String.Empty, false, smFavorites);

      return result;
    }

    #endregion
  }
}
