import imageio
import imutils
import numpy as np
from Layer import Layer 
import cv2
class Exporter:
    fps = 30

    def __init__(self):
        print("Exporter initiated")

    def export(self, frames, outputPath):
        fps = self.fps
        writer = imageio.get_writer(outputPath, fps=fps)
        for frame in frames:
            writer.append_data(np.array(frame))
        writer.close()

    def exportLayers(self,underlay,  layers, outputPath, resizeWidth):
        fps = self.fps
        writer = imageio.get_writer(outputPath, fps=fps)
        i=0
        for layer in layers:
            data = layer.data
            contours = layer.bounds
            if len(data) < 10:
                continue
            for frame, contour in zip(data, contours):
                (x, y, w, h) = contour
                frame = frame
                frame1 = underlay
                frame1 = imutils.resize(frame1, width=resizeWidth)
                frame1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2RGB)
                frame1[y:y+frame.shape[0], x:x+frame.shape[1]] = frame
                cv2.putText(frame1,  str(i), (30,30), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255), 2)
                writer.append_data(np.array(frame1))
                #cv2.imshow("changes overlayed", frame)
                #cv2.waitKey(10) & 0XFF
            i += 1
        
        writer.close() 
        #cv2.destroyAllWindows()
    
    def exportOverlayed(self, underlay, layers, outputPath, resizeWidth):
        fps = self.fps
        writer = imageio.get_writer(outputPath, fps=fps)
        
        maxLength = self.getMaxLengthOfLayers(layers)

        for i in range(maxLength):
            frame1 = underlay
            frame1 = imutils.resize(frame1, width=resizeWidth)
            frame1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2RGB)
            for layer in layers:
                data = layer.data
                if len(layer.data) > i:
                    (x, y, w, h) = layer.bounds[i]
                    frame = layer.data[i]
                    if frame is not None:
                        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                        frame1[y:y+h, x:x+w] = frame
            writer.append_data(np.array(frame1))
        writer.close() 

    def getMaxLengthOfLayers(self, layers):
        maxLength = 0
        for layer in layers:
            if layer.getLength() > maxLength:
                maxLength = layer.getLength()
        return maxLength

