#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()
        self._is_empty = True
        super().__init__(play_clock, context, 'Voice')

    def is_full(self):
        return self._queue.qsize() >= self._context.render_batch * 2

    def _run_step(self):
        try:
            audio_frames, ps = self._queue.get(block=True, timeout=0.01)
            # print('voice render queue size', self._queue.qsize())
        except Empty:
            self._context.notify({'msg_id':  MessageType.Video_Render_Queue_Empty})
            if not self._is_empty:
                print('voice render queue empty')
                self._is_empty = True
            return

        if self._is_empty:
            print('voice render queue not empty')
            self._is_empty = False

        status = MessageType.Video_Render_Queue_Not_Empty
        if self._queue.qsize() < self._context.render_batch:
            status = MessageType.Video_Render_Queue_Empty
        elif self._queue.qsize() >= self._context.render_batch * 2:
            status = MessageType.Video_Render_Queue_Full
        self._context.notify({'msg_id': status})

        self._play_clock.update_display_time()
        self._play_clock.current_time = ps

        for audio_frame in audio_frames:
            frame, type_ = audio_frame
            frame = (frame * 32767).astype(np.int16)

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