using System.Reflection;
using NUnit.Framework;
using UnityEngine;

public class PokerStatsTests
{
    private const BindingFlags PrivateInstance = BindingFlags.NonPublic | BindingFlags.Instance;

    private GameObject root;
    private HoldemGame game;

    [SetUp]
    public void SetUp()
    {
        root = new GameObject("PokerStatsTests");
        game = root.AddComponent<HoldemGame>();
        game.seats = 4;
        game.players = new HoldemPlayer[HoldemGame.MAX_SEATS];
        game.playerState = new int[HoldemGame.MAX_SEATS];
        game.stacks = new int[HoldemGame.MAX_SEATS];
        game.roundContribution = new int[HoldemGame.MAX_SEATS];
        game.potContribution = new int[HoldemGame.MAX_SEATS];

        var dealt = new bool[HoldemGame.MAX_SEATS];
        for (int i = 0; i < game.seats; i++)
        {
            var playerObject = new GameObject("Seat" + i);
            playerObject.transform.SetParent(root.transform);
            game.players[i] = playerObject.AddComponent<HoldemPlayer>();
            game.players[i].game = game;
            game.players[i].seatIdx = i;
            game.playerState[i] = HoldemGame.PLAYER_PENDING;
            game.stacks[i] = 1000;
            dealt[i] = true;
        }
        Invoke("BeginPokerStatsHand", dealt);
    }

    [TearDown]
    public void TearDown()
    {
        Object.DestroyImmediate(root);
    }

    private object Invoke(string method, params object[] args)
    {
        MethodInfo info = typeof(HoldemGame).GetMethod(method, PrivateInstance);
        Assert.IsNotNull(info, method + " should exist");
        return info.Invoke(game, args);
    }

    private void Record(int seat, bool folded, int actionBet, int oldCurrentBet,
        int oldContribution, int oldStack, int oldCallOnlyMask = 0)
    {
        Invoke("RecordPokerAction", seat, folded, actionBet, oldCurrentBet,
            oldContribution, oldStack, oldCallOnlyMask);
    }

    private int Mask(int seat)
    {
        return ((int[])typeof(HoldemGame).GetField("pokerStatsMasks", PrivateInstance).GetValue(game))[seat];
    }

    [Test]
    public void ForcedBlindAndBigBlindCheckDoNotCountAsVpip()
    {
        game.bettingRound = HoldemGame.PREFLOP;

        Record(0, false, 0, 100, 100, 900);

        Assert.AreEqual(0, Mask(0) & (PlayerBalanceComponent.HAND_STAT_VPIP | PlayerBalanceComponent.HAND_STAT_PFR));
    }

    [Test]
    public void PreflopCallCountsVpipAndRaiseCountsVpipAndPfr()
    {
        game.bettingRound = HoldemGame.PREFLOP;

        Record(0, false, 100, 100, 0, 1000);
        Record(1, false, 200, 100, 100, 1000);

        Assert.AreNotEqual(0, Mask(0) & PlayerBalanceComponent.HAND_STAT_VPIP);
        Assert.AreEqual(0, Mask(0) & PlayerBalanceComponent.HAND_STAT_PFR);
        Assert.AreEqual(PlayerBalanceComponent.HAND_STAT_VPIP | PlayerBalanceComponent.HAND_STAT_PFR,
            Mask(1) & (PlayerBalanceComponent.HAND_STAT_VPIP | PlayerBalanceComponent.HAND_STAT_PFR));
    }

    [Test]
    public void ThreeBetOpportunityAndOpenRaiserFoldAreRecorded()
    {
        game.bettingRound = HoldemGame.PREFLOP;

        Record(0, false, 300, 100, 0, 1000); // open
        Record(1, false, 300, 300, 0, 1000); // call with a 3-bet opportunity
        Record(2, false, 500, 300, 100, 500); // short all-in 3-bet to 600
        Record(0, true, 0, 600, 300, 700); // opener folds to it

        Assert.AreNotEqual(0, Mask(1) & PlayerBalanceComponent.HAND_STAT_THREE_BET_OPPORTUNITY);
        Assert.AreNotEqual(0, Mask(2) & PlayerBalanceComponent.HAND_STAT_THREE_BET_OPPORTUNITY);
        Assert.AreNotEqual(0, Mask(2) & PlayerBalanceComponent.HAND_STAT_THREE_BET);
        Assert.AreNotEqual(0, Mask(2) & PlayerBalanceComponent.HAND_STAT_PFR);
        Assert.AreEqual(PlayerBalanceComponent.HAND_STAT_FOLD_TO_THREE_BET_OPPORTUNITY | PlayerBalanceComponent.HAND_STAT_FOLD_TO_THREE_BET,
            Mask(0) & (PlayerBalanceComponent.HAND_STAT_FOLD_TO_THREE_BET_OPPORTUNITY | PlayerBalanceComponent.HAND_STAT_FOLD_TO_THREE_BET));
    }

