import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;

public class FormParam extends JFrame{
    public JPanel MainPanel;
    private JPanel pictureBoxPlane;
    private JButton ButtonCreate;
    private JButton ButtonCreateModif;
    private JToolBar StatusStrip;
    private JLabel LabelInfo;
    private JLabel JLabelSpeed = new JLabel();
    private JLabel JLabelWeight = new JLabel();
    private JLabel JLabelColor = new JLabel();
    private DrawningEntities<EntityPlane,IDrawningIlluminator> _drawningEntities;
    private IDrawningIlluminator SetData()
    {
        Random random=new Random();
        int r = random.nextInt(3);
        if(r==0)
        {
            return new DrawningIlluminator();
        }
        if(r==1)
        {
            return new DrawningSqareIlluminator();
        }
        else
        {
            return new DrawningTriangleIlluminator();
        }
    }
    private void Draw(DrawningPlane _plane) {
        pictureBoxPlane.removeAll();
        Random random = new Random();
        BufferedImage bmp = new BufferedImage(pictureBoxPlane.getWidth(), pictureBoxPlane.getHeight(),BufferedImage.TYPE_INT_RGB);
        Graphics gr = bmp.getGraphics();
        gr.setColor(new Color(238, 238, 238));
        gr.fillRect(0, 0, pictureBoxPlane.getWidth(), pictureBoxPlane.getHeight());
        if (_plane != null) {
            _plane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBoxPlane.getWidth(), pictureBoxPlane.getHeight());
            _plane.DrawTransport(gr);
            JLabelSpeed.setText("Cкорость: " + _plane.GetPlane().GetSpeed() + " ");
            JLabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " ");
            JLabelColor.setText(("Цвет: " + _plane.GetPlane().GetBodyColor() + " "));
            JLabel imageOfPlane = new JLabel();
            imageOfPlane.setPreferredSize(pictureBoxPlane.getSize());
            imageOfPlane.setMinimumSize(new Dimension(1, 1));
            imageOfPlane.setIcon(new ImageIcon(bmp));
            pictureBoxPlane.add(imageOfPlane,BorderLayout.CENTER);
        }
        validate();
    }
    public FormParam()
    {
        Box LabelBox = Box.createHorizontalBox();
        LabelBox.setMinimumSize(new Dimension(1, 20));
        LabelBox.add(JLabelSpeed);
        LabelBox.add(JLabelWeight);
        LabelBox.add(JLabelColor);
        StatusStrip.add(LabelBox);
        _drawningEntities = new DrawningEntities<>(10,10);
        ButtonCreate.addActionListener(e -> {
            Random random = new Random();
            Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
            EntityPlane _plane = new EntityPlane(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst);
            IDrawningIlluminator illum = SetData();
            int IllumCount = random.nextInt(1,4) * 10;
            illum.SetIlluminatorCount(IllumCount);
            if((_drawningEntities.Insert(_plane)!=-1) & (_drawningEntities.Insert(illum)!=-1))
            {
                JOptionPane.showMessageDialog(null,"Объект добавлен");
                Draw(_drawningEntities.CreatePlane());
                LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
            }
        });
        ButtonCreateModif.addActionListener(e -> {
            Random random = new Random();
            Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
            Color colorSecond = JColorChooser.showDialog(null, "Цвет", null);
            EntityAirbus _plane = new EntityAirbus(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst, colorSecond, random.nextBoolean(), random.nextBoolean(), random.nextBoolean());
            IDrawningIlluminator illum = SetData();
            int IllumCount = random.nextInt(1,4) * 10;
            illum.SetIlluminatorCount(IllumCount);
            if((_drawningEntities.Insert(_plane)!=-1) & (_drawningEntities.Insert(illum)!=-1))
            {
                JOptionPane.showMessageDialog(null,"Объект добавлен");
                Draw(_drawningEntities.CreatePlane());
                LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
            }
        });
    }
}