using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Artilleries
{
    internal class DrawingArtillery
    {
        public EntityArtillery Artillery { protected set; get; }
        protected float _startPosX;
        protected float _startPosY;
        private int? _pictureWidth = null;
        private int? _pictureHeight = null;
        protected readonly int _artilleryWidth = 80;
        protected readonly int _artilleryHeight = 50;

        public DrawingArtillery(int speed, float weight, Color bodyColor)
        {
            Artillery = new EntityArtillery(speed, weight, bodyColor);
        }

        protected DrawingArtillery(int speed, float weight, Color bodyColor, int artilleryWidth, int artilleryHeight) : this(speed, weight, bodyColor)
        {
            _artilleryWidth = artilleryWidth;
            _artilleryHeight = artilleryHeight;
        }

        public void SetPosition(int x, int y, int width, int height)
        {
            if (x < 0 || x + _artilleryWidth >= width)
            {
                return;
            }
            if (y < 0 || y + _artilleryHeight >= height)
            {
                return;
            }
            _startPosX = x;
            _startPosY = y;
            _pictureWidth = width;
            _pictureHeight = height;
        }

        public void MoveTransport(Direction direction)
        {
            if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
            {
                return;
            }
            switch (direction)
            {
                case Direction.Right:
                    if (_startPosX + _artilleryWidth + Artillery.Step < _pictureWidth)
                    {
                        _startPosX += Artillery.Step;
                    }
                    break;

                case Direction.Left:
                    if (_startPosX - Artillery.Step >= 0)
                    {
                        _startPosX -= Artillery.Step;
                    }
                    break;

                case Direction.Up:
                    if (_startPosY - Artillery.Step >= 0)
                    {
                        _startPosY -= Artillery.Step;
                    }
                    break;

                case Direction.Down:
                    if (_startPosY + _artilleryHeight + Artillery.Step < _pictureHeight)
                    {
                        _startPosY += Artillery.Step;
                    }
                    break;
            }
        }

        public virtual void DrawTransport(Graphics g)
        {
            if (_startPosX < 0 || _startPosY < 0 || !_pictureHeight.HasValue || !_pictureWidth.HasValue)
            {
                return;
            }
            Brush brush = new SolidBrush(Artillery?.BodyColor ?? Color.Black);
            g.FillRectangle(brush, _startPosX + _artilleryWidth / 8 * 2, _startPosY, _artilleryWidth / 8 * 4, _artilleryHeight / 5);
            g.FillRectangle(brush, _startPosX, _startPosY + _artilleryHeight / 5, _artilleryWidth, _artilleryHeight / 3);

            Brush blackBrush = new SolidBrush(Color.Black);
            Brush grayBrush = new SolidBrush(Color.Gray);
            g.FillEllipse(grayBrush, _startPosX, _startPosY + _artilleryHeight * 2 / 5, _artilleryWidth, _artilleryHeight * 2 / 5);
            g.FillEllipse(blackBrush, _startPosX + _artilleryWidth / 20, _startPosY + _artilleryHeight * 7 / 15, _artilleryWidth * 4 / 20, _artilleryHeight * 10 / 32);
            g.FillEllipse(blackBrush, _startPosX + _artilleryWidth * 5 / 20, _startPosY + _artilleryHeight * 9 / 16, _artilleryWidth * 3 / 20, _artilleryHeight * 8 / 32);
            g.FillEllipse(blackBrush, _startPosX + _artilleryWidth * 8 / 20, _startPosY + _artilleryHeight * 10 / 16, _artilleryWidth * 2 / 20, _artilleryHeight * 6 / 32);
            g.FillEllipse(blackBrush, _startPosX + _artilleryWidth * 10 / 20, _startPosY + _artilleryHeight * 10 / 16, _artilleryWidth * 2 / 20, _artilleryHeight * 6 / 32);
            g.FillEllipse(blackBrush, _startPosX + _artilleryWidth * 12 / 20, _startPosY + _artilleryHeight * 9 / 16, _artilleryWidth * 3 / 20, _artilleryHeight * 8 / 32);
            g.FillEllipse(blackBrush, _startPosX + _artilleryWidth * 15 / 20, _startPosY + _artilleryHeight * 7 / 15, _artilleryWidth * 4 / 20, _artilleryHeight * 10 / 32);
        }

        public void ChangeBorders(int width, int height)
        {
            _pictureWidth = width;
            _pictureHeight = height;
            if (_pictureWidth <= _artilleryWidth || _pictureHeight <= _artilleryHeight)
            {
                _pictureWidth = null;
                _pictureHeight = null;
                return;
            }
            if (_startPosX + _artilleryWidth > _pictureWidth)
            {
                _startPosX = _pictureWidth.Value - _artilleryWidth;
            }
            if (_startPosY + _artilleryHeight > _pictureHeight)
            {
                _startPosY = _pictureHeight.Value - _artilleryHeight;
            }
        }

        public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
        {
            return (_startPosX, _startPosX + _artilleryWidth - 1, _startPosY, _startPosY + _artilleryHeight - 1);
        }
    }
}