#encoding = utf8
import logging
import time
from queue import Empty

import numpy as np

from audio_render import AudioRender
from human.message_type import MessageType
from .base_render import BaseRender

logger = logging.getLogger(__name__)


class VoiceRender(BaseRender):
    def __init__(self, play_clock, context):
        self._audio_render = AudioRender()
        super().__init__(play_clock, context, 'Voice')
        self._current_text = ''

    def render(self, frame, ps):
        self._play_clock.update_display_time()
        self._play_clock.current_time = ps

        for audio_frame in frame:
            frame, type_ = audio_frame
            chunk, txt = frame
            if txt != self._current_text:
                self._current_text = txt
                logging.info(f'VoiceRender: {txt}')
            chunk = (chunk * 32767).astype(np.int16)

            if self._audio_render is not None:
                try:
                    chunk_len = int(chunk.shape[0] * 2)
                    # print('audio frame:', frame.shape, chunk_len)
                    self._audio_render.write(chunk.tobytes(), chunk_len)
                except Exception as e:
                    logging.error(f'Error writing audio frame: {e}')
