File size: 8,072 Bytes
28b5ddf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class InterviewScreen extends JPanel {
    private ClearPassAIGUI controller;
    private JTextArea questionArea;
    private JTextArea answerInput;
    private JTextArea feedbackArea;
    private JButton generateBtn, nextBtn, evaluateBtn, submitBtn, readBtn, stopBtn;
    private JLabel timerLabel, counterLabel, welcomeLabel;
    private Timer timer;
    private int timeLeft = 60;
    private int answeredCount = 0;
    private int totalQuestions;

    public InterviewScreen(ClearPassAIGUI controller, int questionCount) {
        this.controller = controller;
        this.totalQuestions = questionCount;
        setLayout(new BorderLayout(10, 10));

        String username = controller.getCurrentUser();
        welcomeLabel = new JLabel("Welcome, " + username + "!");
        welcomeLabel.setFont(new Font("Arial", Font.BOLD, 16));
        welcomeLabel.setHorizontalAlignment(SwingConstants.CENTER);

        questionArea = createTextArea(false);
        answerInput = createTextArea(true);
        feedbackArea = createTextArea(false);
        disableCopyPaste(answerInput);

        generateBtn = new JButton("Generate Question");
        evaluateBtn = new JButton("Evaluate Answer");
        nextBtn = new JButton("Next Question");
        readBtn = new JButton("Read Question");
        stopBtn = new JButton("Stop Reading");
        submitBtn = new JButton("Submit Interview");
        timerLabel = new JLabel("Time Left: 60s");
        counterLabel = new JLabel(getCounterText());

        JPanel buttonPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 10, 5, 10);

        gbc.gridx = 0;
        buttonPanel.add(generateBtn, gbc);
        gbc.gridx = 1;
        buttonPanel.add(evaluateBtn, gbc);
        gbc.gridx = 2;
        buttonPanel.add(nextBtn, gbc);
        gbc.gridx = 3;
        buttonPanel.add(readBtn, gbc);
        gbc.gridx = 4;
        buttonPanel.add(stopBtn, gbc);
        gbc.gridx = 5;
        buttonPanel.add(timerLabel, gbc);
        gbc.gridx = 6;
        buttonPanel.add(counterLabel, gbc);

        JPanel topPanel = new JPanel(new BorderLayout());
        topPanel.add(welcomeLabel, BorderLayout.NORTH);
        topPanel.add(buttonPanel, BorderLayout.CENTER);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
        centerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        centerPanel.add(new JLabel("Generated Question:"));
        centerPanel.add(new JScrollPane(questionArea));
        centerPanel.add(Box.createVerticalStrut(10));
        centerPanel.add(new JLabel("Your Answer:"));
        centerPanel.add(new JScrollPane(answerInput));
        centerPanel.add(Box.createVerticalStrut(10));
        centerPanel.add(new JLabel("AI Feedback:"));
        centerPanel.add(new JScrollPane(feedbackArea));

        // Create bottom panel for submit button
        JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        submitBtn.setPreferredSize(new Dimension(200, 40)); // Make submit button larger
        bottomPanel.add(submitBtn);

        add(topPanel, BorderLayout.NORTH);
        add(centerPanel, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.SOUTH);

        addListeners();
    }

    private JTextArea createTextArea(boolean editable) {
        JTextArea ta = new JTextArea(5, 50);
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);
        ta.setEditable(editable);
        return ta;
    }

    private void disableCopyPaste(JTextArea ta) {
        ta.getInputMap().put(KeyStroke.getKeyStroke("ctrl C"), "none");
        ta.getInputMap().put(KeyStroke.getKeyStroke("ctrl V"), "none");
        ta.getInputMap().put(KeyStroke.getKeyStroke("ctrl X"), "none");
        ta.setComponentPopupMenu(null);
        ta.setTransferHandler(null);
    }

    private void addListeners() {
        generateBtn.addActionListener(e -> generateQuestion());
        evaluateBtn.addActionListener(e -> evaluateAnswer());
        nextBtn.addActionListener(e -> generateBtn.doClick());
        submitBtn.addActionListener(e -> {
            if (answeredCount > 0) {
                controller.finishSession();
            } else {
                JOptionPane.showMessageDialog(this, 
                    "Please answer at least one question before submitting.", 
                    "No Answers", 
                    JOptionPane.WARNING_MESSAGE);
            }
        });
        readBtn.addActionListener(e -> {
            String question = questionArea.getText().trim();
            if (!question.isEmpty()) {
                try {
                    controller.getTTSClient().speakText(question);
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(this,
                        "Error reading question: " + ex.getMessage(),
                        "Error",
                        JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        stopBtn.addActionListener(e -> {
            controller.getTTSClient().stopSpeaking();
        });
    }

    private void generateQuestion() {
        String role = controller.getRole();
        String username = controller.getCurrentUser();
        if (!role.isEmpty()) {
            String prompt = "Imagine you are an expert interviewer. The candidate is applying for the job role: \""
                    + role + "\". Please create a thoughtful and beginner-friendly interview question.";
            try {
                questionArea.setText(controller.getGeminiClient().generateResponse(prompt));
                answerInput.setText("");
                feedbackArea.setText("");
                startTimer();
            } catch (IOException ex) {
                questionArea.setText("Error: " + ex.getMessage());
            }
        }
    }

    private void evaluateAnswer() {
        String question = questionArea.getText().trim();
        String answer = answerInput.getText().trim();
        if (!question.isEmpty() && !answer.isEmpty()) {
            String evalPrompt = "Evaluate this answer to the question: \"" + question + "\".\n\nAnswer: \"" + answer
                    + "\"\n\nGive short, clear feedback.";
            try {
                String feedback = controller.getGeminiClient().generateResponse(evalPrompt);
                feedbackArea.setText(feedback);
                recordAnswer(question, answer, feedback);
            } catch (IOException ex) {
                feedbackArea.setText("Error: " + ex.getMessage());
            }
        }
    }

    private void startTimer() {
        timeLeft = 60;
        if (timer != null)
            timer.stop();
        timer = new Timer(1000, e -> {
            if (timeLeft <= 0) {
                timer.stop();
                nextBtn.doClick();
            } else {
                timeLeft--;
                timerLabel.setText("Time Left: " + timeLeft + "s");
            }
        });
        timer.start();
    }

    private void recordAnswer(String question, String answer, String feedback) {
        QuestionData qd = new QuestionData();
        qd.setQuestion(question);
        qd.setAnswer(answer);
        qd.setFeedback(feedback);
        controller.getDataModel().setHistory(qd);

        String filename = controller.getCurrentUser() + "_answers.txt";
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename, true))) {
            writer.write("Q: " + question + "\nA: " + answer + "\nF: " + feedback + "\n\n");
        } catch (IOException e) {
            e.printStackTrace();
        }

        answeredCount++;
        counterLabel.setText(getCounterText());

        if (answeredCount >= totalQuestions) {
            controller.finishSession();
        }
    }

    private String getCounterText() {
        return "Answered: " + answeredCount + "/" + totalQuestions;
    }
}