import javax.swing.*;
import java.awt.*;
import java.util.*;


/** An instance represents the time of day in a time zone, in
 terms of hours, minutes, and seconds. The implemented time zones are:<br><br>
 
 PST: Pacific Standard Time, GMT-8 hours (LA)<br>
 PDT: Pacific Daylight Saving Time, GMT-7 hours (LA)<br>
 MST: Mountain Standard Time, GMT-7 hours (Phoenix)<br>
 MDT: Mountain Daylight Savings Time, GMT-6 (Phoenix)<br>
 CST: Central Standard Time, GMT-6 hours (Chicago)<br>
 CDT: Central Daylight Savings Time, GMT-5 hours (Chicago)<br>
 EST: Eastern Standard Time, GMT-5 hours (NY)<br>
 EDT: Eastern Daylight Savings Time, GMT-4 hours (NY)<br>
 GMT: Greenwich Mean Time, GMT<br>
 BST: British Summer Time, GMT+1<br>
 IND: India time, GMT+5:30 hours (New Delhi)<br><br>
 
 India (IND) is included to show that times are not always on
 hourly boundaries from GMT.<br><br>
 
 A time may appear negative or greater than 24 hours.
 This is because of possible conversions of a time from one time zone to another,
 e.g. time of 0 hours GMT is -7 hours PDT, while a time of
 23:59 GMT is 29:29 IND.<br><br>
 
 The user can decide to show the time using a 24-hour clock
 or a AM-PM designation..
 */
public class Time {
    
    private static final String[] zones= new String[]{"PST", "PDT", "MST", "MDT", "CST",
        "CDT", "EST", "EDT", "GMT", "BST", "IND"};
    
    /**The following constants contain the eleven time zones, as Strings. */
    public static final String BST= "BST";
    public static final String GMT= "GMT";
    public static final String EST= "EST";
    public static final String EDT= "EDT";
    public static final String CST= "CST";
    public static final String CDT= "CDT";
    public static final String MST= "MST";
    public static final String MDT= "MDT";
    public static final String PST= "PST";
    public static final String PDT= "PDT";
    public static final String IND= "IND";
    
    /** A string consisting of the 3-letter time zone names, separated by blanks. */
    public static final String ALL= "GMT BST EST EDT CST CDT MST MDT PST PDT IND";
    
    /** Class invariant: Variable tim is a time in seconds on a day in
     time zone zon. The time may be negative or greater than 24 hours, as indicated
     in class specification (which says why). Field disp12Hr has the meaning
     "the time should be viewed as a 12-hour clock".
     
     gui is the JFrame for the GUI, contentPane is its content pane, and
     box contains the strings that appear in the GUI (a title and one line for
     each time zone, each being a JLabel).
     */
    private int tim= 0;
    private String zon= "GMT";
    private boolean disp12Hr= false;
    
    private static JFrame gui;
    private static Container contentPane;
    private static Box box;
    
    /** Constructor: an instance with time 0 in GMT and a 24-hour clock. */
    public Time() {
    }
    
    /** Constructor: A time of day of s seconds in zone z.
     Note: s should greater than -24 hours and less than +48 hours;
     if not, the time 0 is used.
     If z is not a legal zone, GMT will be used. 
     The time should be displayed as am-pm iff b is true */
    public Time(int s, String z, boolean b) {
        if (isLegal(z)) {
            zon= z;
        }
        tim= s;
        if (s <= -24*60*60  ||  48*60*60 <= s) {
            tim= 0;
        }
        disp12Hr= b;
    }
    
    /** Constructor: A time of day of h hours, m minutes, and s seconds in zone z.
     The time should be greater than -24 hours and less than +48 hours;
     if not, the time 0 is used.
     If z is not a legal zone, make it GMT. 
     The time should be displayed as am-pm iff b is true. */
    public Time(int h, int m, int s, String z, boolean b) {

    }
    
    /* = the time, in seconds. */
    public int getTime() {
        return 0;
    }
    
    /** = the time zone. */
    public String getZone() {
        return "";
    }
    
    /** = the am-pm indication --true means display in AM-PM format;
     false means display in 24-hour format. */
    public boolean getAmPm() {
        return true;
    }
    
