# Path: art/baseclasses/scrolltext.py
# class ScrollText(ArtBaseClass):
# 
#     description = "Scroll text across the display"
# 
#     fg = None
#     bg = None
# 
#     def __init__(self, matrix, config):
#         self.config = config
#         self._initText()
#         self.thisMessage = self._getText()
#         self.nextMessage = self._getText()
# 
#         self.typeface = OPCText(typeface_bbc)
#         self.base = 0
# 
#     def _initText(self):
#         raise NotImplementedError
# 
#     def _getText(self):
#         raise NotImplementedError
# 
#     def start(self, matrix):
#         matrix.setFirmwareConfig(nointerp=True)
# 
#     def refresh(self, matrix):
#         matrix.clear(self.bg)
# 
#         y = matrix.height/2 - 4
# 
#         end = self.typeface.drawText(matrix, 0-self.base, y,
#                                      self.thisMessage, self.fg, self.bg)
# 
#         # drawText returns None if the image ran to the end of the page. If it
#         # didn't, then it's time to bring in the new one.
#         if end is not None:
#             self.typeface.drawText(matrix, end, y,
#                                    self.nextMessage, self.fg, self.bg)
#             if end == 1:
#                 # this is the final pixel for the original text. So next time
#                 # through, next message is the active messge.
#                 self.thisMessage = self.nextMessage
#                 self.nextMessage = self._getText()
#                 self.base = -1
# 
#         self.base += 1
# 
#     def interval(self):
#         return 50


from .baseclasses.scrolltext import ScrollText
from opc.colors import BLUE, WHITE
from random import randint
import os
FILE = "assets/fortunes.txt"


class Art(ScrollText):

    description = "Scroll classic fortunes across the display"

    fg = WHITE
    bg = BLUE

    def _initText(self):
        # TODO:catch file exceptions
        stats = os.stat(FILE)
        self.length = stats.st_size
        self.file = open(FILE, "U")

    def _seekFortune(self):
        index = randint(0, self.length)
        self.file.seek(index)

        while self.file.readline()[0] != "%":
            pass

    def _readFortune(self):
        buffer = "+++ "
        while True:
            line = self.file.readline()
            if line == "":
                if buffer == "":
                    # we teleported to the end of the file. Try again.

# Path: art/baseclasses/scrolltext.py
# class ScrollText(ArtBaseClass):
# 
#     description = "Scroll text across the display"
# 
#     fg = None
#     bg = None
# 
#     def __init__(self, matrix, config):
#         self.config = config
#         self._initText()
#         self.thisMessage = self._getText()
#         self.nextMessage = self._getText()
# 
#         self.typeface = OPCText(typeface_bbc)
#         self.base = 0
# 
#     def _initText(self):
#         raise NotImplementedError
# 
#     def _getText(self):
#         raise NotImplementedError
# 
#     def start(self, matrix):
#         matrix.setFirmwareConfig(nointerp=True)
# 
#     def refresh(self, matrix):
#         matrix.clear(self.bg)
# 
#         y = matrix.height/2 - 4
# 
#         end = self.typeface.drawText(matrix, 0-self.base, y,
#                                      self.thisMessage, self.fg, self.bg)
# 
#         # drawText returns None if the image ran to the end of the page. If it
#         # didn't, then it's time to bring in the new one.
#         if end is not None:
#             self.typeface.drawText(matrix, end, y,
#                                    self.nextMessage, self.fg, self.bg)
#             if end == 1:
#                 # this is the final pixel for the original text. So next time
#                 # through, next message is the active messge.
#                 self.thisMessage = self.nextMessage
#                 self.nextMessage = self._getText()
#                 self.base = -1
# 
#         self.base += 1
# 
#     def interval(self):
#         return 50


# Path: art/fortune.py
from .baseclasses.scrolltext import ScrollText
from opc.colors import BLUE, WHITE
from random import randint
import os

FILE = "assets/fortunes.txt"


class Art(ScrollText):

    description = "Scroll classic fortunes across the display"

    fg = WHITE
    bg = BLUE

    def _initText(self):
        # TODO:catch file exceptions
        stats = os.stat(FILE)
        self.length = stats.st_size
        self.file = open(FILE, "U")

    def _seekFortune(self):
        index = randint(0, self.length)
        self.file.seek(index)

        while self.file.readline()[0] != "%":
            pass

    def _readFortune(self):
        buffer = "+++ "
        while True:
            line = self.file.readline()
            if line == "":
                if buffer == "":
                    # we teleported to the end of the file. Try again.

                    return self._getFortune()