<commit_before>﻿using CodeOutputPlugin.Manager;
using Gum;
using Gum.DataTypes;
using Gum.DataTypes.Variables;
using Gum.Plugins;
using Gum.Plugins.BaseClasses;
using Gum.ToolStates;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ToolsUtilities;

namespace CodeOutputPlugin
{
    [Export(typeof(PluginBase))]
    public class MainPlugin : PluginBase
    {
        #region Fields/Properties

        public override string FriendlyName => "Code Output Plugin";

        public override Version Version => new Version(1, 0);

        Views.CodeWindow control;
        ViewModels.CodeWindowViewModel viewModel;
        Models.CodeOutputProjectSettings codeOutputProjectSettings;

        #endregion

        public override bool ShutDown(PluginShutDownReason shutDownReason)
        {
            return true;
        }

        public override void StartUp()
        {
            AssignEvents();

            var item = this.AddMenuItem("Plugins", "View Code");
            item.Click += HandleViewCodeClicked;


        }

        private void AssignEvents()
        {
            this.InstanceSelected += HandleInstanceSelected;
            this.ElementSelected += HandleElementSelected;
            this.VariableSet += HandleVariableSet;
            this.StateWindowTreeNodeSelected += HandleStateSelected;
            this.AddAndRemoveVariablesForType += HandleAddAndRemoveVariablesForType;
            this.ProjectLoad += HandleProjectLoaded;
        }

        private void HandleProjectLoaded(GumProjectSave project)
        {
            codeOutputProjectSettings = CodeOutputProjectSettingsManager.CreateOrLoadSettingsForProject();
        }

        private void HandleStateSelected(TreeNode obj)
        {
            if (control != null)
            {
                RefreshCodeDisplay();
            }
        }

        private void HandleInstanceSelected(ElementSave arg1, InstanceSave instance)
        {
            if(control != null)
            {
                LoadCodeSettingsFile();

                RefreshCodeDisplay();
            }
        }

        private void HandleElementSelected(ElementSave element)
        {
            if (control != null)
            {
                LoadCodeSettingsFile();

                RefreshCodeDisplay();
            }
        }

        private void LoadCodeSettingsFile()
        {
            var element = SelectedState.Self.SelectedElement;
            if(element != null)
            {
                control.CodeOutputElementSettings = CodeOutputElementSettingsManager.LoadOrCreateSettingsFor(element);
            }
        }

        private void HandleVariableSet(ElementSave element, InstanceSave instance, string arg3, object arg4)
        {
            if (control != null)
            {
                RefreshCodeDisplay();

                if (control.CodeOutputElementSettings == null)
                {
                    control.CodeOutputElementSettings = new Models.CodeOutputElementSettings();
                }

                var elementSettings = control.CodeOutputElementSettings;

                if(elementSettings.AutoGenerateOnChange)
                {
                    GenerateCodeForSelectedElement(showPopups:false);
                }<commit_msg>Code export plugin now reacts to more events when auto generate is checked.<commit_after>﻿using CodeOutputPlugin.Manager;
using Gum;
using Gum.DataTypes;
using Gum.DataTypes.Variables;
using Gum.Plugins;
using Gum.Plugins.BaseClasses;
using Gum.ToolStates;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ToolsUtilities;

namespace CodeOutputPlugin
{
    [Export(typeof(PluginBase))]
    public class MainPlugin : PluginBase
    {
        #region Fields/Properties

        public override string FriendlyName => "Code Output Plugin";

        public override Version Version => new Version(1, 0);

        Views.CodeWindow control;
        ViewModels.CodeWindowViewModel viewModel;
        Models.CodeOutputProjectSettings codeOutputProjectSettings;

        #endregion

        public override bool ShutDown(PluginShutDownReason shutDownReason)
        {
            return true;
        }

        public override void StartUp()
        {
            AssignEvents();

            var item = this.AddMenuItem("Plugins", "View Code");
            item.Click += HandleViewCodeClicked;


        }

        private void AssignEvents()
        {
            this.InstanceSelected += HandleInstanceSelected;
            this.InstanceDelete += HandleInstanceDeleted;
            this.InstanceAdd += HandleInstanceAdd;
            this.InstanceReordered += HandleInstanceReordered;

            this.ElementSelected += HandleElementSelected;

            this.VariableSet += HandleVariableSet;
            
            this.StateWindowTreeNodeSelected += HandleStateSelected;
            this.StateRename += HandleStateRename;
            
            this.AddAndRemoveVariablesForType += HandleAddAndRemoveVariablesForType;
            this.ProjectLoad += HandleProjectLoaded;
        }


        private void HandleProjectLoaded(GumProjectSave project)
        {
            codeOutputProjectSettings = CodeOutputProjectSettingsManager.CreateOrLoadSettingsForProject();
        }

        private void HandleStateSelected(TreeNode obj)
        {
            if (control != null)
            {
                RefreshCodeDisplay();
            }
        }

        private void HandleInstanceSelected(ElementSave arg1, InstanceSave instance)
        {
            if(control != null)
            {
                LoadCodeSettingsFile();

                RefreshCodeDisplay();
            }
        }

        private void HandleElementSelected(ElementSave element)
        {
            if (control != null)
            {
                LoadCodeSettingsFile();

                RefreshCodeDisplay();
            }
        }

        private void LoadCodeSettingsFile()
        {
            var element = SelectedState.Self.SelectedElement;
            if(element != null)
            {
                control.CodeOutputElementSettings = CodeOutputElementSettingsManager.LoadOrCreateSettingsFor(element);
            }
        }

        private void HandleVariableSet(ElementSave element, InstanceSave instance, string arg3, object arg4)
        {
            HandleRefreshAndExport();
        }

        private void HandleStateRename(StateSave arg1, string arg2)
        {
            HandleRefreshAndExport();
        }

        private void HandleInstanceDeleted(ElementSave arg1, InstanceSave arg2)
        {
            HandleRefreshAndExport();
        }

        private void HandleInstanceAdd(ElementSave arg1, InstanceSave arg2)
        {
            HandleRefreshAndExport();
        }

        private void HandleInstanceReordered(InstanceSave obj)
        {
            HandleRefreshAndExport();
        }


        private void HandleRefreshAndExport()
        {
            if (control != null)
            {
                RefreshCodeDisplay();

                if (control.CodeOutputElementSettings == null)
                {
                    control.CodeOutputElementSettings = new Models.CodeOutputElementSettings();
                }

                var elementSettings = control.CodeOutputElementSettings;

                if (elementSettings.AutoGenerateOnChange)
                {
                    GenerateCodeForSelectedElement(showPopups: false);
                }
{'fraction_non_alphanumeric': 0.06772908366533864, 'fraction_numerical': 0.0020562909651715717, 'mean_word_length': 1.994228549442093, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '6862231', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1738, 'n_words': 380}