Skip to content
Snippets Groups Projects
UserDataManager.cs 4.29 KiB
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using PuzzlePlayer_Namespace.Properties;    // for accessing user settings

namespace PuzzlePlayer_Namespace
{
    // struct for saving settings
    // saves the current selected theme and the font name and size
    internal struct UserData(string themeName, string fName, int fSize)
    {
        public const string prefix = "SETTINGS";
        public string currentThemeName = themeName;
        public string fontName = fName;
        public int fontSize = fSize;

        public override string ToString()
        {
            return $"{prefix}|{fontName}|{fontSize}|{currentThemeName}";
        }

        public static UserData? FromString(string s)
        {
            string[] split = s.Split('|');
            if (split[0] != prefix)
                return null;

            try
            {
                string fName = split[1];
                int fSize = int.Parse(split[2]);
                string tName = split[3];

                return new UserData(tName, fName, fSize);
            }
            catch (Exception)
            {
                return null;    //unreadable file will be handled somewhere else
            }
        }
    }

    // struct for saving themes
    internal struct Theme(string s, Color col1, Color col2, Color col3)
    {
        public const string prefix = "THEME";
        public string name = s;
        public Color primaryColor = col1;
        public Color secondaryColor = col2;
        public Color tertiaryColor = col3;

        public override string ToString()
        {
            return $"{prefix}|{name}|{primaryColor.ToArgb()}|{secondaryColor.ToArgb()}|{tertiaryColor.ToArgb()}|";
        }

        public static Theme? FromString(string s)
        {
            string[] split = s.Split('|');
            if (split[0] != prefix)
                return null;
            try
            {
                Color c1 = Color.FromArgb(int.Parse(split[2]));
                Color c2 = Color.FromArgb(int.Parse(split[3]));
                Color c3 = Color.FromArgb(int.Parse(split[4]));

                return new Theme(split[1], c1, c2, c3);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

    // Class for managing and storing user data
    internal class UserDataManager
    {
        // properties for getting various settings
        public int Money { get { return Settings.Default.Money; } set { SetMoney(value); } }
        public Font MainFont { get { return GetMainFont(); } }
        public Theme Theme { get { return GetCurrentTheme(); } }
        public UserData Data { get {  return GetUserSettings(); } set { SetUserData(value); } }
        
        private List<(Theme,bool)> unlockedThemes = new List<(Theme, bool)>();

        private const string dataFilePath = "Data.txt";
        private const string themeFilePath = "Themes.txt";
        private const string moneyPrefix = "DO_NOT_TOUCH_THE_MONEY_CHEATER";

        private void SetMoney(int newValue)
        {
            if (newValue < 0)
                return;

            Settings.Default.Money = newValue;
            Settings.Default.Save();
        }

        private UserData GetUserSettings()
        {
            string themeName = Settings.Default.ThemeName;
            string fontName = Settings.Default.FontName;
            int fontSize = Settings.Default.FontSize;

            return new UserData(themeName, fontName, fontSize);
        }

        private void SetUserData(UserData newUserData)
        {
            Settings.Default.FontName = newUserData.fontName;
            Settings.Default.FontSize = newUserData.fontSize;
            Settings.Default.ThemeName = newUserData.currentThemeName;
            Settings.Default.Save();
        }

        private Font GetMainFont()
        {
            string fontName = Settings.Default.FontName;
            int fontSize = Settings.Default.FontSize;

            return new Font(fontName, fontSize);
        }

        public void SetMainFont(string fontName, int fontSize)
        {
            Settings.Default.FontName = fontName;
            Settings.Default.FontSize = fontSize;
            Settings.Default.Save();
        }

        private Theme GetCurrentTheme()
        {
            
        }

    }
}