using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace Valg.EVA.Skanning.Scan.ReadSoftEmulation.ScannerAccess
{
    class EmulatorAgent : ScannerAgent
    {
        private ScannerConfig _scannerConfig;

        public EmulatorAgent(ScannerConfig scannerConfig, int DPI, int Contrast)
        {
            this._scannerConfig = scannerConfig;
            
        }

        public override void StartScanning()
        {
            Aborted = false;
            var folderName = GetEmulatorImageFolderName();
            if (!string.IsNullOrWhiteSpace(folderName))
            {
                var extensions = new string[] { ".tiff", ".tif", ".png", ".bmp", ".gif" };
                var files = System.IO.Directory.GetFiles(folderName, "*.*").Where(f => extensions.Any(f.EndsWith)).ToArray();
                var toMove = new List<string>();
                for (var i = 0; i < files.Length - 1; i += 2)
                {
                    var frontImageBytes = File.ReadAllBytes(files[i]);
                    var backImageBytes = File.ReadAllBytes(files[i + 1]);
                    if (((Emulator)_scannerConfig.ScannerDefinition).MoveAndDeleteImages)
                    {
                        toMove.Add(files[i]);
                        toMove.Add(files[i + 1]);
                    }

                    OnDocumentScanned(frontImageBytes, backImageBytes, true);
                    if (Aborted)
                    {
                        break;
                    }
                }
                foreach (var f in toMove)
                {
                    var scannedDir = Path.Combine(folderName, "scanned");
                    try
                    {
                        Directory.CreateDirectory(scannedDir);
                    }
                    catch (IOException)
                    {

                    }
                    File.Move(f, Path.Combine(scannedDir, Path.GetFileName(f)));
                }
            }
            OnFeederEmpty();
        }

        public override void AbortScanning()
        {
            Aborted = true;
        }


        private string GetEmulatorImageFolderName()
        {
            var emulator = (Emulator)_scannerConfig.ScannerDefinition;
            if (!string.IsNullOrWhiteSpace(emulator.EmulationDirectory))
            {
                return emulator.EmulationDirectory;
            }
            var selectedPath = "";
            var t = new Thread((ThreadStart)(() =>
            {
                var fbd = new FolderBrowserDialog
                {
                    RootFolder = System.Environment.SpecialFolder.MyComputer,
                    ShowNewFolderButton = true
                };
                if (fbd.ShowDialog(new System.Windows.Forms.Form() { TopMost = true }) == DialogResult.Cancel)
                {
                    return;
                }

                selectedPath = fbd.SelectedPath;
            }));

            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            t.Join();

            return selectedPath;

        }
    }
}

