using NUnit.Framework;
using System.Linq;
using System.Text;
using TINK.Services.BluetoothLock.Crypto;

namespace TestTINKLib.Fixtures.ObjectTests.Services.BluetoothLock.Crypto
{
    [TestFixture]
    public class TestCryptoHelper
    {
        /// <summary>
        /// Ensures that decyption from haveltec- lib produces the same results than sharee lib.
        /// </summary>
        [Test]
        public void Test_DecryptStringFromBytes_Aes()
        {
            // keyCopri (value copied from debugging session of sharing_ble_lib/ haveltec code)
            var keyCopri = (new sbyte[] { -6, 53, 29, -112, 7, -83, -41, -7, 30, 45, -13, -2, -108, -29, -90, 71, 15, -74, -76, 32, 0, 0, 0, 0 }).Select(x => (byte)x).ToArray();

            // Encrypted seed value from lock (value copied from debugging session of sharing_ble_lib/ haveltec code)
            var seedLockEnc = (new sbyte[] { 50, 51, -40, 64, 42, 82, 97, -24, 20, -39, -15, 126, 119, -110, 47, -18 }).Select(x => (byte)x).ToArray();

            // Decrypted seed value from lock (value copied from debugging session of sharing_ble_lib/ haveltec code)
            var acces_key = (new sbyte[] { 19, -66, 55, 18, -106, -92, 70, -40, 117, -87, -19, 124, 19, 54, -18, -82 }).Select(x => (byte)x).ToArray();

            var decrypt = new  Cipher().Decrypt(keyCopri, seedLockEnc);

            Assert.IsTrue(acces_key.SequenceEqual(decrypt));
        }


        [Test]
        public void TestGetSeedLock()
        {
            // seed copri (value copied from debugging session of sharing_ble_lib/ haveltec code)
            byte[] seedCopri = (new sbyte[] { -7, -69, 16, -52, 88, 38, -92, 82, -99, -79, 19, 16, -41, -127, 51, 24 }).Select(x => (byte)x).ToArray();

            // keyCopri (value copied from debugging session of sharing_ble_lib/ haveltec code)
            byte[] keyCopri = (new sbyte[] { -6, 53, 29, -112, 7, -83, -41, -7, 30, 45, -13, -2, -108, -29, -90, 71, 15, -74, -76, 32, 0, 0, 0, 0 }).Select(x => (byte)x).ToArray();

            // Encrypted seed value from lock (value copied from debugging session of sharing_ble_lib/ haveltec code)
            byte[] seedLockEnc = (new sbyte[] { 92, 80, -36, -2, 101, -31, -23, -43, 71, 62, 126, -70, 54, -53, -119, -56 }).Select(x => (byte)x).ToArray();

            //// Decryped seed value? access values? from lock (value copied from debugging session of sharing_ble_lib/ haveltec code)
            byte[] seedLockDec = (new sbyte[] { 62, -51, 96, -80, 7, -84, 48, -104, 47, 51, -22, -23, 30, -10, -88, -97 }).Select(x => (byte)x).ToArray();

            var crypto = new AuthCryptoHelper(
                seedLockEnc,
                keyCopri,
                null);

            var result = crypto.GetSeedLock();

            Assert.IsTrue(seedLockDec.SequenceEqual(result)); 
        }


        [Test]
        public void TestGetAccessKeyEncrypted()
        {
            // seed copri (value copied from debugging session of sharing_ble_lib/ haveltec code)
            byte[] seedCopri = (new sbyte[] { -7, -69, 16, -52, 88, 38, -92, 82, -99, -79, 19, 16, -41, -127, 51, 24 }).Select(x => (byte)x).ToArray();

            // keyCopri (value copied from debugging session of sharing_ble_lib/ haveltec code)
            byte[] keyCopri = (new sbyte[] { -6, 53, 29, -112, 7, -83, -41, -7, 30, 45, -13, -2, -108, -29, -90, 71, 15, -74, -76, 32, 0, 0, 0, 0 }).Select(x => (byte)x).ToArray();

            // Encrypted seed value from lock (value copied from debugging session of sharing_ble_lib/ haveltec code)
            byte[] seedLockEnc = (new sbyte[] { 92, 80, -36, -2, 101, -31, -23, -43, 71, 62, 126, -70, 54, -53, -119, -56 }).Select(x => (byte)x).ToArray();

            // Decryped seed value? access values? from lock (value copied from debugging session of sharing_ble_lib/ haveltec code)
            byte[] seedLockDec = (new sbyte[] { 62, -51, 96, -80, 7, -84, 48, -104, 47, 51, -22, -23, 30, -10, -88, -97 }).Select(x => (byte)x).ToArray();

            var crypto = new AuthCryptoHelper(
                seedLockEnc,
                keyCopri,
                null);

            var result = crypto.GetSeedLock();

            Assert.AreEqual(
                Encoding.UTF8.GetString(seedLockDec),
                Encoding.UTF8.GetString(result));
        }
    }
}