
import logging
import pygame
from app import *
from pygame.locals import *
from werkzeug.serving import run_simple

from web import webapp as w
import data_access as da

logging.basicConfig(filename='setlistmanager.log', level=logging.DEBUG)
SCREEN_WIDTH = 160
SCREEN_HEIGHT = 128

class Button:
    pass


class Text():
    """Create a text object."""

    def __init__(self, surface, text, pos, **options):
        self.text = text
        self.surface = surface
        self.pos = pos
        self.bold = True
        self.italic = False
        self.underline = False
        self.background = None # Color('white')
        self.font = pygame.font.SysFont('Arial', 64)
        self.fontname = None # 'Free Sans'
        self.fontsize = 40
        self.fontcolor = Color('black')
        self.set_font()
        da.connect_db('db.db')
        songs = da.get_all_songs_as_json()
        print (songs)

        # self.words = [word.split(' ') for word in self.text.splitlines()]  # 2D array where each row is a list of words.
        # self.space = self.font.size(' ')[0]  # The width of a space.
        # max_width, max_height = self.surface.get_size()
        # x, y = self.pos
        # for line in self.words:
        #     for word in line:
        #         word_surface = self.font.render(word, 0, self.fontcolor)
        #         # print(word)
        #         word_width, word_height = word_surface.get_size()
        #         if x + word_width >= max_width:
        #             x = pos[0]  # Reset the x.
        #             y += word_height  # Start on new row.
        #         surface.blit(word_surface, (x, y))
        #         x += word_width + self.space
        #     x = pos[0]  # Reset the x.
        #     y += word_height  # Start on new row.

        self.render()

    def set_font(self):
        """Set the font from its name and size."""
        self.font = pygame.font.Font(self.fontname, self.fontsize)
        self.font.set_bold(self.bold)
        self.font.set_italic(self.italic)
        self.font.set_underline(self.underline)

    def render(self):
        """Render the text into an image."""
        self.img = self.font.render(self.text, True, self.fontcolor, self.background)
        self.rect = self.img.get_rect()
        self.rect.size = self.img.get_size()
        self.rect.topleft = self.pos

    def draw(self):
        """Draw the text image to the screen."""
        # Put the center of surf at the center of the display
        surf_center = (
            (SCREEN_WIDTH - self.rect.width)/2,
            (SCREEN_HEIGHT - self.rect.height)/2
        )
        App.screen.blit(self.img, surf_center)
        # App.screen.blit(self.img, self.rect)


class App:
    """Create a single-window app with multiple scenes."""

    def __init__(self):
        """Initialize pygame and the application."""
        logging.debug('Initializing App')
        pygame.init()
        pygame.mouse.set_cursor((8, 8), (0, 0), (0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0))
        self.shortcuts = {
            (K_x, KMOD_LMETA): 'print("cmd+X")',
            (K_x, KMOD_LALT): 'print("alt+X")',
            (K_x, KMOD_LCTRL): 'print("ctrl+X")',
            (K_x, KMOD_LMETA + KMOD_LSHIFT): 'print("cmd+shift+X")',
            (K_x, KMOD_LMETA + KMOD_LALT): 'print("cmd+alt+X")',
            (K_x, KMOD_LMETA + KMOD_LALT + KMOD_LSHIFT): 'print("cmd+alt+shift+X")',
        }
        self.color = Color('green')
        self.flags = RESIZABLE
        self.rect = Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
        App.screen = pygame.display.set_mode(self.rect.size, self.flags)
        App.t = Text(App.screen, 'Chorus', pos=(0, 0))
        App.running = True

    def run(self):
        """Run the main event loop."""
        logging.debug('entering method run')

        app = w.create_app()
        run_simple('127.0.0.1', 5000, app, use_debugger=True, use_reloader=True)

        logging.debug('after start of flask')
        while App.running:
            logging.debug('.')
            for event in pygame.event.get():
                if event.type == QUIT:
                    App.running = False
                if event.type == KEYDOWN:
                    self.do_shortcut(event)
            App.screen.fill(self.color)
            App.t.draw()
            pygame.display.update()
        logging.debug('exiting setlistmanager')
        pygame.quit()

    def do_shortcut(self, event):
        """Find the the key/mod combination in the dictionary and execute the cmd."""
        k = event.key
        m = event.mod
        if (k, m) in self.shortcuts:
            exec(self.shortcuts[k, m])

    def toggle_fullscreen(self):
        """Toggle between full screen and windowed screen."""
        self.flags ^= FULLSCREEN
        pygame.display.set_mode((0, 0), self.flags)

    def toggle_resizable(self):
        """Toggle between resizable and fixed-size window."""
        self.flags ^= RESIZABLE
        pygame.display.set_mode(self.rect.size, self.flags)

    def toggle_frame(self):
        """Toggle between frame and noframe window."""
        self.flags ^= NOFRAME
        pygame.display.set_mode(self.rect.size, self.flags)

