using Godot;
using System;
using System.Collections.Generic;
using System.Linq;

public partial class BusinessCard : TextureButton
{
    public Player _player;
    public Goal _goal;
    public GoalName _goalName;
    public bool _goalMet = false;

    public override void _Ready()
    {
        base._Ready();
        _goal = GetNode<Goal>("Goal");
    }

    public override void _Pressed()
    {
        base._Pressed();
        _player._busDebug.Text = _goalName.ToString() + (_goalMet ? "": " NOT") + " MET";
        if (_goalMet)
        {
            _player._activeEnemy.Defeat(_goalName);
            _player.Challenge(_player._activeEnemy._owner._board._owner);
        }
    }

    public void AssignGoal(GoalName GOALNAME)
    {
        _goalName = GOALNAME;
    }

    public void AssignRandomGoal()
    {
        Random r = new Random();
        AssignGoal(_goal._conditions.Keys.ElementAt(r.Next(_goal._conditions.Keys.Count)));
    }

    public void CheckGoal()
    {
        List<Cell> ownedCells = _player._board.GetCellsByOwner(_player);
        List<int> addresses = _goal.GetAddresses(_goalName);
        _goalMet = addresses.All(a=>ownedCells.Select(c=>c._address).ToList().IndexOf(a)>-1);
        if (_goalMet)
        {
            Modulate = new Color(1,1,1,1);
        }
        else
        {
            Modulate = new Color(1,1,1,0.3f);
        }
    }

    public void PassPlayer(Player PLAYER)
    {
        _player = PLAYER;
    }

}