<commit_before>﻿/*
Copyright (c) 2014, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: 

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies, 
either expressed or implied, of the FreeBSD Project.
*/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Diagnostics;
using System.Text;
using System.IO;

using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.VectorMath;
using MatterHackers.MatterControl.ContactForm;
using MatterHackers.MatterControl.DataStorage;
using MatterHackers.Localizations;

namespace MatterHackers.MatterControl.SlicerConfiguration
{
    public class SettingsLayer
    {
        //Container class representing a collection of setting along with the meta info for that collection
        public Dictionary<string, DataStorage.SliceSetting> settingsDictionary;
        public DataStorage.SliceSettingsCollection settingsCollectionData;

        public SettingsLayer(DataStorage.SliceSettingsCollection settingsCollection, Dictionary<string, DataStorage.SliceSetting> settingsDictionary)
        {
            this.settingsCollectionData = settingsCollection;
            this.settingsDictionary = settingsDictionary;
        }
    }
    
    public class ActiveSliceSettings
    {
        static ActiveSliceSettings globalInstance = null;
        static string configFileExtension = "ini";
        private List<SettingsLayer> activeSettingsLayers;
        public RootedObjectEventHandler CommitStatusChanged = new RootedObjectEventHandler();
        public RootedObjectEventHandler SettingsChanged = new RootedObjectEventHandler();
        private int settingsHashCode;

        bool hasUncommittedChanges = false;

        public bool HasUncommittedChanges
        {
            get
            {
                return hasUncommittedChanges; 
            }
            set
            {
                if (this.hasUncommittedChanges != value)
                {
                    this.hasUncommittedChanges = value;
                    OnCommitStatusChanged();
                }
            }
        }

        void OnCommitStatusChanged()
        {
            CommitStatusChanged.CallEvents(this, null);
        }

        void OnSettingsChanged()
        {
            //Set hash code back to 0
            this.settingsHashCode = 0;

            SettingsChanged.CallEvents(this, null);
        }

        // private so that it can only be gotten through the Instance
        ActiveSliceSettings()
        {
        }

        public static ActiveSliceSettings Instance
        {
            get
            {
                if (globalInstance == null)
                {
                    globalInstance = new ActiveSliceSettings();
                    globalInstance.LoadAllSettings();
                }

                return globalInstance;
            }
        }



        /// <summary>
        /// This returns one of the three positions that should be probed when leveling
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Vector2 GetPrintLevelPositionToSample(int index)
        {
            Vector2 bedSize = ActiveSliceSettings.Instance.BedSize;
            Vector2 printCenter = ActiveSliceSettings.Instance.PrintCenter;

            switch (BedShape)
            {
                case MeshVisualizer.MeshViewerWidget.BedShape.Circular:
                    Vector2 firstPosition = new Vector2(printCenter.x, printCenter.y + (bedSize.y / 2) * .5);
                    switch (index)
                    {
                        case 0:
                            return firstPosition;
                        case 1:
                            return Vector2.Rotate(firstPosition, MathHelper.Tau / 3);
                        case 2:
                            return Vector2.Rotate(firstPosition, MathHelper.Tau * 2 / 3);
                        default:
                            throw new IndexOutOfRangeException();
                    }

                case MeshVisualizer.MeshViewerWidget.BedShape.Rectangular:
                default:
                    switch (index)
                    {
                        case 0:
                            return new Vector2(printCenter.x, printCenter.y + (bedSize.y / 2) * .8);
                        case 1:
                            return new Vector2(printCenter.x - (bedSize.x / 2) * .8, printCenter.y - (bedSize.y / 2) * .8);
                        case 2:
                            return new Vector2(printCenter.x + (bedSize.x / 2) * .8, printCenter.y - (bedSize.y / 2) * .8);
                        default:
                            throw new IndexOutOfRangeException();
                    }
            }
        }

