/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
 */
package feedback.client;

import java.io.IOException;
import javax.swing.JOptionPane;

/**
 *
 * @author callum
 */
public class FeedbackClientUI extends javax.swing.JFrame {


    private FeedbackClient client;
    private String[] answers;
    /**
     * Creates new form FeedbackClientUI
     */
    public FeedbackClientUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LBLTitle = new javax.swing.JLabel();
        LBLQuestion = new javax.swing.JLabel();
        PNLResponses = new javax.swing.JPanel();
        BTNAnswer1 = new javax.swing.JButton();
        BTNAnswer2 = new javax.swing.JButton();
        BTNAnswer3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Feedback client response panel");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        LBLTitle.setFont(new java.awt.Font("Yu Gothic UI Semibold", 0, 18)); // NOI18N
        LBLTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LBLTitle.setText("Feedback client response panel");

        LBLQuestion.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LBLQuestion.setText("Waiting for question from server...");

        BTNAnswer1.setText("Option 1");
        BTNAnswer1.setEnabled(false);
        BTNAnswer1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        BTNAnswer1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTNAnswer1ActionPerformed(evt);
            }
        });

        BTNAnswer2.setText("Option 2");
        BTNAnswer2.setEnabled(false);
        BTNAnswer2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        BTNAnswer2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTNAnswer2ActionPerformed(evt);
            }
        });

        BTNAnswer3.setText("Option 3");
        BTNAnswer3.setEnabled(false);
        BTNAnswer3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        BTNAnswer3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTNAnswer3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PNLResponsesLayout = new javax.swing.GroupLayout(PNLResponses);
        PNLResponses.setLayout(PNLResponsesLayout);
        PNLResponsesLayout.setHorizontalGroup(
            PNLResponsesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PNLResponsesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(BTNAnswer1, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(50, 50, 50)
                .addComponent(BTNAnswer2, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(50, 50, 50)
                .addComponent(BTNAnswer3, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        PNLResponsesLayout.setVerticalGroup(
            PNLResponsesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PNLResponsesLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(PNLResponsesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BTNAnswer1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BTNAnswer2, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BTNAnswer3, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(37, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(LBLTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(LBLQuestion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(PNLResponses, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(LBLTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(59, 59, 59)
                .addComponent(LBLQuestion)
                .addGap(33, 33, 33)
                .addComponent(PNLResponses, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    
    private void BTNAnswer1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTNAnswer1ActionPerformed
        client.sendResponse('A');
        disableButtons();
    }//GEN-LAST:event_BTNAnswer1ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        while (true) {
            try {
                client = new FeedbackClient();
                answers = client.getAnswers();

                LBLQuestion.setText(client.getQuestion());  //updates ui with the question

                //updates the buttons text
                BTNAnswer1.setText(answers[0]);
                BTNAnswer2.setText(answers[1]);
                BTNAnswer3.setText(answers[2]);

                //enable buttons 
                BTNAnswer1.setEnabled(true);
                BTNAnswer2.setEnabled(true);
                BTNAnswer3.setEnabled(true);
                
                break;
                
            } catch (RuntimeException e) {
                JOptionPane.showMessageDialog(this,
                    "Could not connect to the server. Is it running?",
                    "Connection Error.  retrying in 5 seconds...",
                    JOptionPane.ERROR_MESSAGE);
            }
            try {
                // Wait 5 seconds before retrying
                Thread.sleep(5000);
            } catch (InterruptedException ie) {
                System.err.println("Retry loop was somehow interrupted.  Shutting down.");
                System.exit(1);
            }
        }
    }//GEN-LAST:event_formWindowOpened

    
    private void BTNAnswer2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTNAnswer2ActionPerformed
        client.sendResponse('B');
        disableButtons();
    }//GEN-LAST:event_BTNAnswer2ActionPerformed

    private void BTNAnswer3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTNAnswer3ActionPerformed
        client.sendResponse('C');
        disableButtons();
    }//GEN-LAST:event_BTNAnswer3ActionPerformed

    private void disableButtons() {
        BTNAnswer1.setEnabled(false);
        BTNAnswer2.setEnabled(false);
        BTNAnswer3.setEnabled(false);
        JOptionPane.showMessageDialog(this, "Your feedback has been sent.");
    }
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton BTNAnswer1;
    public javax.swing.JButton BTNAnswer2;
    public javax.swing.JButton BTNAnswer3;
    public javax.swing.JLabel LBLQuestion;
    private javax.swing.JLabel LBLTitle;
    private javax.swing.JPanel PNLResponses;
    // End of variables declaration//GEN-END:variables
}
