import java.awt.*;
import java.awt.font.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
import javax.swing.border.Border;

public class TheJStore {

    public static void main(String args[]) {
        try {

            JPanel theContentPane=new JPanel();
            final JTextArea notes=new JTextArea(10,90);
            final JTextField the_file=new JTextField();
            Font the_font = new Font("SansSerif", Font.BOLD, 16);
            the_file.setFont(the_font);
            notes.setFont(the_font);
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            JFrame frame = new JFrame("The Jstore");
            JButton select_java = new JButton("Upload .java File");
            JButton edit_notes = new JButton("Edit notes");
            JButton download_version = new JButton("Download Version");

            //ActionListener actionListener =
            select_java.addActionListener(new ActionListener() {
                                              public void actionPerformed(ActionEvent actionEvent) {
                                                  SelectaFile saf=new SelectaFile("Windows");
                                                  String[]the_ans=saf.get_File();
                                                  if (the_ans[0].equals("no_file")) {
                                                      //the_file.setText("no file selected");
                                                      JOptionPane.showMessageDialog(frame, "no file selected");
                                                  } else {
                                                      String thefilepath=the_ans[1].replace("\\", "/");
                                                      the_file.setText(the_ans[0]+"     "+thefilepath );
                                                      String the_answer=(new JStoreServletConnect()).store_version(the_ans[0],thefilepath,notes.getText()).trim() ;
                                                      the_file.setText(the_answer);
                                                  }
                                              }
                                          });                  
            ActionListener download_action=null;
            download_action = new ActionListener() {

                public void actionPerformed(ActionEvent actionEvent) {
                    try {
                        String filandver=the_file.getText().trim();
                        int blankspot=filandver.indexOf(" ");
                        String fil=filandver.substring(0,blankspot).trim();
                        String ver_part=filandver.substring(blankspot).trim();
                        int ver=Integer.parseInt(ver_part);
                        notes.setText((new JStoreServletConnect()).get_version(fil,ver));
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(frame, "no version found");
                    }
                }
            };                  

            download_version.addActionListener(download_action);
            ActionListener edit_notes_action=null;
            try {
                edit_notes_action = new ActionListener() {
                    public void actionPerformed(ActionEvent actionEvent) {
                        String fileandver=the_file.getText().trim();
                        int blankspot=fileandver.indexOf(" ");
                        String fil=fileandver.substring(0,blankspot).trim();
                        String ver_part=fileandver.substring(blankspot).trim();
                        int ver=Integer.parseInt(ver_part);
                        String new_notes=notes.getText();
                        String the_response=(new JStoreServletConnect()).update_notes(fil,ver,new_notes).trim();
                        if (the_response.equals("1"))
                            the_response=": Notes Updated";
                        else
                            the_response=": Notes Not Updated";                                
                        the_file.setText(fileandver+the_response);
                    }
                };                  
            } catch (Exception e) {
            }
            edit_notes.addActionListener(edit_notes_action);
            theContentPane = new JPanel(new BorderLayout());
            JPanel the_buttons=new JPanel(new FlowLayout());
            the_buttons.add(select_java);
            the_buttons.add(edit_notes);
            the_buttons.add(download_version);
            theContentPane.setOpaque(true);
            JPanel theNotesPane= new JPanel(new BorderLayout());
            JLabel NoteLabel=new JLabel();
            Border labelborder = BorderFactory.createLineBorder(Color.BLUE, 2);
            NoteLabel.setBorder(labelborder);
            NoteLabel.setOpaque(true);
            NoteLabel.setBackground(Color.WHITE);
            String HelpString="Enter Notes in TextArea Below. File Name and Version (or just File Name for upload) in Bottom TextField.";
            NoteLabel.setText(HelpString);
            theNotesPane.add(NoteLabel, BorderLayout.NORTH);
            theNotesPane.add(notes, BorderLayout.CENTER);
            theContentPane.add(theNotesPane, BorderLayout.CENTER);
            theContentPane.add(the_file, BorderLayout.SOUTH);
            the_file.setText(" ");
            theContentPane.add(the_buttons, BorderLayout.NORTH);
            frame.setContentPane(theContentPane);
            frame.getContentPane().setBackground(Color.WHITE);
            frame.pack();
            frame.setVisible(true);
            frame.setSize(650, 650);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}