<gh_stars>0﻿using System;

namespace Run
{
    class Program
    {
        // Menu
        public static string OpenMenu(string Items, string title, string cUrsorIndex, string cursor, string CursorFg, string MenuFg, string MenuHFg, string MenuHighlight, string reset)
        {
            Console.Clear();
            string[] items = Items.Split(":::");
            int CursorIndex = Int32.Parse(cUrsorIndex);
            string CursorClear = new string(' ', cursor.Length);
            int ix = 0;
            string output = "";
            int max_index = items.Length;
            if (title.ToLower() != "false")
            {
                Console.SetCursorPosition(0, 0);
                Console.WriteLine(MenuFg + title);
                ix = 1;
                CursorIndex++;
                max_index++;
            }
            for (int i = 0; i < items.Length; i++)
            {
                Console.SetCursorPosition(cursor.Length, i + ix);
                Console.WriteLine(MenuFg + items[i]);
            }
            Console.SetCursorPosition(0, CursorIndex);
            Console.Write($"{CursorFg + cursor}");
            Console.SetCursorPosition(cursor.Length, CursorIndex);
            if (MenuHighlight.ToLower() == "false") { Console.Write(MenuHFg + items[CursorIndex - ix] + reset); }
            else { Console.Write(MenuHFg + MenuHighlight + items[CursorIndex - ix] + reset); }
            Console.SetCursorPosition(0, max_index);
            bool exited = false;
            while (!exited)
            {
                System.ConsoleKey key = Console.ReadKey(true).Key;
                if (key == ConsoleKey.DownArrow)
                {
                    Console.SetCursorPosition(0, CursorIndex);
                    Console.Write(CursorClear);
                    Console.SetCursorPosition(cursor.Length, CursorIndex);
                    Console.Write(reset + MenuFg + items[CursorIndex - ix]);
                    if (CursorIndex == max_index - 1)
                    {
                        Console.SetCursorPosition(cursor.Length, CursorIndex);
                        Console.Write(reset + MenuFg + items[CursorIndex - ix] + reset);
                        CursorIndex = ix;
                        Console.SetCursorPosition(0, CursorIndex);
                        Console.Write(CursorFg + cursor);
                    }
                    else
                    {
                        Console.SetCursorPosition(0, CursorIndex);
                        CursorIndex++;
                        Console.SetCursorPosition(0, CursorIndex);
                        Console.Write(CursorFg + cursor);
                    }
                    Console.SetCursorPosition(cursor.Length, CursorIndex);
                    if (MenuHighlight.ToLower() == "false") { Console.Write(MenuHFg + items[CursorIndex - ix] + reset); }
                    else { Console.Write(MenuHFg + MenuHighlight + items[CursorIndex - ix] + reset); }
                    Console.SetCursorPosition(0, max_index);
                }
                else if (key == ConsoleKey.UpArrow)
                {
                    Console.SetCursorPosition(0, CursorIndex);
                    Console.Write(CursorClear);
                    Console.SetCursorPosition(cursor.Length, CursorIndex);
                    Console.Write(reset + MenuFg + items[CursorIndex - ix] + reset);
                    if (CursorIndex == ix)
                    {
                        CursorIndex = max_index - 1;
                        Console.SetCursorPosition(0, CursorIndex);
                        Console.Write(CursorFg + cursor + reset);
                    }
                    else
                    {
                        CursorIndex = CursorIndex - 1;
                        Console.SetCursorPosition(0, CursorIndex);
                        Console.Write(CursorFg + cursor + reset);
                    }
                    Console.SetCursorPosition(cursor.Length, CursorIndex);
                    if (MenuHighlight.ToLower() == "false") { Console.Write(MenuHFg + items[CursorIndex - ix] + reset); }
                    else { Console.Write(MenuHFg + MenuHighlight + items[CursorIndex - ix] + reset); }
                    Console.SetCursorPosition(0, max_index);
                }
                else if (key == ConsoleKey.Escape)
                {
                    output = "None";
                    exited = true;
                }
                else if (key == ConsoleKey.Enter)
                {
                    {
                        output = (CursorIndex - ix).ToString();
                    }
                    exited = true;
                }
                else
                {
                    output = "key." + key.ToString().ToLower() + ":::" + (CursorIndex - ix).ToString();
                    exited = true;
                }
            }
            return output;
        }

        // Get user input
        public static string PromptInput(string prompt)
        {
            Console.Write(prompt);
            string text = Console.ReadLine();
            if (text == null)
            {
                return "";
            }
            return text.ToString();
        }

        // Write to file
        public static void WriteFile(string path, string Obj)
        {
            string FullPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)+"/"+path;
            using (StreamWriter writer = new StreamWriter(FullPath))
            {
                writer.WriteLine(Obj);
            }
        }

        // Main
        static void Main(string[] args)
        {
            if (args.Length>0){
                if (args[0] == "menu"){
                    WriteFile("output.txt", OpenMenu(args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]));
                }
                else if (args[0] == "prompt"){
                    WriteFile("output.txt", PromptInput(args[1]));
                }
                else if (args[0] == "key"){
                    string x = Console.ReadKey(false).Key.ToString().ToLower();
                    WriteFile("output.txt", x);
                }
            }

        }
    }
}