using System;
using System.IO;
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 UnitTestIridiumDDTransfer
    {

        /// <summary>
        /// Test zmodem send using the bluetooth serial port, but plug it into a linux tty login.
        /// if no login is provided, the attempt should fail gracefully. 
        /// </summary>
        [TestMethod] 
        public void TestIridiumDD_BidirectionalTransfer()
        {
            int resp = 0;
            Init();
            IridiumPosixSession session = new IridiumPosixSession(iModem1);
           
            session.Username = "rawa";
            session.Password = "New0rleans";
            
            //Connect
            byte[] lukesDeskNumber = Encoding.UTF8.GetBytes("+18317751654");
            resp = session.Connect(lukesDeskNumber);
            Assert.AreEqual(resp, 0);

            //Login
            resp = session.Login();
            Assert.AreEqual(resp, 0);
           
            //Delete File if its there already
            session.SendCommand("rm graph.pdf\r");

            //Send File
            resp = session.ZModemSendTransaction("\\SD\\graph.pdf");
            Assert.AreEqual(resp, 0);
            
            //Receive File
            
            //Delete Local File 
            try
            {
                if (File.Exists("\\SD\\manifest.txt"))
                    File.Delete("\\SD\\manifest.txt");
            }
            catch { }

            resp = session.ZModemReceiveTransaction("~/manifest.txt", "\\SD\\");
            Assert.AreEqual(resp, 0);

            //Logout
            //resp = session.Logout();
            //Assert.AreEqual(resp, 0);

            //Shut down the session.
            session.Disconnect();
            
            
        }

        static bool runonce = false;
        private IridiumModem1 iModem1;
        public void Init()
        {
            if (!runonce)
            {
                runonce = true;
                iModem1 = IridiumModem1.Instance;
                Debug.Print("Initializing Iridium Modem");
                iModem1.init();
            }
        }
    }
}
