using System.Threading;
using GHI.IO.Storage;
using Microsoft.SPOT.IO;

namespace HWModules
{
    public sealed class SDStorage 
    {
        public AutoResetEvent evt = new AutoResetEvent(false);

        private MotherBoard mb;

        private static SDStorage _instance;

        public static SDStorage Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new SDStorage();
                //Thread.Sleep(POLL_TIME);//Makes sure the thread has time to mount on the first call to instance
                return _instance;
            }
        }

        private SDStorage()
        {
            //RemovableMedia.Insert += new InsertEventHandler(Inserted);
            //RemovableMedia.Eject += new EjectEventHandler(Ejected);
            mb = MotherBoard.Instance;

            //Start AutoMounting/dismounting thread
            //new Thread(SDMountThread).Start();
            
            sdCard = new SDCard();
            sdCard.Mount();
        }

        private VolumeInfo sdVol;
        public void FlushBuffers()
        {
            if (sdVol == null) 
                sdVol= new VolumeInfo(@"\SD");
            sdVol.FlushAll();


        }

        private static SDCard sdCard;

//        private void Inserted(object sender, MediaEventArgs e)
//        {
//            //Debug.Print("Insert event fired; SD Storage mount is finished.");
//            if (e.Volume.IsFormatted)
//            {

//                //Debug.Print("Available folders:");
//                string[] strs = Directory.GetDirectories(e.Volume.RootDirectory);
//                for (int i = 0; i < strs.Length; i++)
//                    //Debug.Print(strs[i]);

//                //Debug.Print("Available files:");
//                strs = Directory.GetFiles(e.Volume.RootDirectory);
//                for (int i = 0; i < strs.Length; i++)
//                    Debug.Print(strs[i]);
//            }
//            else
//            {
//                //Debug.Print("Media is not formatted. Formatting...");
////                e.Volume.Format("FAT", 0);
////                sRootDirectory = e.Volume.RootDirectory;
//            }
//            evt.Set(); // proceed with other processing
//        }

        //private void Ejected(object sender, MediaEventArgs e)
        //{
        //    //Debug.Print("SD Card Ejected.");

        //}

        //private const int POLL_TIME = 1000;
        //private void SDMountThread()
        //{
        //    // TODO Major issue, we shouldn't be establishing the SD card object without doing anything with it automatically, remove from EngrLogger
        //    SDCard sdCard = EngrLogger.sdCard;

        //    bool sdExists;
        //    while (true)
        //    {
        //        try {
        //            sdExists = mb.SDDetected;

        //            //add some time for connecting
        //            if (sdExists) {
        //                Thread.Sleep(50);
        //                sdExists = mb.SDDetected;
        //            }

        //            if (sdExists && sdCard == null) {
        //                sdCard = new SDCard(SDCard.SDInterface.MCI);
        //                sdCard.Mount();
        //            }

        //            if (sdExists && sdCard !=null) {
        //                if (!sdCard.Mounted)
        //                {
        //                    sdCard.Mount();
        //                }
        //            }

        //            if (!sdExists && sdCard != null) {
        //                sdCard.Unmount();
        //                sdCard.Dispose();
        //                sdCard = null;
        //            }
        //        }
        //        catch {
        //            if (sdCard != null) {
        //                sdCard.Dispose();
        //                sdCard = null;
        //            }
        //        }
        //        Thread.Sleep(POLL_TIME);
        //    }

        //}
    }
}
