using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using System.Windows.Forms;
using static System.Console;
using static System.Threading.Thread;

namespace cce {
    internal class App {

        static internal bool Settings() {
            // Finish Settings screen, make it interactable
            bool saved = false;
            byte reset = 0;
            (int, int) since = (0,0); 
            while (true) {
                Title = Title + " >>> Settings";
                if (since.Item1 >= 4) {
                    saved = false;
                    since.Item1 = 0;
                }
                if (since.Item2 >= 4) {
                    reset = 0;
                    since.Item2 = 0;
                }
                Clear();
                AppSettings.Screen(saved, reset);
                ConsoleKeyInfo cki;
                do {
                    Write(">>> ");
                    cki = Console.ReadKey(); WriteLine();
                    if ((new char[] { '0', '1', 's' }).Contains(char.ToLower(cki.KeyChar))) break;
                    if (cki.Key == ConsoleKey.Backspace) { AppSettings.Save(); return true; }
                    WriteLine("Not a valid option");
                } while (true);

                switch (char.ToLower(cki.KeyChar)) {
                    case '0':
                        AppSettings.Reset(out reset);
                        break;
                    case '1':
                        AppSettings.Lgwrite = AppSettings.Lgwrite == 1 ? (byte)0 : (byte)1;
                        break;
                    case 's':
                        AppSettings.Save();
                        saved = true;
                        break;
                    default:
                        break;
                }
                since.Item1++;
                since.Item2++;
            }
        }
    }
    internal class AppSettings {
        static internal byte Lgwrite = 1;
        static internal string Common = "common.json";
        static internal string Extended = "extended.json";
        static internal string Settings = "settings.json";
        static internal (int, int) top_bottom = (0, 0);
        static internal void Check() {
            if (!File.Exists(Settings)) File.WriteAllText(Settings, JsonConvert.SerializeObject(AppSettings.ToDict()));
        }
        static internal Dictionary<string, string> ToDict () {
            //Add properties and finish this method
            return new Dictionary<string, string>() {
                { "LagWrite", Lgwrite.ToString()},
                { "Common", Common},
                { "Extended", Extended},
                { "Settings", Settings},
            };
        }
        static internal void Update() {
            Check();
            var settings = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(Settings));
            Lgwrite = byte.Parse(settings["LagWrite"]);
            Common = settings["Common"];
            Extended = settings["Extended"];
            Settings = settings["Settings"];
        }
        static internal void Save() {
            File.WriteAllText(Settings, JsonConvert.SerializeObject(AppSettings.ToDict()));
        }

        static internal void Screen(bool saved = false, byte reset = 0) {
            // │ ─ ┌ ┐ └ ┘
            if (saved) {
                BackgroundColor = ConsoleColor.Green;
                Write("Saved!");
                ResetColor();
            }
            if (reset == 2) {
                if (saved) Write(" │ ");
                BackgroundColor = ConsoleColor.Blue;
                Write("SETTINGS RESET!");
                ResetColor();
            } else if (reset == 1) {
                BackgroundColor = ConsoleColor.Red;
                Write("RESET ABORTED!");
                ResetColor();
            }
            WriteLine();
            top_bottom.Item1 = CursorTop;
            WriteLine(@"┌──────────────────────────────────┐
│                                  │
│                           ┌──┐   │
│ [1] Laggy Console Write:  │  │   │
│                           └──┘   │
│                                  │
│ [0] Use Default Settings         │
│                                  │
│ [S] Save Settings                │
│                                  │
└──────────────────────────────────┘

[S] Settings will save automatically upon return to menu

Press [Backspace] to return to main menu");
            top_bottom.Item2 = CursorTop;
            if (Lgwrite == 1) BackgroundColor = ConsoleColor.Green;
            else BackgroundColor = ConsoleColor.Red;
            SetCursorPosition(29, top_bottom.Item1 + 3);
            Write("  ");
            ResetColor();
            SetCursorPosition(0, top_bottom.Item2);
        }
        static internal void Reset(out byte reset) {
            reset = AreYouSure();
            if (reset == 1) return;
            Lgwrite = DefaultSettings.Lgwrite;
            Common = DefaultSettings.Common;
            Extended = DefaultSettings.Extended;
            Settings = DefaultSettings.Settings;
        }
        static private byte AreYouSure() {
            Title = "Are You Sure?";
            SetCursorPosition(0, top_bottom.Item1);
            WriteLine(@"
┌──────────────────────────────────┐
│                                  │
│           Are You Sure           │
│                                  │
│        Press Y to confirm        │
│                                  │
└──────────────────────────────────┘");
            SetCursorPosition(0, top_bottom.Item2);
            var cki = ReadKey();
            WriteLine();
            if (cki.Key == ConsoleKey.Y) return 2;
            else return 1;

        }
    }
    internal class DefaultSettings {
        static internal byte Lgwrite = 1;
        static internal string Common = "common.json";
        static internal string Extended = "extended.json";
        static internal string Settings = "settings.json";
    }
}
