package textbender.a.r.page; // Copyright 2007, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE.

import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.concurrent.atomic.*;
import javax.swing.*;
import org.w3c.dom.*;
import textbender.a.r.desk.*;
import textbender.g.lang.*;
import textbender.g.net.*;
import textbender.g.util.logging.*;
import textbender.o.*;
import textbender.o.rhinohide.*;
import textbender.o.rhinohide._.*;

import static textbender.o.xhtml.XHTML.XHTML_NAMESPACE;


/** The page daemon's default boot applet. Injected into each page
  * by the boot script, this applet in turn injects
  * the {@linkplain ToolbarApplet toolbar applet}.
  * Normally, the toolbar applet would be injected
  * by a user script (Greasemonkey); but demo users have no such script set up.
  * And the page daemon must run from a local codebase,
  * otherwise its Java code is inaccessible to JavaScript of the local document
  * Owing to JavaScript 'same-origin' restrictions, access attempts
  * would be met with an exception like this, from the plug-in:
  * <blockquote>
  *     sun.plugin.liveconnect.OriginNotAllowedException: JavaScript is not from the same origin as the Java code, caller=file:, callee=http://reluk.ca/var/cache/textbender-demo/textbender.jar
  *     </blockquote>
  * Therefore, we resort to a flying trapeze act.
  * This boot applet makes RMI contact with the local desk daemon;
  * then injects the toolbar applet from the local codebase.
  * So (at least for local documents) we have bi-directional Java/JavaScript.
  */
    @ThreadRestricted( "AWT event dispatch" )

public final class BootApplet extends JApplet
{

    /** Constructs a BootApplet.
      */
    public BootApplet()
    {
     // Box contentPane = new Box( BoxLayout.X_AXIS );
     // contentPane.setOpaque( true ); // required, if only per JRootPane
     ///// not painting, I'm forgetting how to Swing...
        JPanel contentPane = new JPanel( /*layout*/null );
        assert contentPane.isOpaque(); // required, if only per JRootPane

     // contentPane.setBackground( Color.getHSBColor( /*H*/0.0f, /*S*/0.0f, /*B*/0.9f )); // grey
        contentPane.setBorder // for contrast, to ensure visibility
        (
            BorderFactory.createMatteBorder
            (
                /*top*/0, /*left*/0, /*bottom*/2, /*right*/2,
                contentPane.getBackground().darker()
            )
        );
        setContentPane( contentPane );

     // setFocusable( false );
    }



   // - A p p l e t ----------------------------------------------------------------------


        private final AtomicBoolean isStartedA = new AtomicBoolean();


