import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;

class test extends application{

   private String[][] arrQuestions;
   private int n = 0;
   private String[] answers;

   private JRadioButton[] choicesRadio;
   private ButtonGroup radioGroup;
   private JButton ButtonNext, ButtonQuit;
   private JTextArea AnswerField;
   private JScrollPane ImagePane;
   private JOptionPane AlertMessage;
   
   public test(String Name, String LastName) throws IOException{
      
      questions q = new questions(); 
      arrQuestions = q.get_all_questions(false);
      answers = new String[arrQuestions.length + 3];
      if (Name.compareTo("") ==  0){
          answers[0] = "anonymous";
          answers[1] = "";
      }
      else{
          answers[0] = Name;
          answers[1] = LastName;
      }
      
      setupDate();
      nextQuestion();
      
      setSize(500, 500);
      setVisible(true);
   }
   
   private void setupDate(){
       
      Calendar now = Calendar.getInstance();
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
      answers[2] = formatter.format(now.getTime()); 
   }
   
   private void setupImagePane(String image){
       
       Icon img = new ImageIcon(conf.RootDirectory+image);
       JLabel labelIcon = new JLabel("", img, SwingConstants.CENTER);
       ImagePane = new JScrollPane(labelIcon);
       ImagePane.setToolTipText("Use the scrollbars if you cannot see the whole image");
       ImagePane.setBorder(BorderFactory.createEmptyBorder());
       ImagePane.setPreferredSize(new Dimension(330, 300));
       
   }
   
   private void nextQuestion() throws IOException{
       removeAllComponents();
       if(arrQuestions.length> n){
          if (arrQuestions[n][1].compareTo("") != 0)
              setupMultipleChoiceContainer();
          else
              setupFillInContainer();
       }
       else {
           saveAnswers();
           setupTestEndContainer();
       }
       
   }
   
   private void setupMultipleChoiceContainer(){
       JLabel labelQuestion = new JLabel(arrQuestions[n][0]);
       addComponent(labelQuestion, 0, 0, 2, 1);
       
       choicesRadio = new JRadioButton[5];
       radioGroup = new ButtonGroup();
       
       if (arrQuestions[n][6].compareTo("") != 0){
           setupImagePane(arrQuestions[n][6]);
           addComponent(ImagePane, 0, 1, 2, 1);
       }
       
       JPanel ChoicePanel = new JPanel();
       ChoicePanel.setLayout(new GridLayout(5, 1));
       
       char ChoiceLetter = 'A';
       
       for (int r= 1; r <= 5; r++){
           if (arrQuestions[n][r].compareTo("") != 0){
            choicesRadio[r-1] = new JRadioButton(ChoiceLetter+". "+arrQuestions[n][r]);
            choicesRadio[r-1].setToolTipText("Hit the radio button reffering to the best choice.");
            ChoicePanel.add(choicesRadio[r-1]);
            radioGroup.add(choicesRadio[r-1]);
            ChoiceLetter++;
           }
        }
           
        addComponent(ChoicePanel, 0, 3, 1, 1);
        
        ButtonNext = new JButton("Next");
        ButtonNext.setToolTipText("Next question");
        ButtonNext.addActionListener(new ButtonNextMChoiceHandler());
        addComponent(ButtonNext, 1, 4, 1, 1);
        
    }
   
   private void setupFillInContainer(){
       JLabel labelQuestion = new JLabel(arrQuestions[n][0]);
       addComponent(labelQuestion, 0, 0, 2, 1);
       
       addComponent(new JLabel(), 0, 0, 1, 1);
       
       if (arrQuestions[n][6].compareTo("") != 0){
           setupImagePane(arrQuestions[n][6]);
           addComponent(ImagePane, 0, 2, 2, 1);
       }
      
       AnswerField = new JTextArea(5, 30);
       AnswerField.setToolTipText("Please enter your response here");
       AnswerField.setLineWrap(true);
       AnswerField.setWrapStyleWord(true);
       addComponent(new JScrollPane(AnswerField), 0, 3, 2, 1);
        
       ButtonNext = new JButton("Next");
       ButtonNext.setToolTipText("Next question");
       ButtonNext.setEnabled(true);
       ButtonNext.addActionListener(new ButtonNextFIHandler());
       addComponent(ButtonNext, 1, 4, 1, 1);
       
   }
   
   private void setupTestEndContainer(){

       JLabel labelNotice = new JLabel("Thank you so much for responding to the questionnaire!");
       addComponent(labelNotice, 0, 0, 1, 1);
       
       JButton ButtonExit = new JButton("Exit");
       ButtonExit.setToolTipText("Click the button to end the application");
       ButtonExit.addActionListener(new ButtonExitHandler());
       addComponent(ButtonExit, 0, 1, 1, 1);
   }
   
   private void saveAnswers() throws IOException{
       responses rsp = new responses();
       rsp.saveAnswers(answers);
   }
   
   private class ButtonNextMChoiceHandler implements ActionListener{
        public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
            int selected = -1;
            for (int r = 0; r< choicesRadio.length; r++)
                if (choicesRadio[r] != null && choicesRadio[r].isSelected())
                    selected = r;
            if (selected != -1){
                char ResponseLetter = 'A';
                for (int r = 0; r < selected; r++)
                    ResponseLetter++;
                answers[n+3] = ""+ResponseLetter;
                n++;
                try{
                    nextQuestion();
                }
                catch(Exception exp){
                    System.err.println(exp.toString());
                }
            }
            else{
                AlertMessage = new JOptionPane();
                AlertMessage.showMessageDialog(null, "Please select your choice!");
            }
       } 
   }
   
   private class ButtonNextFIHandler implements ActionListener{
       public void actionPerformed(java.awt.event.ActionEvent actionEvent){
            answers[n+3] = AnswerField.getText();
            n++;
            try{
                nextQuestion();
            }
            catch(Exception exp){
                System.err.println(exp.toString());
            }
        }
   }
   
   private class ButtonExitHandler implements ActionListener{
        public void actionPerformed(ActionEvent event){
            questionnaire.dispose();
        }
    }
}