[((171, 242), 'logging.basicConfig', 'logging.basicConfig', ([], {'filename': '"""setlistmanager.log"""', 'level': 'logging.DEBUG'}), "(filename='setlistmanager.log', level=logging.DEBUG)\n", (190, 242), False, 'import logging\n'), ((641, 673), 'pygame.font.SysFont', 'pygame.font.SysFont', (['"""Arial"""', '(64)'], {}), "('Arial', 64)\n", (660, 673), False, 'import pygame\n'), ((816, 838), 'data_access.connect_db', 'da.connect_db', (['"""db.db"""'], {}), "('db.db')\n", (829, 838), True, 'import data_access as da\n'), ((855, 881), 'data_access.get_all_songs_as_json', 'da.get_all_songs_as_json', ([], {}), '()\n', (879, 881), True, 'import data_access as da\n'), ((1895, 1941), 'pygame.font.Font', 'pygame.font.Font', (['self.fontname', 'self.fontsize'], {}), '(self.fontname, self.fontsize)\n', (1911, 1941), False, 'import pygame\n'), ((2861, 2894), 'logging.debug', 'logging.debug', (['"""Initializing App"""'], {}), "('Initializing App')\n", (2874, 2894), False, 'import logging\n'), ((2903, 2916), 'pygame.init', 'pygame.init', ([], {}), '()\n', (2914, 2916), False, 'import pygame\n'), ((2925, 3020), 'pygame.mouse.set_cursor', 'pygame.mouse.set_cursor', (['(8, 8)', '(0, 0)', '(0, 0, 0, 0, 0, 0, 0, 0)', '(0, 0, 0, 0, 0, 0, 0, 0)'], {}), '((8, 8), (0, 0), (0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0,\n    0, 0, 0, 0, 0))\n', (2948, 3020), False, 'import pygame\n'), ((3568, 3619), 'pygame.display.set_mode', 'pygame.display.set_mode', (['self.rect.size', 'self.flags'], {}), '(self.rect.size, self.flags)\n', (3591, 3619), False, 'import pygame\n'), ((3769, 3805), 'logging.debug', 'logging.debug', (['"""entering method run"""'], {}), "('entering method run')\n", (3782, 3805), False, 'import logging\n'), ((3821, 3835), 'web.webapp.create_app', 'w.create_app', ([], {}), '()\n', (3833, 3835), True, 'from web import webapp as w\n'), ((3844, 3916), 'werkzeug.serving.run_simple', 'run_simple', (['"""127.0.0.1"""', '(5000)', 'app'], {'use_debugger': '(True)', 'use_reloader': '(True)'}), "('127.0.0.1', 5000, app, use_debugger=True, use_reloader=True)\n", (3854, 3916), False, 'from werkzeug.serving import run_simple\n'), ((3926, 3963), 'logging.debug', 'logging.debug', (['"""after start of flask"""'], {}), "('after start of flask')\n", (3939, 3963), False, 'import logging\n'), ((4341, 4380), 'logging.debug', 'logging.debug', (['"""exiting setlistmanager"""'], {}), "('exiting setlistmanager')\n", (4354, 4380), False, 'import logging\n'), ((4389, 4402), 'pygame.quit', 'pygame.quit', ([], {}), '()\n', (4400, 4402), False, 'import pygame\n'), ((4781, 4824), 'pygame.display.set_mode', 'pygame.display.set_mode', (['(0, 0)', 'self.flags'], {}), '((0, 0), self.flags)\n', (4804, 4824), False, 'import pygame\n'), ((4960, 5011), 'pygame.display.set_mode', 'pygame.display.set_mode', (['self.rect.size', 'self.flags'], {}), '(self.rect.size, self.flags)\n', (4983, 5011), False, 'import pygame\n'), ((5134, 5185), 'pygame.display.set_mode', 'pygame.display.set_mode', (['self.rect.size', 'self.flags'], {}), '(self.rect.size, self.flags)\n', (5157, 5185), False, 'import pygame\n'), ((4003, 4021), 'logging.debug', 'logging.debug', (['"""."""'], {}), "('.')\n", (4016, 4021), False, 'import logging\n'), ((4047, 4065), 'pygame.event.get', 'pygame.event.get', ([], {}), '()\n', (4063, 4065), False, 'import pygame\n'), ((4309, 4332), 'pygame.display.update', 'pygame.display.update', ([], {}), '()\n', (4330, 4332), False, 'import pygame\n')]