using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Net.NetworkInformation;

using GHI.Premium.IO;

namespace FTP
{
    public class Program
    {

        static WifiManager wifi;
        static FTPServer ftp;
        static PersistentStorage _sd;

        public static void Main()
        {
            Debug.Print("");
            Debug.Print("CPF FTP Server");
            Debug.Print("---------------------");

            // Mount SD card
            Debug.Print(">> Mounting SD...");
            _sd = new PersistentStorage("SD");
            _sd.MountFileSystem();
            Debug.Print(">> SD MOUNTED");

            // Start Ethernet =
            Debug.Print(">> Enabling WiFi...");

            wifi = new WifiManager();
            wifi.SSID = "MBARI WiFi";
            wifi.Password = "";
            wifi.Enable(WifiManager.InfrastructureType.AP, WifiManager.ConnectionType.STATIC);
       
            while (!wifi.Connected)
            {
                Thread.Sleep(1000);
                if (wifi.Connected)
                    break;
                Debug.Print(">> NO CONNECTION <<");
                Thread.Sleep(4000);
            }
            Debug.Print(">> CONNECTED: " + wifi.IPAddress);

            // Prepare & Start FTP Server
            Debug.Print(">> Starting FTP server...");
            ftp = new FTPServer(wifi.IPAddress);
            ftp.AddLogin("coastal", "profiler", "\\SD\\", new FTPServer.UserPermissions(true, true, true, true, true, true));
            ftp.AllowAnonymous = true;
            ftp.AnonymousRoot = "\\SD\\";
            ftp.DebugMode = true;
            ftp.Start();
            Debug.Print(">> FTP server STARTED");
            // Put main thread into infinite sleep
            Thread.Sleep(Timeout.Infinite);
        }

    }
}
