using Godot;
using System;

public partial class asteroid : Area2D
{
    [Signal]
    public delegate void ExplodedEventHandler(Vector2 pos, int size, ship shooter);

    public enum AsteroidSize {LARGE, MEDIUM, SMALL};

    private string[] _largeAsteroidTexture = new string[]  {"res://assets/PNG/Meteors/meteorBrown_big1.png", 
                                            "res://assets/PNG/Meteors/meteorBrown_big4.png", 
                                            "res://assets/PNG/Meteors/meteorGrey_big4.png"};

    private string[] _mediumAsteroidTexture = new string[]  {"res://assets/PNG/Meteors/meteorGrey_med1.png",
                                            "res://assets/PNG/Meteors/meteorBrown_med3.png",
                                            "res://assets/PNG/Meteors/meteorBrown_med1.png"};

    private string[] _smallAsteroidTexture = new string[] {"res://assets/PNG/Meteors/meteorBrown_small1.png", 
                                            "res://assets/PNG/Meteors/meteorBrown_small2.png", 
                                            "res://assets/PNG/Meteors/meteorGrey_small1.png"};                                        
                                            

    [Export]
    public AsteroidSize Size;

    private int _speed = 200;

    private Vector2 _movementVector;

    private Sprite2D _sprite = new Sprite2D();

    private CollisionShape2D _colShape;

    public override void _Ready()
    {
        _sprite = GetNode<Sprite2D>("Sprite2D");
        _colShape = GetNode<CollisionShape2D>("CollisionShape2D");
        var rand = new Random();
        Rotation = (float)rand.NextDouble();
        Rotation = (float)((Rotation * 2) * 3.14);
        _movementVector = new Vector2(0, -1);


        CircleShape2D colShapeL;
        switch (Size)
        {
            case AsteroidSize.LARGE:
                _speed = rand.Next(5, 15);
                _sprite.Texture = GD.Load<Texture2D>(_largeAsteroidTexture[rand.Next(3)]);
                colShapeL = GD.Load<CircleShape2D>("res://assets/CollisionShapes/asteroid_cshape_big.tres");
                _colShape.SetDeferred("shape", colShapeL);
                break;
            
            case AsteroidSize.MEDIUM:
                _speed = rand.Next(25, 50);
                _sprite.Texture = GD.Load<Texture2D>(_mediumAsteroidTexture[rand.Next(3)]);
                colShapeL = GD.Load<CircleShape2D>("res://assets/CollisionShapes/asteroid_cshape_medcircle.tres");
                _colShape.SetDeferred("shape", colShapeL);
                break;

            case AsteroidSize.SMALL:
                _speed = rand.Next(50, 75);
                _sprite.Texture = GD.Load<Texture2D>(_smallAsteroidTexture[rand.Next(3)]);
                colShapeL = GD.Load<CircleShape2D>("res://assets/CollisionShapes/asteroid_cshape_smallcircle.tres");
                _colShape.SetDeferred("shape", colShapeL);
                break;
        }
    }

    public void Explode(ship shooter)
    {
        //DEBUG PRINT
        //GD.Print($"DEBUG: Explode() called for {Name} at {GlobalPosition}");
        //GD.Print($"DEBUG: {Name} calling Explode()");
        EmitSignal(SignalName.Exploded, GlobalPosition, (int)Size, shooter);
        QueueFree();
    }

    private void OnBodyEntered(Node body)
    {
        /*if (body is player player)
        {
            player.ShipDamage(30);
            GD.Print("player asteroid collide");
        }*/

        if (body is ship ship)
        {
            ship.ShipDamage((30));
            //GD.Print(("ship asteroid collide"));
            //GD.Print(ship.Name, ship.Health);
        }
        else if (body is StaticBody2D)
        {
            QueueFree();
        }
    }


    public override void _PhysicsProcess(double delta)
    {
        GlobalPosition += _movementVector.Rotated(Rotation) * _speed * (float)delta;
        CircleShape2D circleShape = (CircleShape2D)_colShape.Shape;
        var radius = circleShape.Radius;
        var screenSize = GetViewportRect().Size;

        /*
        if (GlobalPosition.Y + radius < 0)
        {
            GlobalPosition = new Vector2(GlobalPosition.X, ScreenSize.Y + radius);
        }
        else if (GlobalPosition.Y - radius > ScreenSize.Y)
        {
            GlobalPosition = new Vector2(GlobalPosition.X, -radius);
        }
        if (GlobalPosition.X + radius < 0)
        {
            GlobalPosition = new Vector2(ScreenSize.X + radius, GlobalPosition.Y);
        }
        else if (GlobalPosition.X - radius > ScreenSize.X)
        {
            GlobalPosition = new Vector2(-radius, GlobalPosition.Y);
        }
        */
    }
}
