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
{
    /// <summary>
    /// DEC2016 Modified this test to function on a Moxa Terminal Server 
    /// with a TCP client configuration pointing to my development machine
    /// running the python multithreaded server.
    ///  
    /// </summary>
    [TestClass]
    public class UnitTestPosixFileTransfer
    {

        [TestMethod]
        public void TestPosixBadLogin()
        {
            InitBT();
            int resp = 0;

            PosixSession session = new PosixSession(bt.IOPort);
            session.Username = "cpfloat";
            session.Password = "float124";

            Debug.Print("Send the bad login.");
            resp = session.Login();
            Assert.AreEqual(resp, (int) PosixSession.ERROR.NOLOGIN);

            Debug.Print("Sending baduser");

            session.Username = "baduser"; 
            resp = session.Login();
            Assert.AreEqual(resp, (int) PosixSession.ERROR.NOLOGIN);

        }
        
        /// <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 TestPosixZmodemSend()
        {
            int resp = 0;
            InitBT();
            PosixSession session = new PosixSession(bt.IOPort);
            session.Username = "cpfloat";
            session.Password = "float123";
            //Login
            resp = session.Login();
            Assert.AreEqual(resp, 0);
           
            //Delete File if its there already
            // REMOVED RM not supported session.SendCommand("rm graph.pdf\r");

            //Send File
            resp = session.ZModemSendTransaction("\\SD\\graph.pdf");
            Assert.AreEqual(resp, 0);

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

        [TestMethod]
        public void TestPosixZmodemReceive()
        {
            InitBT();
            int resp = 0;

            //Delete Local File 
            try
            {
                if (File.Exists("\\SD\\manifest.txt"))
                    File.Delete("\\SD\\manifest.txt");
            }
            catch { }

            PosixSession session = new PosixSession(bt.IOPort);
            session.Username = "cpfloat";
            session.Password = "float123";

            //Login
            resp = session.Login();
            Assert.AreEqual(resp, 0);
            
            //Receive File
            resp = session.ZModemReceiveTransaction("/Users/ericjmartin/manifest.txt", "\\SD\\");
            Assert.AreEqual(resp, 0);

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


        private static BTConsole bt;
        private bool runonce = false;
        public void InitBT()
        {
            if (!runonce)
            {
                runonce = true;
                bt = BTConsole.Instance;
                bt.init();
                //TODO bt.detachEventHandler();
            }

        }
    }
}
