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 {
    class Program {
        static internal string title = "Corpus Cypher Executable (CCE)";
        [STAThread]
        static void Main(string[] args) {
            byte choice = 255;
            AppSettings.Update();
            while (true) {
                Title = title;
                SetBufferSize(240, 60);
                SetWindowSize(120, 30);
                Clear();
                CursorVisible = false;
                Utils.LagWrite("Welcome to the " + title, true);
                //WriteLine("Welcome to the Corpus cypher executable.");
                WriteCrypto();
                choice = Chooser();
                CursorVisible = false;
                var entry = "";
                if ((new int[] {1,2}).Contains(choice)) {
                    Utils.LagWrite("Please enter the sentence: ");
                    CursorVisible = true;
                    //Write("Please enter the sentence: ");
                    entry = ReadLine();
                    CursorVisible = false;
                }
                bool skip = false;
                if (choice == 1) { Transcoder.Encode(entry); Utils.Clip(Transcoder.result); }
                else if (choice == 2) Transcoder.Decode(entry);
                else if (choice == 5) Transcoder.Teacher();
                else if (choice == 8) skip = App.Settings();
                else if (choice == 9) Transcoder.head_transcribe();
                else if (choice == 254) Help();
                else if (choice == 255) { Beep(4000, 200); return; }

                SetCursorPosition(0, WindowHeight - 2);
                if (!skip) {
                    Utils.LagWrite("\nPress enter to continue, press any other key to exit"); CursorVisible = false;
                    var cki = ReadKey();
                    if (cki.Key != ConsoleKey.Enter) break;
                }
            }
        }
        
        static byte Chooser() {
            while (true) {
                Utils.LagWrite("[1] Encode"
+"\n[2] Decode"
+"\n-------------------------"
+"\n[5] Teach new words"
+"\n-------------------------"
+"\n[8] Settings"
+"\n[9] DEV: Update JSON files using sorted files from python"
+"\n[?] Help"
+"\n[Backspace] Exit"
+"\n>>> ");
                /*Write(@"Options?
[1] Encode
[2] Decode
-------------------------
[5] Teach new words
-------------------------
[9] DEV: Update JSON files using sorted files from python
[0] Exit
>> ");*/
                CursorVisible = true;
                var choice = ReadKey();
                WriteLine();
                if (choice.Key == ConsoleKey.Backspace) return 255;
                switch (choice.KeyChar) {
                    case '1':
                    case '2':
                    case '5':
                    case '9':
                    case '8':
                    case '0':
                    case '?':
                    case '/':
                    case '-':
                    case ',':
                        if ((new char[] { '?','/','-',','}).Contains(choice.KeyChar)) return 254;
                        return byte.Parse(choice.KeyChar.ToString());
                    default:
                        WriteLine("Not a valid option");
                        break;
                }
            }
        }
        static void WriteCrypto()
        {
            var pos = (CursorLeft, CursorTop);
            var crypto = new string[] {"###########################",
                "#                         #",
                "#      Corpus Cypher      #",
                "#                         #",
                "###########################",
                "#        ENG = COR        #",
                "#  A = A   G = J   M = S  #",
                "#  B = T   H = K   N = T  #",
                "#  C = Y   I = I   O = O  #",
                "#  D = P   J = T   P = K  #",
                "#  E = E   K = K   Q = R  #",
                "#  F = T   L = P   R = T  #",
                "#                         #",
                "#      S = Y   W = J      #",
                "#      T = P   X = K      #",
                "#      U = U   Y = Y      #",
                "#      V = T   Z = B      #",
                "#                         #",
                "###########################"};
            for (int i = 0; i < crypto.Length; i++)
            {
                CursorTop = i + 2;
                CursorLeft = WindowWidth - crypto[i].Length - 2;
                foreach (char c in crypto[i]) {
                    Write(c);
                    if (AppSettings.Lgwrite == 1 && Utils.rng.NextDouble()>0.8 && c != ' ') Sleep(1);
                }
            }
            SetCursorPosition(pos.CursorLeft, pos.CursorTop);
        }
        static void Help() {
            Clear();
            var help = $@"Welcome to help for {title}
This executable was made to help you encrypt something into the Corpus Cypher 
and to give you idea of what the person on the other end was trying to say.
This executable isn't fool proof and some mistakes in the code might've slipped
through my fingers. Still I hope it is atleast somewhat useful.

Encoding: Encoded text takes on the capitalization of the entered text.
Decoding: Decoded text takes on the capitalization of the entered text.

Teaching: First you enter in the encrypted word and then you give it the translation. It will remember the word.";
            help = help.Replace("\r\n", "\n");
            Utils.LagWrite(help);
        }

    }
}
