/**
 
 */
package svet.co;

import FormEvents.ButtonsEvents;

import com.evelopers.unimod.runtime.ControlledObject;
import com.evelopers.unimod.runtime.context.StateMachineContext;
import com.evelopers.unimod.core.stateworks.Event;
import com.evelopers.unimod.runtime.context.StateMachineContextImpl;
import javax.swing.*;
import javax.swing.border.*;

import java.awt.*;
import java.awt.event.*;


class MainWindow extends JFrame implements Runnable, ActionListener {
  static final long serialVersionUID = 0;

  static private int width = 800, height = 600;// razmery okna

  Color c1 = new Color(0x000000);

  Color c2 = new Color(0x000000);

  Color c3 = new Color(0x00ff00);

  Color c4 = new Color(0xff0000);

  Color c5 = new Color(0x000000);

  ImageIcon roadImg, humanUpImage, humanDownImage, carRightImage,
      carLeftImage;

  public JButton buttonHumanDown;

  public JButton buttonHumanUp;

  public JButton buttonCarLeft;

  public JButton buttonCarRight;

  class ImagePanel extends JPanel {
    /**
     
     */
    private static final long serialVersionUID = -7150943989162204260L;

    public ImagePanel() {
      super();
    }

    public void paintComponent(Graphics g) {
      super.paintComponent(g)// paint background
      roadImg.paintIcon(this, g, 00);// 25, 140,
      if (TrafficEngine.h_up)
        humanUpImage.paintIcon(this, g, 255270 - TrafficEngine.yup);
      if (TrafficEngine.h_down)
        humanDownImage.paintIcon(this, g, 195+ TrafficEngine.ydown);
      if (TrafficEngine.c_right)
        carRightImage.paintIcon(this, g, + TrafficEngine.xright, 180);
      if (TrafficEngine.c_left)
        carLeftImage.paintIcon(this, g, 400 - TrafficEngine.xleft, 90);
    }
  }

  class LightsPanel extends JPanel {
    /**
     
     */
    private static final long serialVersionUID = 7269465067508865046L;

    public LightsPanel() {
      super();
    }

    public void paintComponent(Graphics g) {
      super.paintComponent(g)// paint background
      Graphics offGr = g;
      width = this.getWidth();// 150;
      height = this.getHeight();// 350;
      int r = 25;
      int d = r * 2;
      offGr.setColor(c1);
      offGr.fillOval(width * - r, height / - r, d, d);

      offGr.setColor(c2);
      offGr.fillOval(width * - r, height / - r, d, d);

      offGr.setColor(c3);
      offGr.fillOval(width * - r, height * - r, d, d);

      offGr.setColor(c4);
      offGr.fillOval(width / - r, height / - r, d, d);

      offGr.setColor(c5);
      offGr.fillOval(width / - r, height * - r, d, d);
    }
  }

  MainWindow() {
    super("Traffic lights");
    String prefix = "/home/magomedov/workspace/Svetofor/res/";
    roadImg = new ImageIcon(prefix + "road.png");
    humanUpImage = new ImageIcon(prefix + "human_up.png");
    humanDownImage = new ImageIcon(prefix + "human_down.png");
    carRightImage = new ImageIcon(prefix + "car_right.png");
    carLeftImage = new ImageIcon(prefix + "car_left.png");
    System.getProperties().list(System.out);
    try {
      // Runtime.getRuntime().exec("nautilus .");
    catch (Exception e) {
    }

    Container c = getContentPane();
    c.setLayout(null);

    ImagePanel imgPan = new ImagePanel();
    c.add(imgPan);
    imgPan.setBounds(30220500320);
    imgPan.setBorder(BorderFactory.createEtchedBorder());
    imgPan.setVisible(true);

    LightsPanel lghPan = new LightsPanel();
    c.add(lghPan);
    lghPan.setBounds(3020500160);
    lghPan.setBorder(BorderFactory.createEtchedBorder());
    lghPan.setVisible(true);

    JLabel roadLabel = new JLabel("Pedestrian crossing", JLabel.CENTER);
    roadLabel.setBounds(3054050020);
    roadLabel.setBorder(BorderFactory.createEtchedBorder());
    c.add(roadLabel);

    JLabel humanLabel = new JLabel("Signals for pedestrians", JLabel.CENTER);
    humanLabel.setBounds(3018025020);
    humanLabel.setBorder(BorderFactory.createEtchedBorder());
    c.add(humanLabel);

    JLabel carLabel = new JLabel("Signals for cars", JLabel.CENTER);
    carLabel.setBounds(28018025020);
    carLabel.setBorder(BorderFactory.createEtchedBorder());
    c.add(carLabel);

    JPanel humanButtons = new JPanel();
    c.add(humanButtons);
    humanButtons.setBounds(550250210100);
    humanButtons.setLayout(new GridLayout(21));
    humanButtons.setBorder(new TitledBorder(BorderFactory
        .createEtchedBorder()"Add pedestrian"));

    buttonHumanDown = new JButton("bottom");
    buttonHumanDown.addActionListener(this);
    buttonHumanDown
        .setToolTipText("Add pedestrian to the bottom of the road");
    humanButtons.add(buttonHumanDown);
    buttonHumanDown.setVisible(true);

    buttonHumanUp = new JButton("top");
    buttonHumanUp.addActionListener(this);
    buttonHumanUp.setToolTipText("Add pedestrian to the top of the road");
    humanButtons.add(buttonHumanUp);
    buttonHumanUp.setVisible(true);

    humanButtons.setVisible(true);

    JPanel carButtons = new JPanel();
    c.add(carButtons);
    carButtons.setBounds(550400210100);
    carButtons.setLayout(new GridLayout(21));
    carButtons.setBorder(new TitledBorder(BorderFactory
        .createEtchedBorder()"Add car"));// BorderFactory.createLineBorder(Color.BLACK));

    buttonCarLeft = new JButton("left");
    buttonCarLeft.addActionListener(this);
    buttonCarLeft.setToolTipText("Add left car to the road");
    carButtons.add(buttonCarLeft);
    buttonCarLeft.setVisible(true);

    buttonCarRight = new JButton("right");
    buttonCarRight.addActionListener(this);
    buttonCarRight.setToolTipText("Add right car to the road");
    carButtons.add(buttonCarRight);
    buttonCarRight.setVisible(true);

    carButtons.setVisible(true);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(width, height);
    setLocation(100100);
    setBackground(Color.WHITE);
    setResizable(false);
    setVisible(true);
  }

  public void sendEvent(String event) {
    ButtonsEvents.engine.getEventManager().handle(new Event(event),
        StateMachineContextImpl.create());
  }

  public void actionPerformed(ActionEvent e) {

    if (e.getSource() == buttonHumanDown)
      sendEvent(ButtonsEvents.E011);
    if (e.getSource() == buttonHumanUp)
      sendEvent(ButtonsEvents.E012);
    if (e.getSource() == buttonCarLeft)
      sendEvent(ButtonsEvents.E021);
    if (e.getSource() == buttonCarRight)
      sendEvent(ButtonsEvents.E022);

  }

  public void free() {
  };

  public void paint(Graphics g) {
    super.paintComponents(g);
  }

  public void run() {
    repaint();
  }
}

public class DrawingWindows implements ControlledObject {

