import os
import sys
import eel
import keyboard

import cv2

import dialogs
import utils

import motion_deblur
import super_resolution

IUT = None # Image Under Test
VUT = None # Video Under Test
callNum = False

eel.init('views')

@eel.expose
def ask_file():
  return dialogs.ask_file()

@eel.expose
def ask_and_save_image(image_base64_str):
  save_file_name = dialogs.ask_save_as_image_file()
  image = utils.base64_to_image(image_base64_str)
  if save_file_name:
    cv2.imwrite(save_file_name, image)

@eel.expose
def submit_vut_path(video_path):
  global VUT
  VUT = cv2.VideoCapture(video_path)
  

@eel.expose
def submit_iut_path(image_path):
  global IUT
  IUT = cv2.imread(image_path)
  
@eel.expose
def submit_iut(img_str):
  global IUT
  IUT = utils.base64_to_image(img_str)

@eel.expose
def do_motion_deblur_v(angle, strength, snr, iut_already_displayed, x = -1, y = -1, w = -1, h = -1):
  global IUT
  video = VUT
  # video.set(1, 0)
  
  fps = video.get(cv2.CAP_PROP_FPS)
  
  # print(fps)
  
  timeNeeded = 1000 // fps
  
  print(timeNeeded)
  
  while(video.isOpened()):
    
    
    try:
      ret, frame = video.read()
      
      if(ret == True):
        IUT = frame
        # print("do ke upar")
        do_motion_deblur(angle, strength, snr, False)
        # print("do ke niche")
        cv2.waitKey(15)
        
        if(keyboard.is_pressed("q")):
          break
        elif(keyboard.is_pressed("d")):
          currentPos = video.get(cv2.CAP_PROP_POS_FRAMES)
          finalPos = int(currentPos + fps*5)
          totalFrames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
          
          if(finalPos > totalFrames):
            finalPos = totalFrames - 1
          
          video.set(1, finalPos)
        elif(keyboard.is_pressed("a")) :
          currentPos = video.get(cv2.CAP_PROP_POS_FRAMES)
          finalPos = int(currentPos - fps*5)
          
          if(finalPos < 0):
            finalPos = 0
          
          video.set(1, finalPos)
        elif(keyboard.is_pressed("s")):
          keyboard.wait("s")
          
        
        
      else:
        print("Video Over")
        video.release()
        break
    except Exception as e:
      print(e)
    
    
    
    
  # print("While ke niiche")
  
  # if(video.isOpened()):
  #   print("video is opened")
  # else:
  #   print("Not opened")


@eel.expose
def do_motion_deblur(angle, strength, snr, iut_already_displayed):
  image = IUT
  # print(type(image))
  if not iut_already_displayed:
    # print("99")
    img_base64_str = utils.image_to_base64(image)
    eel.displayIUT(img_base64_str)
    # print("Display Done")

  try:
    psf, res_image = motion_deblur.motion_deblur(image, angle, strength, snr)

    # cv2.imshow("a", res_image)
    # cv2.waitKey(0)

    psf_base64_str = utils.image_to_base64(psf)
    eel.displayPSF(psf_base64_str)
    
    res_image_base64_str = utils.image_to_base64(res_image)
    eel.displayOUT(res_image_base64_str)  
  except:
    print("e")
  

@eel.expose
def do_super_resolution():
  image = IUT

  img_base64_str = utils.image_to_base64(image)
  eel.displayIUTforSuperResolution(img_base64_str)

  res_image = super_resolution.super_resolve(image)
  res_image_base64_str = utils.image_to_base64(res_image)
  eel.displayOUTforSuperResolution(res_image_base64_str)

def start():
  eel.start('index.html', size=(1024, 768), port=8000)


if __name__ == "__main__":
  start()