using System.Diagnostics;
using PdbFind.Gui.Properties;
using PdbFind.Lib;

namespace PdbFind.Gui
{
    public partial class FormPdbFind : Form
    {
        public FormPdbFind()
        {
            InitializeComponent();

            textBoxSysStore.Text = Settings.Default.SymStore;
        }

        private void buttonSymStoreBrowse_Click(object sender, EventArgs e)
        {
            using var fbd = new FolderBrowserDialog();
            fbd.InitialDirectory = textBoxSysStore.Text;
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                if (!string.IsNullOrWhiteSpace(fbd.SelectedPath) && Directory.Exists(fbd.SelectedPath))
                {
                    textBoxSysStore.Text = fbd.SelectedPath;
                }
            }
        }

        private void FormPdbFind_FormClosed(object sender, FormClosedEventArgs e)
        {
            Settings.Default.SymStore = textBoxSysStore.Text;
            Settings.Default.Save();
        }

        private void buttonPdbExplore_Click(object sender, EventArgs e)
        {
            var pdb = textBoxPdbFile.Text;
            if (!string.IsNullOrWhiteSpace(pdb) && File.Exists(pdb))
            {
                ShowSelectedInExplorer.FileOrFolder(pdb);
            }
        }

        private void buttonPeFileBrowse_Click(object sender, EventArgs e)
        {
            using var ofd = new OpenFileDialog();
            ofd.CheckFileExists = true;
            ofd.Multiselect = false;
            var pe = textBoxPeFile.Text;
            if (!string.IsNullOrWhiteSpace(pe) && File.Exists(pe))
            {
                ofd.InitialDirectory = Path.GetDirectoryName(pe);
                ofd.FileName = Path.GetFileName(pe);
            }

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (File.Exists(ofd.FileName))
                {
                    textBoxPeFile.Text = ofd.FileName;
                    LocatePdb();
                }
            }
        }

        private void LocatePdb()
        {
            var store = textBoxSysStore.Text;
            var pe = textBoxPeFile.Text;

            if (string.IsNullOrWhiteSpace(store) || !Directory.Exists(store))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(pe) || !File.Exists(pe))
            {
                return;
            }

            try
            {
                textBoxPdbFile.Text = Locator.LocatePdbInStore(pe, store);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void textBoxPeFile_TextChanged(object sender, EventArgs e)
        {
            textBoxPdbFile.Text = "";
        }

        private void buttonLocatePdb_Click(object sender, EventArgs e)
        {
            LocatePdb();
        }

        private void FormPdbFind_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data != null && e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        private void FormPdbFind_DragDrop(object sender, DragEventArgs e)
        {
            var files = (string[]?)e.Data?.GetData(DataFormats.FileDrop);
            if (files is { Length: > 0 })
            {
                textBoxPeFile.Text = files.First();
                LocatePdb();
            }
        }
    }
}