package de.ctdo.crashtest.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;

public class MainGui extends JFrame implements IGuiControl {
    private final JTextArea textWall = new JTextArea();
    private final JLabel countDown = new JLabel();
    private final JLabel extraField = new JLabel();
    private final JPanel lowerPanel = new JPanel();
    private final List<GuiEventListener> listenerList = new ArrayList<GuiEventListener>();

    public MainGui() {
        initGui();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //setBounds(0,0, 1280, 1024);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        setUndecorated(true);

        setCursor(getToolkit().createCustomCursor( new BufferedImage(3, 3, BufferedImage.TYPE_INT_ARGB),
                                                   new Point(0, 0), "null"));
        textWall.setCursor(getToolkit().createCustomCursor( new BufferedImage(3, 3, BufferedImage.TYPE_INT_ARGB),
                new Point(0, 0), "null"));

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                char lastKey = e.getKeyChar();

                for(GuiEventListener listener: listenerList) {
                    listener.keyPress(lastKey);
                }
            }
        });

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                for (GuiEventListener listener : listenerList) {
                    listener.windowClosing();
                }
            }
        });

        setVisible(true);
    }

    private void setCountDownText(final int tseconds) {

        countDown.setText(String.format(" %d:%02d.%1d", tseconds / 600, tseconds % 600 / 10, tseconds % 9));
        
        if(tseconds < 400) {
            double percentile = ((tseconds-100.0)/300.0);
            double red = 255.0 - percentile * 255.0;
            double green = 255.0 - red;

            if(red > 255) red = 255;
            if(red < 0) red = 0;
            if(green > 255) green = 255;
            if(green < 0) green = 0;

            Color fColor = new Color((int)red,(int)green, 0);
            countDown.setForeground(fColor);
        }
        else {
            countDown.setForeground(new Color(42, 190, 0));
        }
    }

    private void initGui() {
        Container container = getContentPane();
        container.setLayout(new BorderLayout());
        container.add(textWall, BorderLayout.NORTH);
        container.add(lowerPanel, BorderLayout.SOUTH);

        lowerPanel.setLayout(new BorderLayout());
        lowerPanel.add(countDown, BorderLayout.WEST);
        lowerPanel.add(extraField, BorderLayout.EAST);

        Font wallFont = new Font("Arcade Interlaced", Font.PLAIN, 66);
        Font lowerFont = new Font("Arcade Interlaced", Font.PLAIN, 80);
        Color fontColor = new Color(42, 190, 0);
        
        textWall.setFont(wallFont);
        textWall.setForeground(fontColor);
        textWall.setBackground(Color.BLACK);
        textWall.setText("follow the white rabbit...");
        textWall.setLineWrap(true);
        textWall.setWrapStyleWord(true);
        textWall.setFocusable(false);


        countDown.setFont(lowerFont);
        countDown.setHorizontalAlignment(SwingConstants.LEFT);
        countDown.setForeground(fontColor);
        extraField.setFont(lowerFont);
        extraField.setHorizontalAlignment(SwingConstants.RIGHT);
        extraField.setForeground(fontColor);

        lowerPanel.setBackground(Color.BLACK);
        container.setBackground(Color.BLACK);

        countDown.setText(" 15:00.0");
        extraField.setText("  ");
    }

    @Override
    public void setWall(final String message) {
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                textWall.setText(message);
            }
        };

        SwingUtilities.invokeLater(runnable);
    }

    @Override
    public void setExtra(final String text) {
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                String txt = text.substring(0, Math.min(text.length(), 8));
                extraField.setText(txt + " ");
            }
        };

        SwingUtilities.invokeLater(runnable);
    }

    @Override
    public void setCountDown(final int tseconds) {
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                setCountDownText(tseconds);
            }
        };

        SwingUtilities.invokeLater(runnable);
    }
    
    @Override
    public void showCountDown(final Boolean show) {
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                countDown.setVisible(show);
            }
        };

        SwingUtilities.invokeLater(runnable);
    }

    @Override
    public void addListener(final GuiEventListener listener) {
        listenerList.add(listener);
    }

}
