<commit_before>﻿using MacroManager.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MacroManager
{
    public class MacroService
    {

        #region Fields

        /// <summary>
        /// A repository of macros. Used to persist changes
        /// </summary>
        private IMacroRepository macroRepository;
        /// <summary>
        /// A service for managing the low level hooks. 
        /// </summary>
        private IHookService hookService;

        #endregion

        #region Constructors
        /// <summary>
        /// Constructor, two dependencies are injected. 
        /// </summary>
        public MacroService(IHookService hookService)
        {
            this.hookService = hookService;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Creates a new Macro. 
        /// </summary>
        public Macro CreateMacro(string name, string description = "")
        {
            return new Macro(name, description);
        }

        /// <summary>
        /// Starts recording a new Macro. 
        /// Fires the RecordingStarted event.
        /// </summary>
        public void StartRecording(Macro macro)
        {
            if (this.macroRepository == null)
            {
                throw new Exception("Can't start recording if the Macro repository has not been initialized. Run InitializeRepository.");
            }
            this.hookService.StartRecording(macro);
            this.OnRecordingStarted();
        }

        /// <summary>
        /// Stopes recording the supplied Macro and saves it into the repository. 
        /// Fires the RecordingStopped event.
        /// </summary>
        public void StopRecording(Macro macro)
        {
            if (this.macroRepository == null)
            {
                throw new Exception("Can't start recording if the Macro repository has not been initialized. Run InitializeRepository.");
            }
            this.hookService.StopRecording();
            this.macroRepository.Add(macro);
            this.OnRecordingStopped();
        }

        /// <summary>
        /// Replays the supplied macro. 
        /// </summary>
        public void ReplayMacro(Macro macro)
        {
            if (this.macroRepository == null)
            {
                throw new Exception("Can't start recording if the Macro repository has not been initialized. Run InitializeRepository.");
            }
            this.hookService.ReplayMacro(macro);
        }

        /// <summary>
        /// Returns all the macros from the Macro repository.
        /// </summary>
        public IEnumerable<Macro> GetAllMacros()
        {
            if (this.macroRepository == null)
            {
                throw new Exception("Can't start recording if the Macro repository has not been initialized. Run InitializeRepository.");
            }
            return this.macroRepository.Read();
        }

        /// <summary>
        /// Initializes the repository of the service. 
        /// </summary>
        public void IntitializeRepository(IMacroRepository macroRepository)
        {
            this.macroRepository = macroRepository;
        }

        /// <summary>
        /// Saves all the changes that have been made.
        /// </summary>
        public void SaveChanges()
        {
            if (this.macroRepository == null)
            {
                throw new Exception("Can't start recording if the Macro repository has not been initialized. Run InitializeRepository.");
            }
            this.macroRepository.SaveChanges();
        }

        public void SaveChanges(string fileName)
        {
            if (!(this.macroRepository is XmlMacroRepository))
            {
                throw new Exception("The save changes with a fileName parameter method only works if the repository is an XmlMacroRepository.");
            }
            var existingMacros = this.macroRepository.Read();
            this.macroRepository = new XmlMacroRepository(fileName);
            foreach (var macro in existingMacros)
            {
                this.macroRepository.Add(macro);
            }
            this.macroRepository.SaveChanges();
        }

        #endregion<commit_msg>Added error checking and comments<commit_after>﻿using MacroManager.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MacroManager
{
    public class MacroService
    {

        #region Fields

        /// <summary>
        /// A repository of macros. Used to persist changes
        /// </summary>
        private IMacroRepository macroRepository;
        /// <summary>
        /// A service for managing the low level hooks. 
        /// </summary>
        private IHookService hookService;

        #endregion

        #region Constructors
        /// <summary>
        /// Constructor, two dependencies are injected. 
        /// </summary>
        public MacroService(IHookService hookService)
        {
            this.hookService = hookService;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Creates a new Macro. 
        /// </summary>
        public Macro CreateMacro(string name, string description = "")
        {
            return new Macro(name, description);
        }

        /// <summary>
        /// Starts recording a new Macro. 
        /// Fires the RecordingStarted event.
        /// </summary>
        public void StartRecording(Macro macro)
        {
            if (this.macroRepository == null)
            {
                throw new Exception("Can't start recording if the Macro repository has not been initialized. Run InitializeRepository.");
            }
            this.hookService.StartRecording(macro);
            this.OnRecordingStarted();
        }

        /// <summary>
        /// Stopes recording the supplied Macro and saves it into the repository. 
        /// Fires the RecordingStopped event.
        /// </summary>
        public void StopRecording(Macro macro)
        {
            if (this.macroRepository == null)
            {
                throw new Exception("Can't start recording if the Macro repository has not been initialized. Run InitializeRepository.");
            }
            this.hookService.StopRecording();
            this.macroRepository.Add(macro);
            this.OnRecordingStopped();
        }

        /// <summary>
        /// Replays the supplied macro. 
        /// </summary>
        public void ReplayMacro(Macro macro)
        {
            if (this.macroRepository == null)
            {
                throw new Exception("Can't start recording if the Macro repository has not been initialized. Run InitializeRepository.");
            }
            this.hookService.ReplayMacro(macro);
        }

        /// <summary>
        /// Returns all the macros from the Macro repository.
        /// </summary>
        public IEnumerable<Macro> GetAllMacros()
        {
            if (this.macroRepository == null)
            {
                throw new Exception("Can't start recording if the Macro repository has not been initialized. Run InitializeRepository.");
            }
            return this.macroRepository.Read();
        }

        /// <summary>
        /// Initializes the repository of the service. 
        /// </summary>
        public void IntitializeRepository(IMacroRepository macroRepository)
        {
            this.macroRepository = macroRepository;
        }

        /// <summary>
        /// Saves all the changes that have been made.
        /// </summary>
        public void SaveChanges()
        {
            if (this.macroRepository == null)
            {
                throw new Exception("Can't start recording if the Macro repository has not been initialized. Run InitializeRepository.");
            }
            this.macroRepository.SaveChanges();
        }

        /// <summary>
        /// Saves changes to a specific file, only works when an XML-file is used as a persistence.
        /// </summary>
        public void SaveChanges(string fileName)
        {
            if (this.macroRepository == null)
            {
                throw new Exception("Can't start recording if the Macro repository has not been initialized. Run InitializeRepository.");
            }
            if (!(this.macroRepository is XmlMacroRepository))
            {
                throw new Exception("The save changes with a fileName parameter method only works if the repository is an XmlMacroRepository.");
            }
            var existingMacros = this.macroRepository.Read();
            this.macroRepository = new XmlMacroRepository(fileName);
            foreach (var macro in existingMacros)
            {
                this.macroRepository.Add(macro);
            }
            this.macroRepository.SaveChanges();
        }

        #endregion
{'fraction_non_alphanumeric': 0.08986509086854721, 'fraction_numerical': 0.0, 'mean_word_length': 1.6243417203042716, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, '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': '332118', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2039, 'n_words': 643}