using System;
using System.Threading;
using System.Collections.Generic;
using System.IO;

using MatterHackers.MatterControl.DataStorage;
using MatterHackers.MatterControl.PrintQueue;
using MatterHackers.MatterControl.SlicerConfiguration;
using MatterHackers.Agg.UI;
using MatterHackers.GCodeVisualizer;

namespace MatterHackers.MatterControl.PrintQueue
{
    public class ExportToSdCardProcess
    {
        List<PrintItem> allFilesToExport;
        List<string> savedGCodeFileNames;
        public event EventHandler UpdatePartStatus;
        public event EventHandler StartingNextPart;
        public event EventHandler DoneSaving;

        int itemCountBeingWorkedOn;
        public int ItemCountBeingWorkedOn
        {
            get
            {
                return itemCountBeingWorkedOn;
            }
        }

        public string ItemNameBeingWorkedOn
        {
            get
            {
                if (ItemCountBeingWorkedOn < allFilesToExport.Count)
                {
                    return allFilesToExport[ItemCountBeingWorkedOn].Name;
                }

                return "";
            }
        }

        public int CountOfParts
        {
            get
            {
                return allFilesToExport.Count;
            }
        }

        public ExportToSdCardProcess(List<DataStorage.PrintItem> list)
        {
            // TODO: Complete member initialization
            this.allFilesToExport = list;

            itemCountBeingWorkedOn = 0;
        }

        public void Start()
        {
            if (PrintQueueControl.Instance.Count > 0)
            {
                if (StartingNextPart != null)
                {
                    StartingNextPart(this, new StringEventArgs(ItemNameBeingWorkedOn));
                }

                savedGCodeFileNames = new List<string>();
                allFilesToExport = PrintQueueControl.Instance.CreateReadOnlyPartList();
                foreach (PrintItem part in allFilesToExport)
                {
                    PrintItemWrapper printItemWrapper = new PrintItemWrapper(part);
                    if (System.IO.Path.GetExtension(part.FileLocation).ToUpper() == ".STL")
                    {
                        SlicingQueue.Instance.QueuePartForSlicing(printItemWrapper);
                        printItemWrapper.Done += new EventHandler(sliceItem_Done);
                        printItemWrapper.SlicingOutputMessage += printItemWrapper_SlicingOutputMessage;
                    }
                    else if (System.IO.Path.GetExtension(part.FileLocation).ToUpper() == ".GCODE")
                    {
                        sliceItem_Done(printItemWrapper, null);
                    }
                }
            }
        }

        void printItemWrapper_SlicingOutputMessage(object sender, EventArgs e)
        {
            StringEventArgs message = (StringEventArgs)e;
            if (UpdatePartStatus != null)
            {
                UpdatePartStatus(this, message);
            }
        }

        void sliceItem_Done(object sender, EventArgs e)
        {
            PrintItemWrapper sliceItem = (PrintItemWrapper)sender;

            sliceItem.Done -= new EventHandler(sliceItem_Done);
            savedGCodeFileNames.Add(sliceItem.GCodePathAndFileName);

            itemCountBeingWorkedOn++;
            if (itemCountBeingWorkedOn < allFilesToExport.Count)
            {
                if (StartingNextPart != null)
                {
                    StartingNextPart(this, new StringEventArgs(ItemNameBeingWorkedOn));
                }
            }
            else
            {
                UpdatePartStatus(this, new StringEventArgs("Calculating Total cm3..."));

                if (savedGCodeFileNames.Count > 0)
                {
                    double total = 0;
                    foreach (string gcodeFileName in savedGCodeFileNames)
                    {
                        string[] lines = File.ReadAllLines(gcodeFileName);
                        string filamentAmountLine = lines[lines.Length - 1];
                        int startPos = filamentAmountLine.IndexOf("(");
                        int endPos = filamentAmountLine.IndexOf("cm3)");
                        string value = filamentAmountLine.Substring(startPos + 1, endPos - (startPos + 1));
                        double amountForThisFile;
                        if (double.TryParse(value, out amountForThisFile))
                        {
                            total += amountForThisFile;
                        }
                        else
                        {
                            GCodeFile gcodeFile = new GCodeFile(gcodeFileName);
                            total += gcodeFile.GetFilamentCubicMm(ActiveSliceSettings.Instance.FillamentDiameter) / 1000;
                        }
                    }

                    // now copy all the gcode to the path given
                    for (int i = 0; i < savedGCodeFileNames.Count; i++)
                    {
                        string savedGcodeFileName = savedGCodeFileNames[i];
                        string originalFileName = Path.GetFileName(allFilesToExport[i].Name);
                        string outputFileName = Path.ChangeExtension(originalFileName, ".gcode");
                        throw new NotImplementedException();
                        //string outputPathAndName = Path.Combine(exportPath, outputFileName);

                        if (ActivePrinterProfile.Instance.DoPrintLeveling)
                        {
                            GCodeFile unleveledGCode = new GCodeFile(savedGcodeFileName);
                            PrintLeveling.Instance.ApplyLeveling(unleveledGCode);
                            //unleveledGCode.Save(outputPathAndName);
                        }
                        else
                        {
                            //File.Copy(savedGcodeFileName, outputPathAndName, true);
                        }
                    }

                    if (DoneSaving != null)
                    {
                        DoneSaving(this, new StringEventArgs(string.Format("{0:0.0}", total)));
                    }
                }
            }
        }
    }
}