    [Test]
    public void FourBetBeforeOpenerActsCancelsDirectFoldToThreeBetOpportunity()
    {
        game.bettingRound = HoldemGame.PREFLOP;

        Record(0, false, 300, 100, 0, 1000);
        Record(1, false, 600, 300, 0, 1000);
        Record(2, false, 1000, 600, 0, 1000);
        Record(0, true, 0, 1000, 300, 700);

        Assert.AreEqual(0, Mask(0) & (PlayerBalanceComponent.HAND_STAT_FOLD_TO_THREE_BET_OPPORTUNITY | PlayerBalanceComponent.HAND_STAT_FOLD_TO_THREE_BET));
    }

    [Test]
    public void FlopCBetAndDirectResponsesAreRecordedUntilAPlayerRaises()
    {
        game.bettingRound = HoldemGame.PREFLOP;
        Record(0, false, 300, 100, 0, 1000);
        Invoke("PrepareFlopPokerStats");
        game.bettingRound = HoldemGame.FLOP;

        Record(0, false, 100, 0, 0, 700);
        Record(1, true, 0, 100, 0, 1000);
        Record(2, false, 300, 100, 0, 1000);
        Record(3, true, 0, 300, 0, 1000);

        Assert.AreEqual(PlayerBalanceComponent.HAND_STAT_CBET_OPPORTUNITY | PlayerBalanceComponent.HAND_STAT_CBET,
            Mask(0) & (PlayerBalanceComponent.HAND_STAT_CBET_OPPORTUNITY | PlayerBalanceComponent.HAND_STAT_CBET));
        Assert.AreEqual(PlayerBalanceComponent.HAND_STAT_FOLD_TO_CBET_OPPORTUNITY | PlayerBalanceComponent.HAND_STAT_FOLD_TO_CBET,
            Mask(1) & (PlayerBalanceComponent.HAND_STAT_FOLD_TO_CBET_OPPORTUNITY | PlayerBalanceComponent.HAND_STAT_FOLD_TO_CBET));
        Assert.AreNotEqual(0, Mask(2) & PlayerBalanceComponent.HAND_STAT_FOLD_TO_CBET_OPPORTUNITY);
        Assert.AreEqual(0, Mask(2) & PlayerBalanceComponent.HAND_STAT_FOLD_TO_CBET);
        Assert.AreEqual(0, Mask(3) & PlayerBalanceComponent.HAND_STAT_FOLD_TO_CBET_OPPORTUNITY,
            "After a raise, later players face the raise rather than the c-bet directly");
    }

    [Test]
    public void DonkBetPreventsCBetOpportunity()
    {
        game.bettingRound = HoldemGame.PREFLOP;
        Record(0, false, 300, 100, 0, 1000);
        Invoke("PrepareFlopPokerStats");
        game.bettingRound = HoldemGame.FLOP;

        Record(1, false, 100, 0, 0, 1000);
        Record(0, false, 100, 100, 0, 700);

        Assert.AreEqual(0, Mask(0) & (PlayerBalanceComponent.HAND_STAT_CBET_OPPORTUNITY | PlayerBalanceComponent.HAND_STAT_CBET));
    }

    [Test]
    public void AdvanceBettingClassifiesAcceptedActionBeforeMutatingContributions()
    {
        game.bettingRound = HoldemGame.PREFLOP;
        game.actingSeat = 0;
        game.currentBet = 100;
        game.minimumRaise = 100;
        game.roundContribution[0] = 0;
        game.playerState[0] = HoldemGame.PLAYER_ACTING;
        game.playerState[1] = HoldemGame.PLAYER_PENDING;
        game.lastTransitionMillis = 0;

        Assert.IsTrue(game.AdvanceBetting(1000, true, 100, true));
        Assert.AreNotEqual(0, Mask(0) & PlayerBalanceComponent.HAND_STAT_VPIP);
        Assert.AreEqual(0, Mask(0) & PlayerBalanceComponent.HAND_STAT_PFR);
    }

    [Test]
    public void ResetDiscardsProvisionalStats()
    {
        game.bettingRound = HoldemGame.PREFLOP;
        Record(0, false, 300, 100, 0, 1000);

        game._ResetHandState();

        Assert.IsFalse((bool)typeof(HoldemGame).GetField("pokerStatsHandActive", PrivateInstance).GetValue(game));
    }
}