    /** If b is true, set the display mode to AM-PM;
     if b is false, set it to "twenty-four hours". */
    public void setDisplay(boolean b) {
        
    }
    
    /** = "z is a legal zone" --see class specification. */
    public static boolean isLegal(String z) {
        return true;  
    }
    
    /** = a string representation of the time. This is basically in the
     form "hours:minutes:seconds zone", but it differs depending on
     whether a 12- or 24-hour clock is wanted. 
     We describe the difference with examples:<br><br>
     
     In AM-PM mode, output could be: 06:20:5AM DST  or  06:20:5PM DST<br>
     In 24-hour mode output could be: 06:20:5 DST    or   18:20:5 DST<br><br>
     
     If the time is negative or at least 24 hours, print it using the 24-hour
     mode, even if 12-hour mode is indicated.
     */
    public String toString() {
        int sec= Math.abs(tim % 60);
        int temp= tim / 60;
        int min= Math.abs(temp % 60);
        int hr= temp / 60;
        String result= "";
        
        // Save the am-pm indication in local variable
        // amPm. Then, if the time is negative or >= 24, set amPm to false
        // to 24-hour clock. Then, use amPM to tell what mode to use.
        
        
        // Append either a blank or '-' to result, depending on whether
        // the time is positive or negative.
        
        
        // Append the hour to result
        
        
        // Append the minutes to result
        
        
        // Append the seconds to result
        
        
        // Append the am-pm designation if necessary
        
        
        return result + " " + zon;   
    }
    
    
    
    /** =  a new Time that is the same as this time but described in GMT. */
    public Time timeInGMT() {
        Time t= new Time(tim, GMT, disp12Hr);
        if (zon.equals(BST)) t.tim= tim - 1*3600;
        if (zon.equals(EST)) t.tim= tim + 5*3600;
        if (zon.equals(EDT)) t.tim= tim + 4*3600;
        if (zon.equals(CST)) t.tim= tim + 6*3600;
        if (zon.equals(CDT)) t.tim= tim + 5*3600;
        if (zon.equals(MST)) t.tim= tim + 7*3600;
        if (zon.equals(MDT)) t.tim= tim + 6*3600;
        if (zon.equals(PST)) t.tim= tim + 8*3600;
        if (zon.equals(PDT)) t.tim= tim + 7*3600;
        if (zon.equals(IND)) t.tim= tim - 5*3600 - 3600/2;
        
        return t;
    }
    
    /** =  a new Time that is the same as this time but described in 
     zone z. If z is not a legal zone, the new zone is taken to be GMT. */
    public Time timeInZone(String z) {
        
        return null;
    }
    
    /** = -1 if this time < time t,
     0 if this time equals time t, and
     1 if this time > time t. */
    public int compareTo(Time t) {
        return 1;   
    }
    
    /** Display the time given by this object in all time zones, in 24-hour mode. **/
    public void display() {
        if (gui == null) {
            // Create and save a JFrame and its content pane in gui and contentPane
            gui= new JFrame();
            gui.setTitle("The time in several zones");
            contentPane= gui.getContentPane();
        }
        else { // Remove the previous box of output
            contentPane.remove(box);
        }
        
        box= new Box(BoxLayout.Y_AXIS);
        contentPane.add(box, BorderLayout.CENTER);
        box.add(new JLabel(" The time in eleven different time zones. "));
        
        boolean ampm= getAmPm();  // Save the setting, for restoration later
        setDisplay(false);        // Indicate 24-hour time
        
        // Add the time in all 11 times zones to the GUI.
        // inv: Times for zones zones[0..k-1] have been added to box.
        for (int k= 0; k != zones.length; k= k+1) {
            JLabel j= new JLabel(" " + timeInZone(zones[k]).toString());
            j.setFont(new Font("Courier new", Font.PLAIN, 16));
            box.add(j);
        }
        
        gui.pack();             // This tells the JFrame to layout the components
        gui.setVisible(true);   // Make the JFrame visible
        
        setDisplay(ampm);       // restore the original setting of 24-hour mode
    }
}