using System;

namespace BoxMenu{
    public class Program{
        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;
        }
        static void Main(string[] args){}
    }
}