import sys,os
import curses

home = "home = "
away = "away = "
qtr = "qtr = "
ascore = "ascore = "
hscore = "hscore = "
uhscore = ""
uacscore = ""
uqtr = ""



def datPrint(var):
    file = open("sbdat.txt", "r")
    writer = file.read()
    out = writer[writer.find(var) + len(var):-(len(writer) - writer.find(";", writer.find(var)))]
    file.close()
    return out


def draw_menu(stdscr):
    k = 0
    cursor_x = 0
    cursor_y = 0

    # Clear and refresh the screen for a blank canvas
    stdscr.clear()
    stdscr.refresh()

    # Start colors in curses
    curses.start_color()
    curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
    curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
    curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)

    # Loop where k is the last character pressed
    while (k != ord('q')):

        # Initialization
        stdscr.clear()
        height, width = stdscr.getmaxyx()

        if k == curses.KEY_DOWN:
            cursor_y = cursor_y + 1
        elif k == curses.KEY_UP:
            cursor_y = cursor_y - 1
        elif k == curses.KEY_RIGHT:
            cursor_x = cursor_x + 1
        elif k == curses.KEY_LEFT:
            cursor_x = cursor_x - 1

        cursor_x = max(0, cursor_x)
        cursor_x = min(width-1, cursor_x)

        cursor_y = max(0, cursor_y)
        cursor_y = min(height-1, cursor_y)


        line1 = "{} {} {}".format(datPrint(away).center(10), "QTR".center(10), datPrint(home).center(10))
        line2 = "{} {} {}".format(datPrint(ascore).center(10), datPrint(qtr).center(10), datPrint(hscore).center(10))



        # Declaration of strings
        top = ('*' * 32)[:width-1]
        namesQtr = "{}".format(line1)[:width-1]
        scoreQTR = "{}".format(line2)[:width-1]
        bottom = top
        statusbarstr = "RUNNING HOE"[:width-1]

        if k == 0:
            keystr = "Press any key"[:width-1]

        # Centering calculations
        start_x_top = int((width // 2) - (len(top) // 2) - len(top) % 2)
        start_x_namesQTR = int((width // 2) - (len(top) // 2) - len(top) % 2)
        start_x_scoreQTR = int((width // 2) - (len(top) // 2) - len(top) % 2)
        start_x_bottom = int((width // 2) - (len(top) // 2) - len(top) % 2)
        start_y = int((height // 2) - 2)

        # Rendering some text
        whstr = "Width: {}, Height: {}".format(width, height)
        stdscr.addstr(0, 0, whstr, curses.color_pair(1))

        # Render status bar
        stdscr.attron(curses.color_pair(3))
        stdscr.addstr(height-1, 0, statusbarstr)
        stdscr.addstr(height-1, len(statusbarstr), " " * (width - len(statusbarstr) - 1))
        stdscr.attroff(curses.color_pair(3))

        # Turning on attributes for title
        stdscr.attron(curses.color_pair(2))
        stdscr.attron(curses.A_BOLD)

        # Rendering title <---DISCONTINUED

        # Turning off attributes for title
        stdscr.attroff(curses.color_pair(2))
        stdscr.attroff(curses.A_BOLD)

        # Print all of text
        stdscr.addstr(start_y, start_x_top, top)
        stdscr.addstr(start_y + 1, start_x_namesQTR, namesQtr)
        stdscr.addstr(start_y + 2, start_x_scoreQTR, scoreQTR)
        # stdscr.addstr(start_y + 3, (width // 2) - 2, '-' * 4)
        stdscr.addstr(start_y + 3, start_x_bottom, bottom)
        #stdscr.addstr(start_y + 5, start_x_keystr, keystr)
        stdscr.move(cursor_y, cursor_x)

        # Refresh the screen
        stdscr.refresh()

        # Wait for next input
        k = stdscr.getch()

def main():
    curses.wrapper(draw_menu)

if __name__ == "__main__":
    main()
