<commit_before>﻿using EditorObjects.Parsing;
using FlatRedBall.Glue;
using FlatRedBall.Glue.Controls;
using FlatRedBall.Glue.Elements;
using FlatRedBall.Glue.Errors;
using FlatRedBall.Glue.FormHelpers;
using FlatRedBall.Glue.IO;
using FlatRedBall.Glue.Managers;
using FlatRedBall.Glue.Plugins;
using FlatRedBall.Glue.Plugins.EmbeddedPlugins;
using FlatRedBall.Glue.Plugins.ExportedImplementations;
using FlatRedBall.Glue.Plugins.ExportedImplementations.CommandInterfaces;
using FlatRedBall.Glue.SaveClasses;
using FlatRedBall.IO;
using Glue;
using GlueFormsCore.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace GlueFormsCore.Plugins.EmbeddedPlugins.MenuStripPlugin
{

    [Export(typeof(PluginBase))]
    public class MainMenuStripPlugin : EmbeddedPlugin
    {
        public MainMenuStripPlugin() : base()
        {
            DesiredOrder = DesiredOrder.Critical;
        }
        public override void StartUp()
        {
            var File = AddTopLevelMenuItem("File");
            {
                File.Add("New Project", GlueCommands.Self.ProjectCommands.CreateNewProject);
                File.Add("Load Project...", GlueCommands.Self.DialogCommands.ShowLoadProjectDialog);
                File.Add("Close Project", () => MainGlueWindow.CloseProject(true, false));
            }

            var Edit = AddTopLevelMenuItem("Edit");
            {
                Edit.Add("Find file references...", findFileReferencesToolStripMenuItem_Click);
            }

            var Project = AddTopLevelMenuItem("Project");
            {
                Project.Add("Error Check", RightClickHelper.ErrorCheckClick);
                Project.DropDownItems.Add(new ToolStripSeparator());
                Project.Add("Export Group...", exportToolStripMenuItem_Click);
                Project.Add("Import Group...", ElementImporter.AskAndImportGroup);
            }

            var Content = AddTopLevelMenuItem("Content");
            {
                var AdditionalContent = Content.Add("Additional Content", (Action)null) ;
                {
                    var ViewAdditionalContent = AdditionalContent.Add("View Additional Content Types", (Action)null);
                    {
                        ViewAdditionalContent.Add("For All Projects", forAllProjectsToolStripMenuItem_Click);
                        ViewAdditionalContent.Add("For This Project Only", forThisProjectOnlyToolStripMenuItem_Click);
                    }
                    AdditionalContent.Add("New Content CSV...", newContentCSVToolStripMenuItem_Click);
                    AdditionalContent.Add("View New File Template Folder", viewNewFileTemplateFolderToolStripMenuItem_Click);
                }
                Content.DropDownItems.Add(new ToolStripSeparator());

            }
            // cotinue here

            var Settings = AddTopLevelMenuItem("Settings");
            {
                Settings.Add(
                    "File Associations", 
                    () => new FileAssociationWindow().ShowDialog(MainGlueWindow.Self));

                Settings.Add(
                    "File Build Tools",
                    () => new FileBuildToolAssociationWindow(BuildToolAssociationManager.Self.ProjectSpecificBuildTools.BuildToolList).Show(MainGlueWindow.Self));

                Settings.Add(
                    "Performance Settings",
                    () => new PerformanceSettingsWindow().ShowDialog(MainGlueWindow.Self));

                Settings.Add(
                    "Preferences",
                    () => new PreferencesWindow().Show());

                Settings.DropDownItems.Add(new ToolStripSeparator());

                Settings.Add(
                    "Custom Game Class",
                    customGameClassToolStripMenuItem_Click);
            }


            var Update = AddTopLevelMenuItem("Update");

            var Plugins = AddTopLevelMenuItem("Plugins");
            {
                Plugins.Add("Install Plugin", () => new InstallPluginWindow().Show(MainGlueWindow.Self));
                Plugins.Add("Uninstall Plugin", () => new UninstallPluginWindow().Show(MainGlueWindow.Self));
                Plugins.Add("Create Plugin", () => new CreatePluginWindow().Show(MainGlueWindow.Self));
                Plugins.DropDownItems.Add(new ToolStripSeparator());
            }

            // No one uses experimental, so get rid of it...
            //var Experimental = AddTopLevelMenuItem("Experimental");

            var Help = AddTopLevelMenuItem("Help");
            {
                Help.Add("Tutorials", () => Process.Start("http://flatredball.com/documentation/tutorials/"));
                Help.Add("Report a Bug", () => Process.Start("https://github.com/vchelaru/flatredball/issues"));
            }
        }


        private void findFileReferencesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextInputWindow tiw = new TextInputWindow();

            tiw.DisplayText = "Enter the file name with extension, but no path (for example \"myfile.png\")";



            if (tiw.ShowDialog(MainGlueWindow.Self) == DialogResult.OK)
            {
                List<ReferencedFileSave> matchingReferencedFileSaves = new List<ReferencedFileSave>();
                List<string> matchingRegularFiles = new List<string>();

                string result = tiw.Result.ToLower();

                List<ReferencedFileSave> allReferencedFiles = ObjectFinder.Self.GetAllReferencedFiles();

                foreach (ReferencedFileSave rfs in allReferencedFiles)
                {
                    if (FileManager.RemovePath(rfs.Name.ToLower()) == result)
                    {
                        matchingReferencedFileSaves.Add(rfs);
                    }

                    string absoluteFileName = ProjectManager.MakeAbsolute(rfs.Name);

                    if (File.Exists(absoluteFileName))
                    {
                        List<FilePath> referencedFiles = null;

                        try
                        {
                            referencedFiles = FileReferenceManager.Self.GetFilesReferencedBy(absoluteFileName, TopLevelOrRecursive.Recursive);
                        }<commit_msg>added error handling when trying to open a browser.<commit_after>﻿using EditorObjects.Parsing;
using FlatRedBall.Glue;
using FlatRedBall.Glue.Controls;
using FlatRedBall.Glue.Elements;
using FlatRedBall.Glue.Errors;
using FlatRedBall.Glue.FormHelpers;
using FlatRedBall.Glue.IO;
using FlatRedBall.Glue.Managers;
using FlatRedBall.Glue.Plugins;
using FlatRedBall.Glue.Plugins.EmbeddedPlugins;
using FlatRedBall.Glue.Plugins.ExportedImplementations;
using FlatRedBall.Glue.Plugins.ExportedImplementations.CommandInterfaces;
using FlatRedBall.Glue.SaveClasses;
using FlatRedBall.IO;
using Glue;
using GlueFormsCore.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace GlueFormsCore.Plugins.EmbeddedPlugins.MenuStripPlugin
{

    [Export(typeof(PluginBase))]
    public class MainMenuStripPlugin : EmbeddedPlugin
    {
        public MainMenuStripPlugin() : base()
        {
            DesiredOrder = DesiredOrder.Critical;
        }
        public override void StartUp()
        {
            var File = AddTopLevelMenuItem("File");
            {
                File.Add("New Project", GlueCommands.Self.ProjectCommands.CreateNewProject);
                File.Add("Load Project...", GlueCommands.Self.DialogCommands.ShowLoadProjectDialog);
                File.Add("Close Project", () => MainGlueWindow.CloseProject(true, false));
            }

            var Edit = AddTopLevelMenuItem("Edit");
            {
                Edit.Add("Find file references...", findFileReferencesToolStripMenuItem_Click);
            }

            var Project = AddTopLevelMenuItem("Project");
            {
                Project.Add("Error Check", RightClickHelper.ErrorCheckClick);
                Project.DropDownItems.Add(new ToolStripSeparator());
                Project.Add("Export Group...", exportToolStripMenuItem_Click);
                Project.Add("Import Group...", ElementImporter.AskAndImportGroup);
            }

            var Content = AddTopLevelMenuItem("Content");
            {
                var AdditionalContent = Content.Add("Additional Content", (Action)null) ;
                {
                    var ViewAdditionalContent = AdditionalContent.Add("View Additional Content Types", (Action)null);
                    {
                        ViewAdditionalContent.Add("For All Projects", forAllProjectsToolStripMenuItem_Click);
                        ViewAdditionalContent.Add("For This Project Only", forThisProjectOnlyToolStripMenuItem_Click);
                    }
                    AdditionalContent.Add("New Content CSV...", newContentCSVToolStripMenuItem_Click);
                    AdditionalContent.Add("View New File Template Folder", viewNewFileTemplateFolderToolStripMenuItem_Click);
                }
                Content.DropDownItems.Add(new ToolStripSeparator());

            }
            // cotinue here

            var Settings = AddTopLevelMenuItem("Settings");
            {
                Settings.Add(
                    "File Associations", 
                    () => new FileAssociationWindow().ShowDialog(MainGlueWindow.Self));

                Settings.Add(
                    "File Build Tools",
                    () => new FileBuildToolAssociationWindow(BuildToolAssociationManager.Self.ProjectSpecificBuildTools.BuildToolList).Show(MainGlueWindow.Self));

                Settings.Add(
                    "Performance Settings",
                    () => new PerformanceSettingsWindow().ShowDialog(MainGlueWindow.Self));

                Settings.Add(
                    "Preferences",
                    () => new PreferencesWindow().Show());

                Settings.DropDownItems.Add(new ToolStripSeparator());

                Settings.Add(
                    "Custom Game Class",
                    customGameClassToolStripMenuItem_Click);
            }


            var Update = AddTopLevelMenuItem("Update");

            var Plugins = AddTopLevelMenuItem("Plugins");
            {
                Plugins.Add("Install Plugin", () => new InstallPluginWindow().Show(MainGlueWindow.Self));
                Plugins.Add("Uninstall Plugin", () => new UninstallPluginWindow().Show(MainGlueWindow.Self));
                Plugins.Add("Create Plugin", () => new CreatePluginWindow().Show(MainGlueWindow.Self));
                Plugins.DropDownItems.Add(new ToolStripSeparator());
            }

            // No one uses experimental, so get rid of it...
            //var Experimental = AddTopLevelMenuItem("Experimental");

            var Help = AddTopLevelMenuItem("Help");
            {
                Help.Add("Tutorials", () => OpenInBrowser("http://flatredball.com/documentation/tutorials/"));
                Help.Add("Report a Bug", () => OpenInBrowser("https://github.com/vchelaru/flatredball/issues"));
            }
        }


        private void OpenInBrowser(string url)
        {
            try
            {
                Process.Start(url);
            }
            catch
            {
                var message = 
                    $"Could not open a browser to the URL:\n\n{url}\n\nTry entering the address in a browser manually.";

                GlueCommands.Self.DialogCommands.ShowMessageBox(message);
            }
        }

        private void findFileReferencesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextInputWindow tiw = new TextInputWindow();

            tiw.Message = "Enter the file name with extension, but no path (for example \"myfile.png\")";



            if (tiw.ShowDialog(MainGlueWindow.Self) == DialogResult.OK)
            {
                List<ReferencedFileSave> matchingReferencedFileSaves = new List<ReferencedFileSave>();
                List<string> matchingRegularFiles = new List<string>();

                string result = tiw.Result.ToLower();

                List<ReferencedFileSave> allReferencedFiles = ObjectFinder.Self.GetAllReferencedFiles();

                foreach (ReferencedFileSave rfs in allReferencedFiles)
                {
                    if (FileManager.RemovePath(rfs.Name.ToLower()) == result)
                    {
                        matchingReferencedFileSaves.Add(rfs);
                    }

                    string absoluteFileName = ProjectManager.MakeAbsolute(rfs.Name);

                    if (File.Exists(absoluteFileName))
                    {
                        List<FilePath> referencedFiles = null;

                        try
                        {
                            referencedFiles = FileReferenceManager.Self.GetFilesReferencedBy(absoluteFileName, TopLevelOrRecursive.Recursive);
                        }
{'fraction_non_alphanumeric': 0.09534688904120021, 'fraction_numerical': 0.0, 'mean_word_length': 2.0216290842153706, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 35, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6990294', 'n_tokens_mistral': 3351, 'n_tokens_neox': 3100, 'n_words': 565}