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

namespace ScanAssistant
{
    public partial class FormOpenSourceDir : Form
    {
        public string sourceDir { get; set; }
        public string destinationDir { get; set; }
        public List<string> fileNames { get; set; }
        public bool previousImage = false;

        public FormOpenSourceDir()
        {
            InitializeComponent();
            buttonOK.Enabled = false;
        }

        private void buttonBrowseSource_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();

            if (!string.IsNullOrEmpty(textSourceDir.Text))
            {
                folder.SelectedPath = textSourceDir.Text;
            }

            if (DialogResult.OK == folder.ShowDialog())
            {
                textSourceDir.Clear();

                textSourceDir.Text = sourceDir = folder.SelectedPath;
                buttonSetDefault.Enabled = true;

                string[] files = Directory.GetFiles(folder.SelectedPath);
                    //.Where(file => file.ToLower().EndsWith("jpg") || file.ToLower().EndsWith("png") || file.ToLower().EndsWith("tif") || file.ToLower().EndsWith("tiff"));


                if (files.Length > 1)
                {
                    foreach (string file in files)
                    {
                        listBoxSourceDir.Items.Add(Path.GetFileName(file));
                    }
                    labelSourceFileCount.Text = files.Count().ToString();
                }
            }
        }

        private void buttonBrowseDestination_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();

            if (!string.IsNullOrEmpty(textDestinationDir.Text))
            {
                folder.SelectedPath = textSourceDir.Text;
            }

            if (DialogResult.OK == folder.ShowDialog())
            {
                textDestinationDir.Clear();
                textDestinationDir.Text = destinationDir = folder.SelectedPath;
                if (checkDestinationDirExists())
                {
                    buttonOK.Enabled = true;
                }
                else
                {
                    buttonOK.Enabled = false;
                }
            }
        }

        private void buttonSetDefault_Click(object sender, EventArgs e)
        {
            textDestinationDir.Clear();
            DirectoryInfo parentDir = Directory.GetParent(textSourceDir.Text);
            textDestinationDir.Text = parentDir + "\\work";
            if (checkDestinationDirExists())
            {
                buttonOK.Enabled = true;
            }
            else
            {
                buttonOK.Enabled = false;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            sourceDir = textSourceDir.Text;
            destinationDir = textDestinationDir.Text;
            fileNames = listBoxSourceDir.Items.OfType<string>().ToList();
            this.Close();
        }

        private void textSourceDir_KeyUp(object sender, KeyEventArgs e)
        {
            if (Directory.Exists(textSourceDir.Text))
            {
                buttonSetDefault.Enabled = true;
                listBoxSourceDir.Items.Clear();
                string[] files = Directory.GetFiles(textSourceDir.Text);

                if (files.Length > 1)
                {
                    foreach (string file in files)
                    {
                        listBoxSourceDir.Items.Add(Path.GetFileName(file));
                    }
                    labelSourceFileCount.Text = listBoxSourceDir.Items.Count.ToString();
                }
                sourceDir = textSourceDir.Text;
            }
        }

        public bool checkDestinationDirExists()
        {
            if (!Directory.Exists(textDestinationDir.Text))
            {
                MessageBox.Show("Destination directory is not set," + Environment.NewLine + "or is not created!" , "Directory unknown", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            destinationDir = textDestinationDir.Text;
            return true;
        }

        private void listBoxSourceDir_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(listBoxSourceDir.SelectedIndex != -1)
            {
                if (previousImage)
                {
                    pictureBox1.InitialImage = null;
                }
                Application.UseWaitCursor = true;
                pictureBox1.ImageLocation = textSourceDir.Text + "\\" + listBoxSourceDir.SelectedItem.ToString();
                previousImage = true;
                Application.UseWaitCursor = false;
            }
        }

        private void textDestinationDir_KeyUp(object sender, KeyEventArgs e)
        {
            if(checkDestinationDirExists())
            {
                buttonOK.Enabled = true;
            }
            else
            {
                buttonOK.Enabled = false;
            }
        }
    }
}
