package ProjectElectricLocomotive;
import java.awt.*;
import java.security.cert.PolicyNode;
import java.util.Random;

public class DrawingLocomotive {
    public EntityLocomotive EntityLocomotive;
    protected IDrawingWheels _drawingWheels;
    private int _pictureWidth;
    private int _pictureHeight;
    public int _startPosX;
    public int _startPosY;
    private int _locoWidth = 150;
    private int _locoHeight = 50;

    public int GetPosX(){
        return _startPosX;
    }
    public int GetPosY(){
        return _startPosY;
    }
    public int GetWidth(){
        return _locoWidth;
    }
    public int GetHeight(){
        return _locoHeight;
    }

    public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int height)
    {
        if (width < _locoWidth || height < _locoHeight)
        {
            return;
        }
        _pictureWidth = width;
        _pictureHeight = height;
        EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);

        Random rnd = new Random();
        int WhatWheels = rnd.nextInt(0, 3);

        if(WhatWheels == 0) _drawingWheels = new DrawingWheel();
        if(WhatWheels == 1) _drawingWheels = new DrawingEmptyWheels();
        if(WhatWheels == 2) _drawingWheels = new DrawingWheelsBlueCrom();
    }

    protected DrawingLocomotive(int speed, double weight, Color bodyColor, int width,
                                int height, int locoWidth, int locoHeight)
    {
        this(speed,weight, bodyColor, width, height);
        if (width < _locoWidth || height < _locoHeight)
        {
            return;
        }
        _pictureWidth = width;
        _pictureHeight = height;
        _locoWidth = locoWidth;
        _locoHeight = locoHeight;
    }

    protected DrawingLocomotive(EntityLocomotive entityLocomotive, IDrawingWheels iDrawingWheels,
                                int width, int height){
        EntityLocomotive = entityLocomotive;
        _drawingWheels  = iDrawingWheels;
        _pictureWidth = width;
        _pictureHeight = height;
    }

    protected DrawingLocomotive(EntityLocomotive entityLocomotive, IDrawingWheels iDrawingWheels,
                                int width, int height, int locoWidth, int  locoHeight){
        this(entityLocomotive, iDrawingWheels, width, height);
        _locoWidth = locoWidth;
        _locoHeight = locoHeight;
    }

    public void SetWheelsCount(int wheelsCount){
        _drawingWheels.SetWheelsCount(wheelsCount);
    }

    //Установка позиции
    public void SetPosition(int x, int y)
    {
        if (x < 0 || x + _locoWidth > _pictureWidth)
        {
            x = _pictureWidth - _locoWidth;
        }
        if (y < 0 || y + _locoHeight > _pictureHeight)
        {
            y = _pictureHeight - _locoHeight;
        }
        _startPosX = x;
        _startPosY = y;
    }

    public void MoveTransport(DyrectionType direction) {
        if (EntityLocomotive == null) {
            return;
        }
        switch (direction) {
            case Left:
                if (_startPosX - EntityLocomotive.Step() > 0) {
                    _startPosX -= (int) EntityLocomotive.Step();
                }
                break;
            case Up:
                if (_startPosY - EntityLocomotive.Step() > 0) {
                    _startPosY -= (int) EntityLocomotive.Step();
                }
                break;
            case Right:
                if (_startPosX + EntityLocomotive.Step() + _locoWidth < _pictureWidth) {
                    _startPosX += (int) EntityLocomotive.Step();
                }
                break;
            case Down:
                if (_startPosY + EntityLocomotive.Step() + _locoHeight < _pictureHeight) {
                    _startPosY += (int) EntityLocomotive.Step();
                }
                break;
        }
    }

    public void DrawTransport(Graphics g)
    {
        {
            if (EntityLocomotive == null) return;
        }

        Color colorBlack = Color.BLACK;
        Color windows = Color.BLUE;
        Color bodyColor = EntityLocomotive.BodyColor;

        //локомотив
        g.setColor(bodyColor);
        Polygon locoP = new Polygon();
        locoP.addPoint(_startPosX, _startPosY + 40);
        locoP.addPoint(_startPosX, _startPosY + 30);
        locoP.addPoint(_startPosX + 20, _startPosY + 20);
        locoP.addPoint(_startPosX + 70, _startPosY + 20);
        locoP.addPoint(_startPosX +80, _startPosY + 30);
        locoP.addPoint(_startPosX +80, _startPosY + 40);
        locoP.addPoint(_startPosX +75, _startPosY + 45);
        locoP.addPoint(_startPosX +5, _startPosY + 45);
        locoP.addPoint(_startPosX, _startPosY + 40);
        g.fillPolygon(locoP);

        g.setColor(colorBlack);
        g.drawPolygon(locoP);

        //окошки
        Polygon window = new Polygon();
        window.addPoint(_startPosX + 10, _startPosY + 30);
        window.addPoint(_startPosX +15, _startPosY + 25);
        window.addPoint(_startPosX + 20, _startPosY + 25);
        window.addPoint(_startPosX + 20, _startPosY + 30);
        window.addPoint(_startPosX +10, _startPosY + 30);
        g.setColor(windows);
        g.fillPolygon(window);
        g.fillRect(_startPosX + 25, _startPosY + 25, 10, 5);
        g.setColor(Color.black);
        g.drawPolygon(window);
        g.drawRect(_startPosX + 25, _startPosY + 25, 10, 5);

        //обязательные колеса
        //loco
        g.fillOval(_startPosX + 10, _startPosY + 45, 9, 9);
        g.fillOval(_startPosX + 25, _startPosY + 45, 9, 9);
        g.fillOval(_startPosX + 50, _startPosY + 45, 9, 9);
        g.fillOval(_startPosX + 65, _startPosY + 45, 9, 9);

        //telejka
        Polygon telega = new Polygon();

        telega.addPoint(_startPosX + 90, _startPosY + 25);
        telega.addPoint(_startPosX + 95, _startPosY + 20);
        telega.addPoint(_startPosX + 145, _startPosY + 20);
        telega.addPoint(_startPosX + 150, _startPosY + 25);
        telega.addPoint(_startPosX + 150, _startPosY + 45);
        telega.addPoint(_startPosX + 90, _startPosY + 45);
        telega.addPoint(_startPosX + 90, _startPosY + 25);

        g.setColor(bodyColor);
        g.fillPolygon(telega);
        g.setColor(colorBlack);
        g.drawPolygon(telega);

        //телега окна
        g.setColor(colorBlack);
        g.drawLine(_startPosX + 80, _startPosY + 40, _startPosX + 90, _startPosY + 40);
        g.setColor(windows);        g.fillRect(_startPosX + 95, _startPosY + 30, 10, 5);
        g.fillRect(_startPosX + 115, _startPosY + 30, 10, 5);
        g.fillRect(_startPosX + 135, _startPosY + 30, 10, 5);

    _drawingWheels.DrawWheels(g, colorBlack, _startPosX, _startPosY, 9,9);
    }

    public boolean CanMove(DyrectionType direction)
    {
        if (EntityLocomotive == null)
        {
            return false;
        }
        switch(direction) {
            //влево
            case Left:
                if (_startPosX - EntityLocomotive.Step() > 0) return true;
                break;
            case Up:
                if (_startPosY - EntityLocomotive.Step() > 0) return true;
                break;
            case Right:
                if (_startPosX + EntityLocomotive.Step() < _pictureWidth) return true;
                break;
            case Down:
                if (_startPosY + EntityLocomotive.Step() < _pictureHeight) return true;
                break;
        }
        return false;
    }

    public IMoveableObject GetMoveableObject() {
        return new DrawingObjectLocomotive(this);
    }
}