  static MainWindow sl = new MainWindow();

  TrafficEngine trEn = new TrafficEngine();

  /**
   * @unimod.action.descr Defaults are setting
   */
  public void z0(StateMachineContext context) {
    sl.paint(sl.getGraphics());
    sl.run();
    TrafficEngine.h_up = false;
    TrafficEngine.h_down = false;
    TrafficEngine.c_left = false;
    TrafficEngine.c_right = false;
    TrafficEngine.hurry = false;
    TrafficEngine.stopping = false;
    TrafficEngine.xleft = 0;
    TrafficEngine.xright = 0;
    TrafficEngine.yup = 0;
    TrafficEngine.ydown = 0;
  }

  /**
   * @unimod.action.descr Cars - green turnes off
   */
  public void z1(StateMachineContext context) {
    sl.c3 = Color.BLACK;
    sl.run();
  }

  /**
   
   * @unimod.action.descr Cars - green turnes on
   */
  public void z2(StateMachineContext context) {
    sl.c3 = Color.GREEN;
    sl.run();
  }

  /**
   * @unimod.action.descr Cars - yellow turns on
   */
  public void z3(StateMachineContext context) {
    sl.c2 = Color.YELLOW;
    sl.run();
  }

  /**
   * @unimod.action.descr Cars - yellow turns off; red turnes on
   */
  public void z4(StateMachineContext context) {
    sl.c2 = Color.BLACK;
    sl.c1 = Color.RED;
    sl.run();
  }

  /**
   * @unimod.action.descr People - red turnes off; green turnes on
   */
  public void z5(StateMachineContext context) {
    sl.c4 = Color.BLACK;
    sl.c5 = Color.GREEN;
    sl.run();
  }

  /**
   * @unimod.action.descr People - green turnes on
   */
  public void z6(StateMachineContext context) {
    sl.c5 = Color.GREEN;
    sl.run();
  }

  /**
   * @unimod.action.descr Peopel - green turnes off
   */
  public void z7(StateMachineContext context) {
    sl.c5 = Color.BLACK;
    sl.run();
  }

  /**
   * @unimod.action.descr People - red turnes on; Cars - yellow turnes on; red
   *                      turnes on
   */
  public void z8(StateMachineContext context) {
    sl.c4 = Color.RED;
    sl.c2 = Color.YELLOW;
    sl.c1 = Color.RED;
    sl.run();
  }

  /**
   * @unimod.action.descr Cars - green turnes on; yellow turnes off; red
   *                      turnes off
   */
  public void z9(StateMachineContext context) {
    sl.c1 = Color.BLACK;
    sl.c2 = Color.BLACK;
    sl.c3 = Color.GREEN;
    sl.run();
  }

  /**
   * @unimod.action.descr Humans can go message
   */
  public void z10(StateMachineContext context) {
    sl.sendEvent(ButtonsEvents.E03);
  }

  /**
   * @unimod.action.descr Humans should hurry message
   */
  public void z11(StateMachineContext context) {
    sl.sendEvent(ButtonsEvents.E04);
  }

  /**
   * @unimod.action.descr Everyone stops message
   */
  public void z12(StateMachineContext context) {
    sl.sendEvent(ButtonsEvents.E07);
  }

  /**
   * @unimod.action.descr Cars can drive message
   */
  public void z13(StateMachineContext context) {
    sl.sendEvent(ButtonsEvents.E05);
  }

  /**
   * @unimod.action.descr Cars should hurry message
   */
  public void z14(StateMachineContext context) {
    sl.sendEvent(ButtonsEvents.E06);
  }

  /**
   * @unimod.action.descr Are there any humans?
   */
  public boolean x1(StateMachineContext context) {
    return TrafficEngine.h_down || TrafficEngine.h_up;
  }
  /**
   * @unimod.action.descr Are there any cars?
   */
  public boolean x2(StateMachineContext context) {
    return TrafficEngine.c_left || TrafficEngine.c_right;
  }

}