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 Utils {
        static internal Random rng = new Random();
        static internal void VarSleep(int adj, int low = 0, int high = 300, int step = 10,
            decimal chance = 0.25m) {
            var del = rng.Next(Math.Abs(adj));
            if (del <= (int)(adj * chance)) del = low;
            else if (del >= adj - (int)(adj * chance)) del = high;
            else del = del * step;
            Sleep(del);
        }
        static internal void Clip(string clip) {
            WriteLine();
            LagWrite("Do you want to copy the result to the Clipboard? [Y/n]: ");
            var cki = ReadKey().KeyChar;
            WriteLine();
            if (Char.ToLower(cki) == 'y') { Clipboard.SetText(clip); WriteLine("Writing to Clipboard: Done!"); } else WriteLine("Writing to Clipboard: Canceled by User");
        }
        internal static void LagWrite(string input, bool newline = false) {
            if (AppSettings.Lgwrite == 0) {
                if (newline) WriteLine(input);
                else Write(input);
                return;
            }
            var phrases = new List<List<string>>();
            var lines = input.Split(new char[] { '\n' });
            for (int i = 0; i < lines.Length; i++) {
                var line = new List<string>();
                var split = lines[i].Split(' ');
                int min = 4;
                int selector = rng.Next(min, split.Length > min + 1 ? split.Length : min + 1);
                var selected = new List<string>();
                for (int j = 0; j < split.Length; j++) {
                    selected.Add(split[j]);
                    if (j != 0 && j % selector == 0) {
                        line.Add(String.Join(" ", selected));
                        selected.Clear();
                    }
                }
                if (selected.Count > 0) line.Add(String.Join(" ", selected));
                phrases.Add(line);
            }
            for (int i = 0; i < phrases.Count; i++) {
                for (int j = 0; j < phrases[i].Count; j++) {
                    Write(j != phrases[i].Count - 1 ? phrases[i][j] + " " : phrases[i][j]);
                    VarSleep(22);
                }
                if (newline || i < phrases.Count - 1) WriteLine();
            }
        }
    }
}
