#!/bin/python2

import curses
import locale

screen = curses.initscr()
curses.noecho()
curses.cbreak()
screen.keypad(1)

maxrings = 9
ringstate = range(maxrings)


def setup():
    for i in range(maxrings):
        ringstate[i] = True

def drawwin(curwin, title):
    curwin.border(0)
    curwin.addch(0, 2, curses.ACS_RTEE)
    title = " " + title + " "
    curwin.addstr(0, 3, title)
    curwin.addch(0, len(title) + 3, curses.ACS_LTEE)

def drawrings(curwin):
    for i in range(maxrings):
        if ringstate[i]:
            curwin.addstr(5, 4 + 4 * (maxrings - i - 1), str(i + 1), curses.A_REVERSE)
        else:
            curwin.addstr(5, 4 + 4 * (maxrings - i - 1), str(i + 1))

def drawringselector(curwin, ringsel):
    curwin.addch(7, 4 + 4 * (maxrings - ringsel - 1), '^')
    curwin.addch(8, 4 + 4 * (maxrings - ringsel - 1), '|')

def didwewin():
    for i in range(maxrings):
        if ringstate[i] == True:
            return False
    return True

def drawinfo(curwin, failedring, winstate):
    curwin.addstr(10, 4, "<Space> Toggle Ring")
    curwin.addstr(11, 4, "    <R> Reset Rings")
    curwin.addstr(12, 4, "    <Q> Quit")
    if failedring:
        curwin.addstr(2, 4, "The ring won't pass through...", curses.A_REVERSE)
    elif winstate:
        curwin.addstr(2, 4, "Yay! You won! Good for you...")
    else:
        curwin.addstr(2, 4, "Remove all rings (un-hilight).")

def adjustwinpositiontocenter(curwin):
    winheight, winwidth = curwin.getmaxyx()
    scrheight, scrwidth = screen.getmaxyx()

    posx = scrwidth // 2 - winwidth // 2
    posy = scrheight // 2 - winheight // 2

    if posx < 0:
        posx = 0
    if posy < 0:
        posy = 0

    curwin.mvwin(posy, posx)

def trytogglering(ringsel):
    if ringsel == 0:
        ringstate[ringsel] = not ringstate[ringsel]
        return True
    elif ringstate[ringsel - 1]:
        cantoggle = True
        if ringsel > 1:
            for i in range(ringsel - 1):
                if ringstate[i]:
                    cantoggle = False
        if cantoggle == True:
            ringstate[ringsel] = not ringstate[ringsel]
            return True
    return False

def drawshuttle(curwin):
    position = -1
    for i in range(maxrings):
        if ringstate[i]:
            position = i
            break

    if position == -1:
        return

    vposition = 6 + 4 * (maxrings - position - 1)
    for i in range(vposition):
        curwin.addch(4, i, curses.ACS_HLINE)
        curwin.addch(6, i, curses.ACS_HLINE)

    curwin.addch(4, vposition, curses.ACS_URCORNER)
    curwin.addch(5, vposition, curses.ACS_VLINE)
    curwin.addch(6, vposition, curses.ACS_LRCORNER)

def main():
    width = 4 * maxrings + 5
    if width < 35:
        width = 35
    win = curses.newwin(15, width, 0, 0)

    setup()
    ringsel = 0

    failedring = False
    winstate = False

    c = 0
    while c != ord('q'):
        screen.erase()
        win.erase()

        adjustwinpositiontocenter(win)
        
        winstate = didwewin()
        drawrings(win)
        drawshuttle(win)
        drawringselector(win, ringsel)
        drawwin(win, "Baguenaudier")
        drawinfo(win, failedring, winstate)

        screen.noutrefresh()
        win.noutrefresh()
        curses.doupdate()

        failedring = False
        c = screen.getch()

        if c == curses.KEY_LEFT and ringsel < maxrings - 1:
            ringsel += 1
        if c == curses.KEY_RIGHT and ringsel > 0:
            ringsel -= 1
        if c == ord('r'):
            setup()
        if c == ord(' '):
            failedring = not trytogglering(ringsel)


try:
    main()
finally:
    curses.nocbreak()
    screen.keypad(0)
    curses.echo()
    curses.endwin()

