
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;


public class BikeShop extends JApplet implements ActionListener, ItemListener 
{   
    // private variables
    private final String appletTitle = "Ben and Azaan's BIKES";

    private final double SPRINGPRICE = 5.00;
    private final double SUMMERPRICE = 7.50;
    private final double FALLPRICE = 3.99;
    private final double WINTERPRICE = 2.50;
    
    private final String[] SEASONS = { "Spring", "Summer", "Fall", "Winter" };
    private int currentSeason = 0;
    private double currentSeasonCost = SPRINGPRICE; //default is set to Spring
    
    private Image bikes[];
    private final int NUMBEROFBIKES = 4;
    private int selectedBike = 0;   
    
    private int daysRented = 0;
    private double totalCost = 0;
    
    
    // main layout for applet
    private JTextField txfNumberOfDays;
    private JRadioButton rbSpring, rbSummer, rbFall, rbWinter;
    private JButton btnCalculateTotal, btnNextBike, btnPrevBike;
                    
    private JLabel  labelTitle, labelSeasons, labelNumberOfDays, labelTotal, 
                    labelTotalDisplay, labelBikeImage;      
    private JPanel  panelWest, panelCenter, panelSeasons, panelBike, panelRentInfo,
                    panelInput, panelTotal, panelBikeImage, panelBikeChoice, panelCenterBottom;          
    
                    
    // JFrame Order                
    private JFrame  order;
    
    
    
    // setting layout and calling init classes
    public void init() {
        
        setLayout(new BorderLayout());
        loadPictures();
        initControls();
        initMainPanels();
        
    
    }
    
    // load pics
    private void loadPictures()
    {
        bikes = new Image[NUMBEROFBIKES];
        bikes[0] = getImage(getCodeBase(), "bike1.jpg");
        bikes[1] = getImage(getCodeBase(), "bike2.jpg");
        bikes[2] = getImage(getCodeBase(), "bike3.jpg");
        bikes[3] = getImage(getCodeBase(), "bike4.jpg");
    } // end loadPictures
    
    // initialize main panels
    private void initMainPanels()
    {
        // draw panels
        panelWest = new JPanel();
        panelWest.setLayout(new BoxLayout(panelWest, BoxLayout.PAGE_AXIS));
        panelWest.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        
        panelCenter = new JPanel();
        panelCenter.setLayout(new BoxLayout(panelCenter, BoxLayout.PAGE_AXIS));
        panelCenter.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        panelCenterBottom = new JPanel(new GridLayout(1,2));

        // initialize more panels
        initSeasonsPanel();
        initRentInfoPanel();
        initBikeChoicePanel();
        
        
        // even our panels have panels
        panelWest.add(panelSeasons);
        
        panelCenter.add(panelRentInfo);
        panelCenter.add(panelCenterBottom);
        
        panelCenterBottom.add(panelBike);
        
    
        // calling main panels into applet
        add(panelWest, BorderLayout.WEST);
        add(panelCenter, BorderLayout.CENTER);
    }
    
    // initialize Rent Info Panel
    private void initRentInfoPanel()
    {
        // draw panels
        panelRentInfo = new JPanel();
        panelRentInfo.setLayout(new BoxLayout(panelRentInfo, BoxLayout.Y_AXIS));
        panelRentInfo.setBorder(BorderFactory.createTitledBorder("Rent Info"));

        panelInput = new JPanel(new FlowLayout());
        panelTotal = new JPanel(new FlowLayout());
        panelRentInfo.add(panelInput);
        panelRentInfo.add(panelTotal);
        
        // adding input to panels
        panelInput.add(labelNumberOfDays);
        panelInput.add(txfNumberOfDays);
        panelInput.add(btnCalculateTotal);
        
        panelTotal.add(labelTotal);
        panelTotal.add(labelTotalDisplay);      
    }
    
    // initialize Seasons Panel
    private void initSeasonsPanel()
    {
        // draw panels
        panelSeasons = new JPanel();
        panelSeasons.setLayout(new BoxLayout(panelSeasons, BoxLayout.PAGE_AXIS));
        panelSeasons.setBorder(BorderFactory.createTitledBorder("Choose Season"));
        
        panelSeasons.add(rbSpring);
        panelSeasons.add(rbSummer);
        panelSeasons.add(rbFall);
        panelSeasons.add(rbWinter);       
    }
    
    // initialize Controls
    private void initControls()
    {
        // create applet title
        labelTitle = new JLabel(appletTitle);
        labelTitle.setFont(new Font("Comic Sans MS", Font.BOLD, 20));
        labelTitle.setHorizontalAlignment(SwingConstants.CENTER);
        add(labelTitle, BorderLayout.NORTH);

        // init controls
        initRentInfoControls();
        initBikeChoiceControls();
        initSeasonsControls();
        
    }
    
