#encoding = utf8
import logging
import os
from queue import Queue

import pygame
from pygame.locals import *

from human import HumanContext
from utils import config_logging, read_image

logger = logging.getLogger(__name__)
current_file_path = os.path.dirname(os.path.abspath(__file__))


class PyGameUI:
    def __init__(self):
        self._human_context = None
        self._queue = None
        self.screen_ = pygame.display.set_mode((800, 600), HWSURFACE | DOUBLEBUF | RESIZABLE)
        self.clock = pygame.time.Clock()

        background = os.path.join(current_file_path, '..', 'data', 'background', 'background.jpg')
        logger.info(f'background: {background}')
        self._background = pygame.image.load(background).convert()
        self.background_display_ = pygame.transform.scale(self._background, (800, 600))
        self._human_image = None
        self.running = True

    def start(self):
        self._queue = Queue()
        self._human_context = HumanContext()
        self._human_context.build()
        render = self._human_context.render_handler
        render.set_image_render(self)

    def run(self):
        self.start()
        while self.running:
            self.clock.tick(60)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
                elif event.type == VIDEORESIZE:
                    self.background_display_ = pygame.transform.scale(self._background, event.dict['size'])
            self.screen_.blit(self.background_display_, (0, 0))
            self._update_human()
            if self._human_image is not None:
                self.screen_.blit(self._human_image, (0, 0))
            pygame.display.flip()
        self.stop()
        pygame.quit()

    def _update_human(self):
        if self._queue.empty():
            return
        image = self._queue.get()
        self._human_image = pygame.image.frombuffer(image.tobytes(), image.shape[1::-1], "RGB")

    def stop(self):
        logger.info('stop')
        if self._human_context is not None:
            self._human_context.pause_talk()
            self._human_context.stop()

    def on_render(self, image):
        self._queue.put(image)


if __name__ == '__main__':
    config_logging('../logs/info.log', logging.INFO, logging.INFO)

    logger.info('------------start------------')
    ui = PyGameUI()
    ui.run()
    logger.info('------------finish------------')