/*
 * [ArrowNonets.java]
 *
 * Summary: Displays nonets of arrows of various types. One shot.
 *
 * Copyright: (c) 1999-2014 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.7+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2011-10-03 initial version
 */
package com.mindprod.entities;

import com.mindprod.fastcat.FastCat;
import com.mindprod.hunkio.HunkIO;

import java.io.File;
import java.io.IOException;

import static java.lang.System.err;
import static java.lang.System.out;

/**
 * Displays nonets of arrows of various types. One shot.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2011-10-03 initial version
 * @since 2011-02-10
 */
public final class ArrowNonets
    {
    /**
     * where we put the generated HTML for inclusion on the website
     */
    private static final String NONET_OUTPUT_FILE = "entityarrownonets.htmlfrag";

    /**
     * converts awkward characters to HTML entities. output appears in entitiesarrownonets.html
     *
     * @param args names of files to process, dirs, files, -s, *.*, no wildcards.
     */
    public static void main( String[] args )
        {
        final FastCat sb = new FastCat( 25 );
        sb.append(
                "<table class=\"standard\"><caption class=\"hidden\">ways of making arrows</caption><thead><tr><th colspan=\"2\">Ways of " +
                "Making Arrows</th></tr>"
        );
        sb.append( "<tr><th>Appearance</th>\n" );
        sb.append( "<th>HTML tags</th></tr></thead>\n" );
        sb.append( "<tbody><tr><td>\n" );
        final Nonet angleArrows = new Nonet( new String[] {
                "&nbsp;",
                "&and;",
                "&nbsp;",
                "&lt;",
                "&mdash;",
                "&gt;",
                "&nbsp;",
                "&or;",
                "&nbsp;" } );
        sb.append( angleArrows.grid( "arrow appearance", "behold", false ) );
        sb.append( "</td><td>\n" );
        sb.append( angleArrows.grid( "HTML entities", "htmlentity", true ) );
        sb.append( "</td></tr>\n<tr><td>\n" );
        final Nonet singleArrows = new Nonet( new String[] {
                "&#x2196;",
                "&uarr;",
                "&#x2197;",
                "&larr;",
                "&harr;",
                "&rarr;",
                "&#x2199;",
                "&darr;",
                "&#x2198;" } );
        sb.append( singleArrows.grid( "arrow appearance", "behold", false ) );
        sb.append( "</td><td>\n" );
        sb.append( singleArrows.grid( "HTML entities", "htmlentity", true ) );
        sb.append( "</td></tr>\n<tr><td>\n" );
        final Nonet doubleArrows = new Nonet( new String[] {
                "&nbsp;",
                "&uArr;",
                "&nbsp;",
                "&lArr;",
                "&hArr;",
                "&rArr;",
                "&nbsp;",
                "&dArr;",
                "&nbsp;" } );
        sb.append( doubleArrows.grid( "arrow appearance", "behold", false ) );
        sb.append( "</td><td>\n" );
        sb.append( doubleArrows.grid( "HTML entities", "htmlentity", true ) );
        sb.append( "</td></tr></tbody></table>\n" );
        sb.append( "<!-- End of html generated by the com.mindprod.entities.ArrowNonets program -->\n" );
        final String result = sb.toString();
        try
            {
            HunkIO.writeEntireFile( new File( NONET_OUTPUT_FILE ), result );
            out.println( NONET_OUTPUT_FILE + " generated" );
            }
        catch ( IOException e )
            {
            err.println( "Could not write " + NONET_OUTPUT_FILE );
            }
        } // end main
    } // end ArrowNonets

/**
 * which 9 chars used to describe a grid of arrows
 */
class Nonet
    {
    /**
     * entity for an arrow for arrow
     */
    private final String[] nonets;

    /**
     * constructor to build a grid of 9 arrow chars
     *
     * @param nonets entities for  9 arrow chars, top to bottom, left to right
     */
    Nonet( String[] nonets )
        {
        assert nonets != null && nonets.length == 9 : "must have 9 entries in a Nonet.  ";
        for ( String entity : nonets )
            {
            assert entity.startsWith( "&" ) : "Missing & no start of entity " + entity;
            assert entity.endsWith( ";" ) : "Missing semicolon on end of entity " + entity;
            }
        this.nonets = nonets;
        }

    /**
     * build HTML to display a 3x3 grid of arrow chars
     */
    String grid( final String summary, final String cssClass, boolean visibleEntities )
        {
        final FastCat sb = new FastCat( 60 );
        sb.append( "<table class=\"borderless\"><caption class=\"hidden\">" );
        sb.append( summary );  // not visible
        sb.append( "</caption><colgroup style=\"text-align:center\" class=\"entity\" span=\"3\"></colgroup>\n" );
        sb.append( "<tbody>" );
        for ( int i = 0; i < 9; i++ )
            {
            if ( i % 3 == 0 )
                {
                sb.append( "<tr>" );
                }
            sb.append( "<td class=\"" );
            sb.append( cssClass );
            sb.append( "\">" );
            if ( visibleEntities )
                {
                String entity = nonets[ i ];
                if ( !entity.equals( "&nbsp;" ) )
                    {
                    sb.append( EntifyStrings.entifyHTML( entity ) ); // convert & to &amp; on start
                    }
                }
            else
                {
                sb.append( nonets[ i ] );
                }
            sb.append( "</td>\n" );
            if ( i % 3 == 2 )
                {
                sb.append( "</tr>\n" );
                }
            }
        sb.append( "</tbody></table>\n" );
        return sb.toString();
        }
    }
