From 3e62661115cdc6549ad3cc130632fd95ff769844 Mon Sep 17 00:00:00 2001
From: bionic85 <144353436+bionic85@users.noreply.github.com>
Date: Mon, 6 Jan 2025 16:46:55 +0100
Subject: [PATCH] UserDataManager class is done. Implementation is still needed

---
 PuzzlePlayer/Resources/Themes.txt |   3 +
 PuzzlePlayer/UserDataManager.cs   | 116 ++++++++++++++----------------
 2 files changed, 58 insertions(+), 61 deletions(-)
 create mode 100644 PuzzlePlayer/Resources/Themes.txt

diff --git a/PuzzlePlayer/Resources/Themes.txt b/PuzzlePlayer/Resources/Themes.txt
new file mode 100644
index 0000000..296637a
--- /dev/null
+++ b/PuzzlePlayer/Resources/Themes.txt
@@ -0,0 +1,3 @@
+THEME|Dark|66|69|73|54|57|62|0|0|0|True
+THEME|Light|228|229|241|210|211|219|72|75|106|True
+THEME|Pink|255|209|220|252|178|197|251|199|255|True
\ No newline at end of file
diff --git a/PuzzlePlayer/UserDataManager.cs b/PuzzlePlayer/UserDataManager.cs
index 0e24aac..15c01a4 100644
--- a/PuzzlePlayer/UserDataManager.cs
+++ b/PuzzlePlayer/UserDataManager.cs
@@ -6,53 +6,19 @@ 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)
+    internal struct Theme(string s, Color col1, Color col2, Color col3, bool isUnlockedByDefault)
     {
         public const string prefix = "THEME";
         public string name = s;
         public Color primaryColor = col1;
         public Color secondaryColor = col2;
         public Color tertiaryColor = col3;
+        public bool unlocked = isUnlockedByDefault; // keeps track if this theme is unlocked
 
         public override string ToString()
         {
-            return $"{prefix}|{name}|{primaryColor.ToArgb()}|{secondaryColor.ToArgb()}|{tertiaryColor.ToArgb()}|";
+            return $"{prefix}|{name}|{primaryColor.R}|{primaryColor.G}|{primaryColor.B}|{secondaryColor.R}|{secondaryColor.G}|{secondaryColor.B}|{tertiaryColor.R}|{tertiaryColor.G}|{tertiaryColor.B}|{unlocked}";
         }
 
         public static Theme? FromString(string s)
@@ -62,11 +28,11 @@ namespace PuzzlePlayer_Namespace
                 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]));
+                Color c1 = Color.FromArgb(int.Parse(split[2]),int.Parse(split[3]),int.Parse(split[4]));
+                Color c2 = Color.FromArgb(int.Parse(split[5]),int.Parse(split[6]),int.Parse(split[7]));
+                Color c3 = Color.FromArgb(int.Parse(split[8]),int.Parse(split[9]),int.Parse(split[10]));
 
-                return new Theme(split[1], c1, c2, c3);
+                return new Theme(split[1], c1, c2, c3, bool.Parse(split[11]));
             }
             catch (Exception)
             {
@@ -82,37 +48,47 @@ namespace PuzzlePlayer_Namespace
         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 List<(Theme,bool)> unlockedThemes;
 
-        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)
+        public UserDataManager()
         {
-            if (newValue < 0)
-                return;
-
-            Settings.Default.Money = newValue;
-            Settings.Default.Save();
+            unlockedThemes = GetUnlockedThemes();
         }
 
-        private UserData GetUserSettings()
+        private List<(Theme,bool)> GetUnlockedThemes()
         {
-            string themeName = Settings.Default.ThemeName;
-            string fontName = Settings.Default.FontName;
-            int fontSize = Settings.Default.FontSize;
+            List<(Theme,bool)> result = new List<(Theme, bool)>();
+
+            if (!File.Exists(themeFilePath))
+                throw new Exception("blijf van de theme file af kkr djalla");
+
+            using(StreamReader sr = new StreamReader(themeFilePath))
+            {
+                while (!sr.EndOfStream)
+                {
+                    Theme? t = Theme.FromString(sr.ReadLine());
+                    if(t == null)
+                        throw new Exception("blijf van de theme file af kkr djalla, ga t zelf maar fixen");
+                    else
+                    {
+                        Theme theme = (Theme)t;
+                        result.Add((theme, theme.unlocked));
+                    }  
+                }
+            }
 
-            return new UserData(themeName, fontName, fontSize);
+            return result;
         }
 
-        private void SetUserData(UserData newUserData)
+        private void SetMoney(int newValue)
         {
-            Settings.Default.FontName = newUserData.fontName;
-            Settings.Default.FontSize = newUserData.fontSize;
-            Settings.Default.ThemeName = newUserData.currentThemeName;
+            if (newValue < 0)
+                return;
+
+            Settings.Default.Money = newValue;
             Settings.Default.Save();
         }
 
@@ -133,8 +109,26 @@ namespace PuzzlePlayer_Namespace
 
         private Theme GetCurrentTheme()
         {
-            
+            string currentThemeName = Settings.Default.ThemeName;
+            foreach ((Theme,bool) tb in unlockedThemes)
+            {
+                if (tb.Item2 && currentThemeName == tb.Item1.name)
+                    return tb.Item1;
+            }
+            throw new Exception("can't find the selected theme in unlocked themes");
         }
 
+        public void SetCurrentTheme(string newThemeName)
+        {
+            foreach ((Theme, bool) tb in unlockedThemes)
+            {
+                if(tb.Item2 && newThemeName == tb.Item1.name)
+                {
+                    Settings.Default.ThemeName = newThemeName;
+                    Settings.Default.Save();
+                    return;
+                }
+            }
+        }
     }
 }
-- 
GitLab