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

public partial class Player : Actor
{
	public bool _isTurn = false;
	public string _name;
    public Phone _phone;
    public Boss _boss;
    public Enemy _activeEnemy;
    public RichTextLabel _debug, _busDebug;
    public List<BusinessCard> _businessCards;

	public override void _Ready()
	{
        base._Ready();

        _debug = GetNode<RichTextLabel>("Debug");
        _busDebug = GetNode<RichTextLabel>("BusDebug");

        _phone = GetNode<Phone>("Phone");
        _phone.PassPlayer(this);

        _boss = GetNode<Boss>("Boss");
        _boss.PassPlayer(this);

        _businessCards = GetChildren().Where(c=>c is BusinessCard).Cast<BusinessCard>().ToList();
        for (int i = 0; i < _businessCards.Count; i++)
        {
            _businessCards[i].PassPlayer(this);
            if (_businessCards[i].Name.ToString().IndexOf("Default") > -1)
            {
                string goalNameString = _businessCards[i].Name.ToString().Replace("Default","").Replace("Alt","");
                GoalName goalName;
                Enum.TryParse(goalNameString, out goalName);
                _businessCards[i].AssignGoal(goalName);
            }
            else
            {
                _businessCards[i].AssignRandomGoal();
            }
        }
        
        Challenge(_boss);
	}

    public void Challenge(Enemy ENEMY)
    {
        _board?.Deactivate();
        ENEMY._board.Activate();
        _board = ENEMY._board;
        _activeEnemy = ENEMY;
        CheckGoals();

        string text = "";
        if (_activeEnemy is Mook)
        {
            text = "dragon " + _activeEnemy._owner._number + "\nmook " + _activeEnemy._number;
        }
        else if (_activeEnemy is Dragon)
        {
            text = "dragon " + _activeEnemy._number;
        }
        _debug.Text = text;
    }

    public void CheckGoals()
    {
        _businessCards.ForEach(b=>b.CheckGoal());
    }

    public void RunLost()
    {
        _debug.Text = "YOU LOSE!!";
    }

    public void RunWon()
    {
        _debug.Text = "YOU WIN!!";
    }

    // public bool CheckWin(List<Cell> CELLS)
    // {
    //     foreach (Goal.GoalName cond in _goal._eligibleGoals)
    //     {   
    //         foreach (List<int> condList in _goal._conditions[cond])
    //         {
    //             // if (condList.All(a=>CELLS.FirstOrDefault<Cell>(c=>c._address == a, null)?._owner == this))
    //             // {
    //             //     return true;
    //             // }
    //         }
    //     }
    //     return false;
    // }
    // public void MarkCell(Cell CELL)
    // {
    //     CELL._mark.Texture = _mark.Texture;
    //     // CELL._owner = this;
    // }

	public override void _Process(double delta)
	{
		if (Input.IsActionJustReleased("backspace"))
        {
            if (_board._owner._owner != null)
            {
                Challenge(_board._owner._owner);
            }
        }
	}

}
