using Godot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.Serialization;

public partial class Board : Sprite2D
{
    public bool _active = false, _moveMade = false, _won = false;
    public List<Shield> _shields = new();
    public Enemy _owner;
    public Actor _winner = null;
    public GoalName _winningPattern;
    public override void _Ready()
    {
        _shields = GetChildren().Where(c=>c is Shield).Cast<Shield>().ToList();
        int xSize = (int)Texture.GetSize().X;
        int ySize = (int)Texture.GetSize().Y;
        for (int i = 0; i < _shields.Count; i++)
        {
            _shields[i]._address = i;
            float shieldX = _shields[i].TextureNormal.GetSize().X;
            float shieldY = _shields[i].TextureNormal.GetSize().Y;

            _shields[i].Scale = new Vector2(shieldX / (xSize/3), shieldY / (ySize/3));
            int c = i % 3 - 1;
            int r = (int)(i/3) - 1;
            
            _shields[i].Position = new Vector2(xSize / 3f * c - shieldX / 2, ySize / 3f * r - shieldY / 2);
        }
    }


    public override void _Process(double DELTA_)
	{
        
    }

    public void Activate()
    {
        Disable(false);
    }

    public Actor CheckForWinner()
    {
        // List<List<int>> playerGoalsMet = _owner._playerOpponent.CheckGoals();
        // List<List<int>> ownerGoalsMet = _owner.CheckGoals();
        // _winner = playerGoalsMet.Count>0 ? _owner._playerOpponent : ownerGoalsMet.Count>0 ? _owner : null;
        // _won = _winner != null;
        
        return _winner;
    }

    public void ClaimOwnership(Enemy OWNER)
    {
        _owner = OWNER;
        _shields.ForEach(c=>c._owner = OWNER);
        _shields.ForEach(c=>c._board = this);
    }

    public void ClearBoard(){
        foreach (Shield shield in _shields)
        {
            // shield.Mark();
        }
    }

    public void Deactivate()
    {
        Disable(true);
    }

    public Shield GetShieldByTenant(Enemy TENANT)
    {
        return _shields.Single(c=>c._tenant == TENANT);
    }

    public Shield GetShieldByAddress(int ADDRESS)
    {
        return _shields.Single(c=>c._address == ADDRESS);
    }

    public List<Shield> GetShieldsByOwner(Actor ACTOR)
    {
        return _shields.Where(c=>c._marker == ACTOR).ToList();
    }

    //THIS SHOULD BE MOVED INTO ACTION LOGIC
    // public void RotateBoard(int DEGREES, int REPEAT)
    // {
    //     for (int i = 0; i < REPEAT; i++)
    //     {
    //         Rotation += DEGREES * ((float)Math.PI / 180);
    //         RenumberShields();
    //     }
    // }

    public void RenumberShields()
    {
        _shields = _shields.OrderBy(c => c.Position.Y).ThenBy(c => c.Position.X).ToList();

        for (int i = 0; i < _shields.Count; i++)
        {
            _shields[i]._address = i;
        }
        
    }

    public void Start()
    {
        ClearBoard();
        // _player._isTurn = true;
    }

    public void Disable(bool DISABLED)
    {
        _active = !DISABLED;
        Visible = !DISABLED;
        SetProcess(!DISABLED);
        _shields.ForEach(c=>c.Disable(DISABLED));
    }
}