    public @ThreadSafe void start()
    {
        if( isStartedA.getAndSet( true )) return; // start once only
        LoggerX.i(getClass()).fine( "booting" );
        try
        {
            final RhiWindow window = RhiWindow.createWindow( BootApplet.this );
            try
            {
                final File file = resolveFile( window );
                if( file != null ) // then indicate to user that page source is a local file:
                {
                    JComponent contentPane = (JComponent)getContentPane();
                    contentPane.setBackground // FIX to read from style sheets, somehow
                        ( Color.getHSBColor( /*H*/0.60f, /*S*/0.25f, /*B*/1.00f )); // bluish
                     // ( Color.getHSBColor( /*H*/0.33f, /*S*/0.40f, /*B*/0.99f )); // green
                     // ( Color.getHSBColor( /*H*/0.12f, /*S*/0.40f, /*B*/0.99f )); // orange
                     // ( Color.WHITE );
                }
                final Document document = window.getDocument();

              // <* id='textbender-a-r-page-applet-parent'>
              // ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `
                final Element appletParent = document.getElementById
                    ( "textbender-a-r-page-applet-parent" );

                try
                {
                    final HostServiceRegistry hostServiceRegistry
                        = HostServiceRegistry.X.findRegistry(); // toolbar applet's PageVisit will also do this lookup, through Connections.refresh, but they cannot share the reference in any way, because the two applets have separate classloaders (Firefox 2, Plug-In 1.6)
                    if( hostServiceRegistry == null )
                    {
                     // appletParent.insertBefore
                     // (
                     //     document.createTextNode( "No running desk daemon,\nomitting page daemon.\n" ),
                     //     appletParent.getFirstChild()
                     // );
                        appletParent.appendChild
                        (
                            document.createTextNode( "\nNo running desk daemon,\nomitting page daemon." )
                        );
                        return;
                    }

                    final String localJarURI =
                        hostServiceRegistry.allInOneJar().toURI().toASCIIString();
                    LoggerX.i(getClass()).finest( "resolved local JAR: " + localJarURI );

                  // <applet id='textbender-a-r-page-boot-applet' . . .
                  // Cf. boot.js, _textbender_a_r_page_boot_inject()
                  // ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `
                    final Element applet = document.createElementNS( XHTML_NAMESPACE, "applet" );
                    applet.setAttributeNS( null, "id", "textbender-a-r-page-applet" );
                    applet.setAttributeNS( null, "alt", "[textbender-a-r-page-applet: for recombinant text]" );
                    applet.setAttributeNS( null, "archive", localJarURI );
                    applet.setAttributeNS( null, "code", "textbender/a/r/page/ToolbarApplet.class" );
                    applet.setAttributeNS( null, "MAYSCRIPT", "true" );
                    applet.setAttributeNS( null, "width", "300" ); // default, if unaltered by styles
                    applet.setAttributeNS( null, "height", "21" ); // "
                    applet.appendChild( document.createTextNode
                        ( "[textbender-a-r-page-applet:\nunsupported by this browser]" ));

              // // appletParent.insertBefore
              // //     ( document.createTextNode( "\n" ), appletParent.getFirstChild() );
              // // appletParent.insertBefore( applet, appletParent.getFirstChild() );
              // ///// applet fails to render/start, Firefox 1.5, so:
              // // appletParent.insertBefore( applet, appletParent.getFirstChild() );
              // // appletParent.insertBefore
              // //     ( document.createTextNode( "\n" ), applet.getNextSibling() );
              // ///// which works, but maybe this is nicer:
              //    appletParent.insertBefore
              //    (
              //        applet,
              //        appletParent.insertBefore
              //            ( document.createTextNode( "\n" ), appletParent.getFirstChild() )
              //    );
                    appletParent.appendChild( document.createTextNode( "\n" ));
                    appletParent.appendChild( applet );
                }
                catch( Exception x )
                {
                    try
                    {
                     // appletParent.insertBefore
                     // (
                     //     document.createTextNode( "[textbender-a-r-page-applet:\n" + x + "]\n" ),
                     //     appletParent.getFirstChild()
                     // );
                        appletParent.appendChild
                        (
                            document.createTextNode( "\n[textbender-a-r-page-applet:\n" + x + "]" )
                        );
                    }
                    catch( RuntimeException xR ) {}
                    throw x;
                }
            }
            finally{ window.release(); }
        }
        catch( StunnedRhinoException xSR ) { LoggerX.i(getClass()).config( Browser.pageExitSupressionMessage( xSR )); }
        catch( Exception x ) { LoggerX.i(getClass()).log( LoggerX.WARNING, /*message*/"", x ); } // all, checked or not
    }



//// P r i v a t e ///////////////////////////////////////////////////////////////////////


//  static JComponent createContentPane() // Common with page daemon ToolbarApplet. Could be defined there, but would pull alot of code into this boot applet's jar.
//  {
//   // Box contentPane = new Box( BoxLayout.X_AXIS );
//   // contentPane.setOpaque( true ); // required, if only per JRootPane
//   ///// not painting, I'm forgetting how to Swing...
//      JPanel contentPane = new JPanel( /*layout*/null );
//      assert contentPane.isOpaque(); // required, if only per JRootPane
//
//      contentPane.setBackground( Color.getHSBColor( /*H*/0.0f, /*S*/0.0f, /*B*/0.9f )); // grey
//      contentPane.setBorder // for contrast, to ensure visibility
//      (
//          BorderFactory.createMatteBorder
//          (
//              /*top*/0, /*left*/0, /*bottom*/2, /*right*/2,
//              contentPane.getBackground().darker()
//          )
//      );
//      return contentPane;
//  }
//
//
//
    /** Resolves the source of the page as a local file,
      * per {@linkplain PageVisit#file() file}().
      *
      *     @return source as a local file,
      *         or null if source is not a local file
      */
    static File resolveFile( RhiWindow window ) throws URISyntaxException // Common with ToolbarApplet. Could be defined there, and called from here instead, but that would pull alot of code into this boot applet's jar.
    {
        File file = null; // till proven otherwise
        URI href = new URI( (String)window.eval( "location.href" ));
        if( "file".equals( href.getScheme() )) file = new File( URIX.stripQueryAndFragment( href ));
        return file;
    }



}