/*
 * [Doha.java]
 *
 * Summary: Countdown to Doha Climate Change conference 2012-11-26 to 2012-12-12-07.
 *
 * Copyright: (c) 2011-2017 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.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2011-12-11 initial version, cloned from Copenhagen
 */
package com.mindprod.inauguration;

import com.mindprod.common18.BigDate;
import com.mindprod.common18.Build;
import com.mindprod.common18.FontFactory;
import com.mindprod.common18.Hybrid;
import com.mindprod.common18.VersionCheck;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Label;

/**
 * Countdown to Doha Climate Change conference 2012-11-26 to 2012-12-12-07.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2011-12-11 initial version, cloned from Copenhagen
 * @since 2011-12-11
 */
public final class Doha extends Applet
    {
    /**
     * height of Applet box in pixels. Does not include surrounding frame.
     */
    private static final int APPLET_HEIGHT = 36;

    /**
     * Width of Applet box in pixels.
     */
    private static final int APPLET_WIDTH = 560;

    private static final int FIRST_COPYRIGHT_YEAR = 2011;

    /**
     * undisplayed copyright notice
     */
    private static final String EMBEDDED_COPYRIGHT =
            "Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com";

    private static final String RELEASE_DATE = "2011-12-11";

    /**
     * embedded version string
     */
    private static final String TITLE_STRING = "Doha";

    /**
     * embedded version string
     */
    private static final String VERSION_STRING = "1.0";

    /**
     * where we display time to next inauguration
     */
    private Label time;

    /**
     * Allow this Applet to run as as application as well.
     *
     * @param args command line arguments are ignored.
     */
    public static void main( String args[] )
        {
        Hybrid.fireup( new Doha(),
                TITLE_STRING + " " + VERSION_STRING,
                APPLET_WIDTH,
                APPLET_HEIGHT );
        } // end main

    /**
     * Called by the browser or Applet viewer to inform
     * this Applet that it is being reclaimed and that it should destroy
     * any resources that it has allocated.
     */
    public void destroy()
        {
        time = null;
        }

    /**
     * Called by the browser or Applet viewer to inform
     * this Applet that it has been loaded into the system.
     */
    public void init()
        {
        if ( !VersionCheck.isJavaVersionOK( 1, 8, 0, this ) )
            {
            return;
            }
        // default layout, no About box.
        setVisible( false );
        setBackground( Color.green );// none should show if all is well.
        setLayout( new BorderLayout() );
        BigDate conference = new BigDate( 2012, 11, 26 );
        // decide date based on DST and local TimeZone.
        BigDate today = BigDate.localToday();
        int[] age = BigDate.age( today, conference );
        StringBuilder sb = new StringBuilder( 100 );
        if ( age[ 0 ] > 0 )
            {
            sb.append( age[ 0 ] );
            sb.append( ( age[ 0 ] == 1 ) ? " year, " : " years, " );
            }
        if ( age[ 1 ] > 0 )
            {
            sb.append( age[ 1 ] );
            sb.append( ( age[ 1 ] == 1 ) ? " month, " : " months, " );
            }
        if ( age[ 2 ] > 0 )
            {
            sb.append( age[ 2 ] );
            sb.append( ( age[ 2 ] == 1 ) ? " day " : " days " );
            }
        sb.append( "until the Doha Climate Change Conference in Qatar." );
        time = new Label( sb.toString(), Label.CENTER );
        time.setBackground( Build.BACKGROUND_FOR_BLENDING );
        time.setForeground( new Color( 0x000800 ) );
        time.setFont( FontFactory.build( "Dialog", Font.BOLD, 14 ) );
        add( time, BorderLayout.CENTER );
        validate();
        setVisible( true );
        } // end init
    }