    // initialize Bike Choice Panel
    private void initBikeChoicePanel()
    {
        // draw panels
        panelBike = new JPanel();
        panelBike.setLayout(new BoxLayout(panelBike, BoxLayout.Y_AXIS));
        panelBike.setBorder(BorderFactory.createTitledBorder("Choose Bike"));

        panelBikeChoice = new JPanel(new FlowLayout());
        panelBikeImage = new JPanel(new FlowLayout());
        
        panelBike.add(panelBikeChoice);
        panelBike.add(panelBikeImage);
        
        // adding input to panels
        panelBikeChoice.add(btnPrevBike);
        panelBikeChoice.add(btnNextBike);
        
        panelBikeImage.add(labelBikeImage);     
        
        
    }
    
    // initialize Rent Info Controls
    private void initRentInfoControls()
    {
        // draw controls
        labelNumberOfDays = new JLabel("Enter Number of Days:");
        labelTotal = new JLabel("Total Cost:");
        labelTotal.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
        labelTotalDisplay = new JLabel("$.00");
        labelTotalDisplay.setFont(new Font("Comic Sans MS", Font.BOLD, 18));
        
        txfNumberOfDays = new JTextField("0", 5);
        txfNumberOfDays.addActionListener(this);
        btnCalculateTotal = new JButton("Calculate Total");
    }
    
    // initialize Bike Choice Controls
    private void initBikeChoiceControls()
    {
        // draw controls
        labelBikeImage = new JLabel(new ImageIcon(bikes[0]));
        labelBikeImage.setHorizontalTextPosition(JLabel.CENTER);

        btnNextBike = new JButton("Next");
        btnPrevBike = new JButton("Previous");
        
        btnCalculateTotal.addActionListener(this);
        btnNextBike.addActionListener(this);
        btnPrevBike.addActionListener(this);
        
        
    }
    
    // initialize Seasons Controls
    private void initSeasonsControls()
    {
        // draw controls
        rbSpring = new JRadioButton("Spring", true);
        rbSummer = new JRadioButton("Summer        ");
        rbFall = new JRadioButton("Fall");
        rbWinter = new JRadioButton("Winter");
        
        // add item listeners
        rbSpring.addItemListener(this);
        rbSummer.addItemListener(this);
        rbFall.addItemListener(this);
        rbWinter.addItemListener(this);
        
        // add radio buttons to group
        ButtonGroup grpSeasons = new ButtonGroup();
        grpSeasons.add(rbSpring);
        grpSeasons.add(rbSummer);
        grpSeasons.add(rbFall);
        grpSeasons.add(rbWinter);       
    }
    
    
    
    // calculate total
    public double calculateTotal(int numberOfDays, double cost) {
        return cost * numberOfDays;
    }
    
    // return dollar amount
    public String getCurrency(double c) {
        NumberFormat numberFormat = new DecimalFormat("$#,###.00");
        return numberFormat.format(c);
    }
    
    //change based upon season input
    public void itemStateChanged(ItemEvent e) 
    {
        AbstractButton source = (AbstractButton)e.getSource();
        int state = e.getStateChange();
        
        // Set currentSeasonCost for inputed season
        if (state == ItemEvent.SELECTED) 
        {
            if (source == rbSpring) {
                currentSeasonCost = SPRINGPRICE;
                currentSeason = 0;
            }
            else if(source == rbSummer) {
                currentSeasonCost = SUMMERPRICE;
                currentSeason = 1;
            }
            else if(source == rbFall) {
                currentSeasonCost = FALLPRICE;
                currentSeason = 2;
            }
            else if(source == rbWinter) {
                currentSeasonCost = WINTERPRICE;
                currentSeason = 3;
            }
            updateTotal();
        }
    }
    
    // update the displayed total
    public void updateTotal()
    {
        int numDays = 0;
        numDays = Integer.parseInt(txfNumberOfDays.getText());
        
        // calculate and display total
        totalCost = calculateTotal(numDays, currentSeasonCost);
        labelTotalDisplay.setText(getCurrency(totalCost));
    }

    // Action Performed
    
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        
        if(source == btnCalculateTotal)
            updateTotal();
        
        else if(source == btnNextBike)
            nextBike();
        else if (source == btnPrevBike)
            prevBike();
    }
    
    // cycle bike image
    private void nextBike()
    {
        if (selectedBike == NUMBEROFBIKES-1)
            selectedBike = 0;
        else
            selectedBike++;
        
        labelBikeImage.setIcon(new ImageIcon(bikes[selectedBike]));
    }
    
    // cycle bike image
    private void prevBike()
    {
        if (selectedBike == 0)
            selectedBike = NUMBEROFBIKES-1;
        else
            selectedBike--;
        
        labelBikeImage.setIcon(new ImageIcon(bikes[selectedBike]));
    }
}