using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace RMDEC
{
    public partial class projectSelector : Form
    {

        private List<MVProject> mvProjectList = new List<MVProject>();
        public projectSelector()
        {
            InitializeComponent();
        }

        private void onIndexingComplete()
        {
            selectButton.Enabled = true;
            this.Cursor = Cursors.Arrow;
        }

        private void tryAddProject(string projectFile)
        {
            string relativeName = projectFile.Remove(0,projectDir.Text.Length + 1);

            //Try MV
            try
            {
                MVProject proj = MVProject.ParseSystemJson(projectFile);
                mvProjectList.Add(proj);
                projectList.Items.Add("[RMMV] - "+ proj.GameTitle + " - " + relativeName);
            }
            finally { }
        }
        private void updateProjectList(string projectDir)
        {
            if (Directory.Exists(projectDir))
            {

                this.Cursor = Cursors.WaitCursor;

                Thread indexProjects = new Thread(() =>
                {
                    IEnumerable fileList = Directory.EnumerateFiles(projectDir, "*", SearchOption.AllDirectories);
                    foreach (string fileEntry in fileList)
                    {
                        if (Path.GetFileName(fileEntry) == "System.json")
                        {
                            Invoke((Action)delegate
                            {
                                tryAddProject(fileEntry);
                            });
                        }
                    }

                    Invoke((Action)delegate
                    {
                        onIndexingComplete();
                    });
                });

                indexProjects.Start();
            }

        }
        
        private void clearProjectList()
        {
            mvProjectList.Clear();
            projectList.Items.Clear();
            selectButton.Enabled = false;
        }

        private void projectDir_TextChanged(object sender, EventArgs e)
        {
            clearProjectList();
            string newDir = projectDir.Text;
            updateProjectList(newDir);    
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            CommonOpenFileDialog folderDialog = new CommonOpenFileDialog();
            folderDialog.IsFolderPicker = true;

            if (folderDialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                projectDir.Text = folderDialog.FileName;
            }
        }

        private void selectButton_Click(object sender, EventArgs e)
        {
            int index = projectList.SelectedIndex;
            if(index >= 0)
            {
                MVProjectToolset mvProjectToolset = new MVProjectToolset(mvProjectList[index]);
                mvProjectToolset.ShowDialog();
            }
        }
    }
}
