# sankalp/cli.py

import curses
import webbrowser

menu_items = [
    "Sankalp Shrivastava",
    "I'm a technologist and an entrepreneur.",
    "Some thoughts here",
    "Twitter",
    "Github",
    "Bento",
    "Email: s@sankalp.sh"
]

links = [
    "",
    "",
    "https://sankalp.sh/thoughts.html",
    "https://twitter.com/1sankalp",
    "https://github.com/1Sankalp",
    "https://bento.me/1sankalp",
    "mailto:s@sankalp.sh"
]

def print_menu(stdscr, selected_row_idx):
    stdscr.clear()
    h, w = stdscr.getmaxyx()
    for idx, row in enumerate(menu_items):
        x = w//2 - len(row)//2
        y = h//2 - len(menu_items)//2 + idx
        if idx == selected_row_idx:
            stdscr.attron(curses.color_pair(1))
            stdscr.addstr(y, x, row)
            stdscr.attroff(curses.color_pair(1))
        else:
            stdscr.addstr(y, x, row)
    stdscr.refresh()

def run_cli():
    curses.wrapper(main)

def main(stdscr):
    curses.curs_set(0)
    curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
    current_row = 0

    print_menu(stdscr, current_row)

    while True:
        key = stdscr.getch()
        if key == curses.KEY_UP and current_row > 0:
            current_row -= 1
        elif key == curses.KEY_DOWN and current_row < len(menu_items) - 1:
            current_row += 1
        elif key == curses.KEY_ENTER or key in [10, 13]:
            if links[current_row]:
                webbrowser.open(links[current_row])
        print_menu(stdscr, current_row)