        public void LoadAllSettings()
        {
            this.activeSettingsLayers = new List<SettingsLayer>();
            globalInstance.LoadSettingsForPrinter();
            globalInstance.LoadSettingsForMaterial(1);
            globalInstance.LoadSettingsForQuality();

            if (ActivePrinterProfile.Instance.ActivePrinter != null)
            {
                PrintLeveling.Instance.SetPrintLevelingEquation(
                    ActivePrinterProfile.Instance.GetPrintLevelingMeasuredPosition(0),
                    ActivePrinterProfile.Instance.GetPrintLevelingMeasuredPosition(1),
                    ActivePrinterProfile.Instance.GetPrintLevelingMeasuredPosition(2),
                    ActiveSliceSettings.Instance.PrintCenter);
            }
            OnSettingsChanged();

            this.HasUncommittedChanges = false;
        }

        public void LoadSettingsForMaterial(int extruderIndex)
        {
            if (ActivePrinterProfile.Instance.ActivePrinter != null)
            {
                DataStorage.SliceSettingsCollection collection;
                if (ActivePrinterProfile.Instance.GetMaterialSetting(extruderIndex) != 0)
                {
                    int materialOneSettingsID = ActivePrinterProfile.Instance.GetMaterialSetting(extruderIndex);
                    collection = DataStorage.Datastore.Instance.dbSQLite.Table<DataStorage.SliceSettingsCollection>().Where(v => v.Id == materialOneSettingsID).Take(1).FirstOrDefault();
                    SettingsLayer printerSettingsLayer = LoadConfigurationSettingsFromDatastore(collection);
                    this.activeSettingsLayers.Add(printerSettingsLayer);
                }                
            }
        }

        public void LoadSettingsForQuality()
        {

        }

