#encoding = utf8
import copy
import time
from queue import Empty
from enum import Enum

import cv2
import numpy as np

from .base_render import BaseRender
from human.message_type import MessageType


class VideoRender(BaseRender):
    def __init__(self, play_clock, context, human_render):
        super().__init__(play_clock, context, 'Video')
        self._human_render = human_render

    def render(self, frame, ps):
        res_frame, idx, type_ = frame

        if type_ == 0:
            combine_frame = self._context.frame_list_cycle[idx]
        else:
            bbox = self._context.coord_list_cycle[idx]
            combine_frame = copy.deepcopy(self._context.frame_list_cycle[idx])
            y1, y2, x1, x2 = bbox
            try:
                res_frame = cv2.resize(res_frame.astype(np.uint8), (x2 - x1, y2 - y1))
            except:
                print('resize error')
                return
            combine_frame[y1:y2, x1:x2, :3] = res_frame

        image = combine_frame
        # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        if self._human_render is not None:
            self._human_render.put_image(image)
