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 pictureBoxShip;
    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 DrawingEntities<EntityShip,IAdditionalDrawingObject> _drawingEntities;
    private IAdditionalDrawingObject SetData()
    {
        Random random=new Random();
        int r = random.nextInt(3);
        if(r==0)
        {
            return new DrawingTrapezDeck();
        }
        if(r==1)
        {
            return new DrawingDeck();
        }
        else
        {
            return new  DrawingOvalDeck();
        }
    }
    private void Draw(DrawingShip _ship) {
        pictureBoxShip.removeAll();
        Random random = new Random();
        BufferedImage bmp = new BufferedImage(pictureBoxShip.getWidth(), pictureBoxShip.getHeight(),BufferedImage.TYPE_INT_RGB);
        Graphics gr = bmp.getGraphics();
        gr.setColor(new Color(238, 238, 238));
        gr.fillRect(0, 0, pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
        if (_ship != null) {
            _ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100),
                    pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
            _ship.DrawTransport(gr);
            JLabelSpeed.setText("Cкорость: " + _ship.GetShip().GetSpeed() + " ");
            JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " ");
            JLabelColor.setText(("Цвет: " + _ship.GetShip().GetBodyColor() + " "));
            JLabel imageOfShip = new JLabel();
            imageOfShip.setPreferredSize(pictureBoxShip.getSize());
            imageOfShip.setMinimumSize(new Dimension(1, 1));
            imageOfShip.setIcon(new ImageIcon(bmp));
            pictureBoxShip.add(imageOfShip,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);
        _drawingEntities=new DrawingEntities<>(10,10);
        ButtonCreate.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                Random random = new Random();
                Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
                EntityShip ship=new EntityShip(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst);
                IAdditionalDrawingObject deck = SetData();
                int DecksCount=random.nextInt(1,4);
                deck.SetAddEnum(DecksCount);
                if((_drawingEntities.Insert(ship)!=-1) & (_drawingEntities.Insert(deck)!=-1))
                {
                    JOptionPane.showMessageDialog(null,"Объект добавлен");
                    Draw(_drawingEntities.CreateShip());
                    LabelInfo.setText(_drawingEntities.indx+ " " + _drawingEntities.indy);
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
                }
            }
        });
        ButtonCreateModif.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Random random = new Random();
                Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
                Color colorSecond = JColorChooser.showDialog(null, "Цвет", null);
                EntityContainerShip _ship=new EntityContainerShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst,colorSecond,random.nextBoolean(),random.nextBoolean());
                IAdditionalDrawingObject deck = SetData();
                int DecksCount=random.nextInt(1,4);
                deck.SetAddEnum(DecksCount);
                if((_drawingEntities.Insert(_ship)!=-1) & (_drawingEntities.Insert(deck)!=-1))
                {
                    JOptionPane.showMessageDialog(null,"Объект добавлен");
                    Draw(_drawingEntities.CreateShip());
                    LabelInfo.setText(_drawingEntities.indx+ " " + _drawingEntities.indy);
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
                }
            }
        });
    }
}