using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CPF;
using System.Text;
using Microsoft.SPOT;
using System.Threading;
using HWModules;
using SWModules;

namespace CPFUnitTests
{
    [TestClass]
    public class UnitTestIridium
    {
        static UnitTestIridium()
        {
        }

        [TestMethod]
        public void TestIridiumSBDD2()
        {
            Init();
            int err = 0;
            while (err == 0)
            {
                err = imodem.waitForResponse(NAL_A3LAR.commands.sbdd2);
                _messageQueue.processQ();
                Thread.Sleep(100);
            }
            if (err < 0)
                Assert.Fail("Failed to get response from SBDD2 Message");
        }
        
        /// <summary>
        /// Test will fail if not outside, should we change CREG handling to pass on any response?
        /// </summary>
        // TODO P1 This unit test points out a programming error that even if 
        //      a CREG response occurs, it will only suceed on successful registration.
        [TestMethod]
        public void TestIridiumCREG()
        {
            Init();
            int err = 0;
            while ((err = imodem.waitForResponse(NAL_A3LAR.commands.creg)) == 0)
            {
                _messageQueue.processQ();
                Thread.Sleep(100);
            }
            if (err < 0)
                Assert.Fail();
        }
        
        [TestMethod]
        public void TestIridiumCSQ()
        {
            Init();
            int err = 0;
            while ((err = imodem.waitForResponse(NAL_A3LAR.commands.csq)) == 0)
            {
                _messageQueue.processQ();
                Thread.Sleep(100);
            }
            if (err < 0)
                Assert.Fail();
        }


        [TestMethod]
        public void TestIridiumSBDWT()
        {
            
            Init();
            //SurfaceOps.buildSBDWTStatusMsg();
            imodem.SBDWTMsg = new StringBuilder("UNIT TEST\r");

            int err = 0;
            while ((err = imodem.waitForResponse(NAL_A3LAR.commands.sendSBDWT)) == 0)
            {
                _messageQueue.processQ();
                Thread.Sleep(100);
            }
            if (err < 0)
                Assert.Fail();

            while ((err = imodem.waitForResponse(NAL_A3LAR.commands.loadSBDWTBuffer)) == 0)
            {
                _messageQueue.processQ();
                Thread.Sleep(100);
            }
            if (err < 0)
                Assert.Fail();
        }

        [TestMethod]
        public void TestIridiumHangup()
        {
            int err;
            Init();

            imodem.sendEscapeSequence();
            while ((err = imodem.waitForResponse(NAL_A3LAR.commands.ath0)) == 0)
            {
                _messageQueue.processQ();
                Thread.Sleep(100);
            }
            if (err < 0)
                Assert.Fail();
        }

        static bool runonce = false;
        private IridiumModem1 imodem;
        private MessageQueue _messageQueue;
        public void Init()
        {
            if (runonce) return;
            runonce = true;

            MotherBoard mb = MotherBoard.Instance;
            mb.EnableChannelPower(MotherBoard.ChannelNames.IridiumChannel);
            EngrLogger.writeToColumns("Initializing Iridium Modem");
            imodem = IridiumModem1.Instance;
            imodem.init();
            _messageQueue = MessageQueue.Instance;
            Thread.Sleep(5000);
            _messageQueue.processQMaxLimit();

        }
    }
}
