import curses
from resources.digits import bigDigits,bigDigitsIndexes
NumericWidth = 142
class windowManager:
    def __init__(self,stdscr):
        curses.curs_set(0)
        self.initializeColors()
        self.initializeWindows(stdscr)
        self.resizeWindows()
        self.blinking = False;
    def initializeColors(self):
        if curses.can_change_color():
            curses.init_color(1,100,100,100) #color 1 is grey
            curses.init_pair(1,curses.COLOR_CYAN,1) #timer
            curses.init_pair(2,curses.COLOR_WHITE,1) # background
            curses.init_pair(3,1,curses.COLOR_CYAN) # scramble

        else:
            curses.init_pair(1,curses.COLOR_WHITE,curses.COLOR_BLACK)
            curses.init_pair(2,curses.COLOR_WHITE,curses.COLOR_BLACK)
    def initializeWindows(self,stdscr):
        self.mainScreen = stdscr
        self.winTimer = curses.newwin(1,1,0,0)
        self.winLog = curses.newwin(1,1,0,0)
        self.winOptions = curses.newwin(1,1,0,0)
        self.winScramble = curses.newwin(1,1,0,0)
        self.winStats = curses.newwin(1,1,0,0)

    def resizeWindows(self):
        (maxY,maxX) = self.mainScreen.getmaxyx()
        self.mainScreen.bkgd(' ',curses.color_pair(1))
        self.mainScreen.refresh()
        if(maxX>NumericWidth):
            self.winTimer.mvwin(1,int((maxX-NumericWidth)/2))
            self.winTimer.resize(16,NumericWidth+1)
            self.winTimer.bkgd(' ',curses.color_pair(1))
            self.winScramble.mvwin(17,0)
            self.winScramble.resize(3,maxX)
            self.winScramble.bkgd(' ',curses.color_pair(1))
            self.winOptions.mvwin(21,0)
            self.winOptions.resize(7,maxX)
            self.winOptions.bkgd(' ',curses.color_pair(1))
            self.winLog.mvwin(30,2)
            self.winLog.resize(30,60)
            self.winLog.bkgd(' ',curses.color_pair(1))
        else:
            raise ValueError('toosmall')
        curses.doupdate()

    def centerTime(self):
        (maxY,maxX) = self.mainScreen.getmaxyx()
        self.winTimer.mvwin(int((maxY-16)/3),int((maxX-NumericWidth)/3))
        self.mainScreen.bkgd(' ',curses.color_pair(1))
        self.mainScreen.refresh()

    def showScramble(self,scramble):
        #self.winScramble.erase()
        (maxY,maxX)=self.winScramble.getmaxyx()
        startXCoord = int((maxX-len(scramble))/2)
        startYCoord = maxY-1
        self.winScramble.erase()
        self.winScramble.border()
        self.winScramble.addstr(1,startXCoord,scramble)
        self.winScramble.refresh()

    def showLog(self,dataObj):
        self.winLog.clear()
        self.winLog.border()
        line = 1
        for i in dataObj:
            stringToWrite = str(i[1])+ ". " 
            time=i[0]
            if time == None:
                stringToWrite += "     DNF"
            else:
                mins = int(time / 60)
                sex = time % 60