package ac.essex.gp.cluster;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.InetAddress;

/**
 * Starts a basic HTTP Server
 */

public class GPServerFrame extends JFrame implements GPServerListener {

    protected JLabel serverStatus;
    protected JLabel gpStatus;
    protected GPServer server;
    protected JButton stop;

    protected JLabel lblTotalRequests, lblTotalEvaluations;
    protected int totalRequests;
    protected int totalEvaluations;

    JPanel c;
    JLabel ipAddress;
    Color old;

    public GPServerFrame() {

        // use the system look and feel where possible
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            System.out.println("Unable to load native look and feel");
        }              

        try {

        Container con = getContentPane();

        c = new JPanel();
        c.setLayout(new GridLayout(3, 1));

        serverStatus = new JLabel();
        serverStatus.setHorizontalAlignment(JLabel.CENTER);
        serverStatus.setBorder(BorderFactory.createTitledBorder("Server Status"));

        InetAddress localaddr = InetAddress.getLocalHost();
        ipAddress = new JLabel(localaddr.getHostAddress());
        ipAddress.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
        ipAddress.setHorizontalAlignment(JLabel.CENTER);
        ipAddress.setBorder(BorderFactory.createTitledBorder("IP Address"));

        gpStatus = new JLabel("Ready");
        gpStatus.setHorizontalAlignment(JLabel.CENTER);
        gpStatus.setBorder(BorderFactory.createTitledBorder("GP Status"));        

        c.add(ipAddress);
        c.add(serverStatus);
        c.add(gpStatus);

        JPanel statusBar = new JPanel(new GridLayout(1,3));
        lblTotalRequests = new JLabel();
        statusBar.add(lblTotalRequests);
        lblTotalEvaluations = new JLabel();
        statusBar.add(lblTotalEvaluations);
        stop = new JButton("Stop GP");
        stop.setEnabled(false);
        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                server.stopGP();
            }
        });
        statusBar.add(stop);

        con.add(c, BorderLayout.CENTER);
        con.add(statusBar, BorderLayout.SOUTH);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                server.stop();
                System.exit(0);
            }
        });



        try {
            setIconImage(new ImageIcon(getClass().getResource("/server-icon.gif")).getImage());
        } catch (Exception e) {
        }

        setTitle(GPServer.APP_NAME);
        setLocation(50,50);
        setSize(300,280);
        setVisible(true);

        server = new GPServer(this);

        old = c.getBackground();

        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, e.toString());
        }

    }

    public void onRequest(String request) {
        System.out.println("Server: " + request);
        totalRequests++;
        lblTotalRequests.setText("Requests: " + totalRequests);
    }

    public void onGPFinished(int evaluations) {
        stop.setEnabled(false);
        totalEvaluations+=evaluations;
        lblTotalEvaluations.setText("Evaluations: " + totalEvaluations);
    }

    public void onServerStatusUpdate(String message) {
        serverStatus.setForeground(Color.BLACK);
        serverStatus.setText(message);
    }

    public void onServerError(String message) {
        serverStatus.setForeground(Color.RED);
        serverStatus.setText(message);
    }

    public void onGPStarted() {
        stop.setEnabled(true);
    }

    public void onGPStatusUpdate(String message) {
        gpStatus.setText(message);
    }

    public void onMessage(String message) {
        final JFrame f = new JFrame();
        JLabel l = new JLabel(message);
        f.setTitle("Message");
        l.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
        f.add(l);
        f.setBackground(Color.WHITE);
        f.setSize(800, 200);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        new Thread() {
            public void run() {
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                }
                f.setVisible(false);
            }
        }.start();
    }

    public void onPing() {

        c.setBackground(Color.RED);
        new Thread() {
            public void run() {
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                }
                c.setBackground(old);
            }
        }.start();
    }

    public static void main(String[] args) {
        new GPServerFrame();
    }

}
