<%@ page language="C#" contenttype="text/html" inherits="_Default, FaxCore.OSA40" uiculture="Auto" %>
<%@ Import Namespace="Osa.MfpWebService" %>
<%@ Import Namespace="Osa.Util" %>
<%@ Import Namespace="System.Xml" %>
<%
    OsaRequestInfo req = new OsaRequestInfo(Request);
    USERINFO_TYPE credSSO = null;
    Log logger = new Log(Server.MapPath(""));
    // You should check whether the remote user is an OpenSystems enabled MFP.
    // Or an exception is thrown because they has not UISessionID.
    // You can use a utility method OsaRequestInfo.IsOpenSystems() for this check.
    
    // Create a MfpUISession object for this UISessionID
    string msg = string.Empty;
    if (!Helper.UISession.Create(req, Request.PhysicalApplicationPath, ref msg))
    {
        bool HTMLerrorpage = false;
        
        for (int i = 0; i < Request.Headers.AllKeys.Length; i++)
        {
            if (string.Compare(Request.Headers.AllKeys[i], "X-BC-Use-Alternative", true) == 0)
            {
                HTMLerrorpage = true;
            }
        }
        logger.DebugLog("is html error: " + HTMLerrorpage.ToString());
        logger.DebugLog("msg: " + msg);
        if (HTMLerrorpage)
        {
            //Response.Redirect(string.Format("FatalErrorHtml.aspx?msg={0}", msg));
          Response.Redirect("login.aspx");
        }
        else
        {
            //Response.Redirect(string.Format("FatalError.aspx?msg={0}", msg));
          Response.Redirect("login.aspx");
        }
        return;
    }

    // Set custom fields
    Helper.UISession s = Helper.UISession.Get(req.GetUISessionID());
    s.css_file = req.GetBC_Resolution();
    s.workMode = Request.Params["mode"];
  
  //SSO
    try
    {
      credSSO = s.GetUserInfo(req);

      if (credSSO.metadata.Length > 1)
      {
        Session["SSO"] = credSSO;
      }
    }
    catch (Exception err)
    {
      logger.DebugLog("SSO ERROR: " + err.Message);
    }
    
    //PROPERTY_TYPE[] prp = new PROPERTY_TYPE[4];
    //PROPERTY_TYPE p1 = new PROPERTY_TYPE();  
    //p1.sysname = "display-name";
    //p1.Value = "sam ng";
    //prp[0] = p1;

    //PROPERTY_TYPE p2 = new PROPERTY_TYPE();  
    //p2.sysname = "login-name";
    //p2.Value = "admin";
    //prp[1] = p2;
    //PROPERTY_TYPE p3 = new PROPERTY_TYPE();  
    //p3.sysname = "secure-key";
    //p3.Value = "password";
    //prp[2] = p3;
    //PROPERTY_TYPE p4 = new PROPERTY_TYPE();  
    //p4.sysname = "email-address";
    //p4.Value = "sam@faxcore.com";
    //prp[3] = p4;
    //credSSO.metadata = prp;

    //if (credSSO.metadata.Length > 1)
    //{
    //  Session["SSO"] = credSSO;
    //}
    
    //logger.DebugLog("SSO Metadata count: "+ credSSO.metadata.Length.ToString());
  
    // Redirect to the ready page.
    const string ERRORPAGE = "ErrorBW.aspx";
    const string MAINPAGE = "login.aspx";
    const string HTMLBROWSERNAME = "NetFront";
    
    string url = ERRORPAGE;
    logger.DebugLog("user agent 2: " + Request.UserAgent);
    if (Request.UserAgent.Contains(HTMLBROWSERNAME))
    {
        url = MAINPAGE;
    }
    else
    {
        for (int i = 0; i < Request.Headers.AllKeys.Length; i++)
        {
          logger.DebugLog(string.Format("Key: {0} - {1}", Request.Headers.AllKeys[i].ToString(), Request.Headers.Get(i).ToString()));
            if (string.Compare(Request.Headers.AllKeys[i], "X-BC-Alternative", true) == 0)
            {
                // Check whether the HTML browser is available.
                if (Request.Headers.Get(i).IndexOf(HTMLBROWSERNAME) > -1)
                {
                    // Navigate to the page for NetFront
                    url = MAINPAGE;
                    break;
                }
            }
        }
    }
    Response.Redirect(url, false);
%>
