using System.Security.Cryptography;
using System.Text;
using NUnit.Framework;

// The deal's hole card commitments: our Udon SHA-256 has to match the real one.
public class HoldemCommitTests
{
    private static byte[] Digest(uint[] words)
    {
        var bytes = new byte[32];
        for (int i = 0; i < 8; i++)
        {
            bytes[i * 4] = (byte)(words[i] >> 24);
            bytes[i * 4 + 1] = (byte)(words[i] >> 16);
            bytes[i * 4 + 2] = (byte)(words[i] >> 8);
            bytes[i * 4 + 3] = (byte)words[i];
        }
        return bytes;
    }

    [TestCase("")]
    [TestCase("abc")]
    [TestCase("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")]
    public void Sha256MatchesDotNet(string text)
    {
        var message = Encoding.ASCII.GetBytes(text);
        using (var sha = SHA256.Create())
        {
            CollectionAssert.AreEqual(sha.ComputeHash(message), Digest(HoldemCommit.Sha256(message)));
        }
    }

    // Lengths either side of the padding and block boundaries
    [TestCase(24)]
    [TestCase(55)]
    [TestCase(56)]
    [TestCase(63)]
    [TestCase(64)]
    [TestCase(65)]
    [TestCase(200)]
    public void Sha256MatchesDotNetAtBlockBoundaries(int length)
    {
        var message = new byte[length];
        for (int i = 0; i < length; i++) message[i] = (byte)(i * 37 + 11);
        using (var sha = SHA256.Create())
        {
            CollectionAssert.AreEqual(sha.ComputeHash(message), Digest(HoldemCommit.Sha256(message)));
        }
    }

    [Test]
    public void CommitIsTheFirstEightBytesOfTheBigEndianFields()
    {
        var message = new byte[24];
        int[] fields = { 7, 3, 51, 0, -123456789, int.MinValue };
        for (int f = 0; f < fields.Length; f++)
        {
            uint v = unchecked((uint)fields[f]);
            message[f * 4] = (byte)(v >> 24);
            message[f * 4 + 1] = (byte)(v >> 16);
            message[f * 4 + 2] = (byte)(v >> 8);
            message[f * 4 + 3] = (byte)v;
        }
        byte[] expected;
        using (var sha = SHA256.Create()) expected = sha.ComputeHash(message);
        long expectedCommit = 0;
        for (int i = 0; i < 8; i++) expectedCommit = (expectedCommit << 8) | expected[i];

        Assert.AreEqual(expectedCommit, HoldemCommit.Commit(7, 3, 51, 0, -123456789, int.MinValue));
    }

    [Test]
    public void ChangingAnyFieldChangesTheCommit()
    {
        long baseline = HoldemCommit.Commit(5, 2, 40, 12, 99, -99);
        Assert.AreEqual(baseline, HoldemCommit.Commit(5, 2, 40, 12, 99, -99), "commit must be deterministic");
        Assert.AreNotEqual(baseline, HoldemCommit.Commit(6, 2, 40, 12, 99, -99), "hand id");
        Assert.AreNotEqual(baseline, HoldemCommit.Commit(5, 3, 40, 12, 99, -99), "seat");
        Assert.AreNotEqual(baseline, HoldemCommit.Commit(5, 2, 41, 12, 99, -99), "card 0");
        Assert.AreNotEqual(baseline, HoldemCommit.Commit(5, 2, 40, 13, 99, -99), "card 1");
        Assert.AreNotEqual(baseline, HoldemCommit.Commit(5, 2, 40, 12, 98, -99), "salt a");
        Assert.AreNotEqual(baseline, HoldemCommit.Commit(5, 2, 40, 12, 99, -98), "salt b");
    }

    [Test]
    public void ShuffleExcludingDealsEachUnusedCardOnce()
    {
        var used = new bool[52];
        used[0] = used[13] = used[51] = true;
        var rest = HoldemDeckUtil.ShuffleExcluding(used);

        Assert.AreEqual(49, rest.Length);
        var seen = new bool[52];
        foreach (int card in rest)
        {
            Assert.IsFalse(used[card], "dealt a used card " + card);
            Assert.IsFalse(seen[card], "dealt " + card + " twice");
            seen[card] = true;
        }
    }
}
