using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Machinations
{
    /// <summary>
    /// User settings of this program
    /// </summary>
    public class MachnationsSettings
    {
        private bool m_autoSave;
        private const string SavePath = "settings.txt";
        public const string LastGraph = "lastGraph.xml";
        

        public MachnationsSettings()
        {
            if (!File.Exists(SavePath))
            {
                m_autoSave = true;
                
                SaveSettings();
            }
            else
            {
                var settings = File.ReadAllLines(SavePath).ToList();
                settings.ForEach(line =>
                {
                    var words = line.Split('=');
                    switch (words[0])
                    {
                        case "autosave":
                            m_autoSave = bool.Parse(words[1]);
                            break;
                        case "componentPath":
                            MachinationsVariables.ComponentsDirectory = words[1];
                            break;
                        default: throw new NotImplementedException("unknown settings");
                    }
                });
            } 
        }

        public bool AutoSave
        {
            get { return m_autoSave; }
            set
            {
                m_autoSave = value;
                SaveSettings();
            }
        }

        public string ComponentPath
        {
            get { return MachinationsVariables.ComponentsDirectory; }
        }

        public void SaveComponentPath(string newPath)
        {
            string original = MachinationsVariables.ComponentsDirectory;
            MachinationsVariables.ComponentsDirectory = newPath;
            SaveSettings();
            MachinationsVariables.ComponentsDirectory = original;
        }


        private void SaveSettings()
        {
            List<string> lines = new List<string>
            {
                "autosave="+ m_autoSave,
                "componentPath=" + MachinationsVariables.ComponentsDirectory
            };
            File.WriteAllLines(SavePath, lines);
        }
    }
}