        public void LoadSettingsForPrinter()
        {
            //Load default settings from the .ini file as first layer
            LoadDefaultConfigrationSettings();

            //Load printer settings from database as second layer
            LoadPrinterConfigurationSettings();<commit_msg>Enable Quality slice presets.<commit_after>﻿/*
Copyright (c) 2014, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: 

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies, 
either expressed or implied, of the FreeBSD Project.
*/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Diagnostics;
using System.Text;
using System.IO;

using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.VectorMath;
using MatterHackers.MatterControl.ContactForm;
using MatterHackers.MatterControl.DataStorage;
using MatterHackers.Localizations;

namespace MatterHackers.MatterControl.SlicerConfiguration
{
    public class SettingsLayer
    {
        //Container class representing a collection of setting along with the meta info for that collection
        public Dictionary<string, DataStorage.SliceSetting> settingsDictionary;
        public DataStorage.SliceSettingsCollection settingsCollectionData;

        public SettingsLayer(DataStorage.SliceSettingsCollection settingsCollection, Dictionary<string, DataStorage.SliceSetting> settingsDictionary)
        {
            this.settingsCollectionData = settingsCollection;
            this.settingsDictionary = settingsDictionary;
        }
    }
    
    public class ActiveSliceSettings
    {
        static ActiveSliceSettings globalInstance = null;
        static string configFileExtension = "ini";
        private List<SettingsLayer> activeSettingsLayers;
        public RootedObjectEventHandler CommitStatusChanged = new RootedObjectEventHandler();
        public RootedObjectEventHandler SettingsChanged = new RootedObjectEventHandler();
        private int settingsHashCode;

        bool hasUncommittedChanges = false;

        public bool HasUncommittedChanges
        {
            get
            {
                return hasUncommittedChanges; 
            }
            set
            {
                if (this.hasUncommittedChanges != value)
                {
                    this.hasUncommittedChanges = value;
                    OnCommitStatusChanged();
                }
            }
        }

        void OnCommitStatusChanged()
        {
            CommitStatusChanged.CallEvents(this, null);
        }

        void OnSettingsChanged()
        {
            //Set hash code back to 0
            this.settingsHashCode = 0;

            SettingsChanged.CallEvents(this, null);
        }

        // private so that it can only be gotten through the Instance
        ActiveSliceSettings()
        {
        }

        public static ActiveSliceSettings Instance
        {
            get
            {
                if (globalInstance == null)
                {
                    globalInstance = new ActiveSliceSettings();
                    globalInstance.LoadAllSettings();
                }

                return globalInstance;
            }
        }



        /// <summary>
        /// This returns one of the three positions that should be probed when leveling
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Vector2 GetPrintLevelPositionToSample(int index)
        {
            Vector2 bedSize = ActiveSliceSettings.Instance.BedSize;
            Vector2 printCenter = ActiveSliceSettings.Instance.PrintCenter;

            switch (BedShape)
            {
                case MeshVisualizer.MeshViewerWidget.BedShape.Circular:
                    Vector2 firstPosition = new Vector2(printCenter.x, printCenter.y + (bedSize.y / 2) * .5);
                    switch (index)
                    {
                        case 0:
                            return firstPosition;
                        case 1:
                            return Vector2.Rotate(firstPosition, MathHelper.Tau / 3);
                        case 2:
                            return Vector2.Rotate(firstPosition, MathHelper.Tau * 2 / 3);
                        default:
                            throw new IndexOutOfRangeException();
                    }

                case MeshVisualizer.MeshViewerWidget.BedShape.Rectangular:
                default:
                    switch (index)
                    {
                        case 0:
                            return new Vector2(printCenter.x, printCenter.y + (bedSize.y / 2) * .8);
                        case 1:
                            return new Vector2(printCenter.x - (bedSize.x / 2) * .8, printCenter.y - (bedSize.y / 2) * .8);
                        case 2:
                            return new Vector2(printCenter.x + (bedSize.x / 2) * .8, printCenter.y - (bedSize.y / 2) * .8);
                        default:
                            throw new IndexOutOfRangeException();
                    }
            }
        }

        public void LoadAllSettings()
        {
            this.activeSettingsLayers = new List<SettingsLayer>();
            globalInstance.LoadSettingsForPrinter();
            globalInstance.LoadSettingsForMaterial(1);
            globalInstance.LoadSettingsForQuality();

            if (ActivePrinterProfile.Instance.ActivePrinter != null)
            {
                PrintLeveling.Instance.SetPrintLevelingEquation(
                    ActivePrinterProfile.Instance.GetPrintLevelingMeasuredPosition(0),
                    ActivePrinterProfile.Instance.GetPrintLevelingMeasuredPosition(1),
                    ActivePrinterProfile.Instance.GetPrintLevelingMeasuredPosition(2),
                    ActiveSliceSettings.Instance.PrintCenter);
            }
            OnSettingsChanged();

            this.HasUncommittedChanges = false;
        }

        public void LoadSettingsForMaterial(int extruderIndex)
        {
            if (ActivePrinterProfile.Instance.ActivePrinter != null)
            {
                DataStorage.SliceSettingsCollection collection;
                if (ActivePrinterProfile.Instance.GetMaterialSetting(extruderIndex) != 0)
                {
                    int materialOneSettingsID = ActivePrinterProfile.Instance.GetMaterialSetting(extruderIndex);
                    collection = DataStorage.Datastore.Instance.dbSQLite.Table<DataStorage.SliceSettingsCollection>().Where(v => v.Id == materialOneSettingsID).Take(1).FirstOrDefault();
                    SettingsLayer printerSettingsLayer = LoadConfigurationSettingsFromDatastore(collection);
                    this.activeSettingsLayers.Add(printerSettingsLayer);
                }                
            }
        }

        public void LoadSettingsForQuality()
        {
            if (ActivePrinterProfile.Instance.ActivePrinter != null)
            {
                DataStorage.SliceSettingsCollection collection;
                if (ActivePrinterProfile.Instance.ActiveQualitySettingsID != 0)
                {
                    int materialOneSettingsID = ActivePrinterProfile.Instance.ActiveQualitySettingsID;
                    collection = DataStorage.Datastore.Instance.dbSQLite.Table<DataStorage.SliceSettingsCollection>().Where(v => v.Id == materialOneSettingsID).Take(1).FirstOrDefault();
                    SettingsLayer printerSettingsLayer = LoadConfigurationSettingsFromDatastore(collection);
                    this.activeSettingsLayers.Add(printerSettingsLayer);
                }
            }
        }

        public void LoadSettingsForPrinter()
        {
            //Load default settings from the .ini file as first layer
            LoadDefaultConfigrationSettings();

            //Load printer settings from database as second layer
            LoadPrinterConfigurationSettings();
{'fraction_non_alphanumeric': 0.062058497285305624, 'fraction_numerical': 0.00537100823165392, 'mean_word_length': 1.9937084935337295, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 31, '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': '6975138', 'n_tokens_mistral': 4093, 'n_tokens_neox': 3773, 'n_words': 1111}