"""
test register cli
"""
import datetime
import shutil
from enum import Enum
from functools import cached_property
from itertools import product
from pathlib import Path

import attr
import cv2
import numpy as np
import pandas as pd
from loguru import logger
from moviepy.editor import *

from utils import divide_chunks, datenum_to_datetime, nearest, progressbar, RegisteredImage
import time # progress bar

from PIL import Image, ImageFont, ImageDraw
import os
from time import strptime
from dateutil import tz

import openpyxl
import scipy.io as sio
from datetime import datetime, timedelta

#-----------------------------------------------------------------------------#
#          Update this file path for Image Tagging - Tide Data
# Example: parent_dir = '/Users/admin/OneDrive - UNSW/My files/CoastSnap/'

parent_dir_ACTUAL = r"C:\Users\z5079346\OneDrive - UNSW\My files\CoastSnap"

#-----------------------------------------------------------------------------#   
# RETRIEVE IMAGES PARENT DIRECTORY IN .csv#

coastsnap_sites = pd.read_csv("C:/Users/z5079346/OneDrive - UNSW/Projects/Coastsnap_test/CoastSnap_Sites.csv")
parent_directory = coastsnap_sites.parent_directory[0] 
print(parent_directory)

#-----------------------------------------------------------------------------#   


folder = r"C:\Users\z5079346\OneDrive - UNSW\Projects\Coastsnap_test\Images_Test\cathieillaroo\Photoshop\2021"

'''
For each site in Images Parent Directory:
    Iterate through years in reverse:
        Iterate through images names (dates) in reverse:
            Check if image name in 'Registered'? If not, tag and save
            For this step, do image_name.replace('Processed' with 'Registered')
            


'''
def get_site_tide_data(parent_dir, site):
    
    # Retrieve tide data for the given site
    db = openpyxl.load_workbook(parent_dir + "/Database/CoastSnapDB.xlsx")
    beach_data = db[site]
    tide_filename = beach_data["B24"].value
    
    if tide_filename == 'NO_TIDE.mat':
        return False
        
    mat_path = parent_dir + '/Tide Data/' + tide_filename
    mat = sio.loadmat(mat_path)
    tide_dict = mat['tide']
    ts = tide_dict[0][0] # Format of tide/time Matlab data is gross
    tt = list(ts[0])     # ts[0] = tides, ts[1] = times, ts[2] = 
    
    print(site + ": " +"Loading tide data... (this may take half a minute)")     
    tide_times = [datenum_to_datetime(i) for i in tt] # THIS STEP TAKES A LONG TIME

    return tide_times, ts    


def tag_image(image2tag):
    
    image = Image.open(image2tag.pathname)
    draw = ImageDraw.Draw(image)
    image2tag.get_dimensions()
    
    # White Text Box 
    rect_height = image2tag.height/20
         
    # Create white text box
    draw.rectangle((0, 0, image2tag.width, rect_height), fill='white')

    # Tag image with text
    draw.text((20, rect_height/4),image2tag.tag, font = image2tag.font, fill=(0, 0, 0)) 
    
    registered_path_wrong = image2tag.pathname[:-4] + '_registered.jpg' 
    registered_path = registered_path_wrong.replace('Photoshop', 'Registered')
    image = image.convert('RGB')

    image.save(registered_path)

for site in os.listdir(parent_directory): # Loop through SITES
    i=0
    site_complete = False # A flag to stop tagging images when found
    tide_data = False
    font = None
    photoshop_path = parent_directory +'/'+ site + '/Photoshop'
    try: # Check if site contains 'Processed' directory
        years_list = os.listdir(photoshop_path)
        years_list.reverse()
    except: 
        continue
    for year in years_list: # Loop through YEARS
        if site_complete: break 
        year_path = photoshop_path + '/' + year
        image_list = os.listdir(year_path)
        image_list.reverse()
        for image_filename in image_list: # Loop through IMAGES
            #print(image_filename) 
            registered_year_path = year_path.replace('Photoshop', 'Registered')
            registered_image_path = registered_year_path + '/' + image_filename[:-4] + '_registered.jpg' 
            if site not in image_filename:
                continue
            # Check if image already tagged
            if os.path.isfile(registered_image_path): 
                site_complete = True;
                if i == 0:
                    print(site + ": " + str(i) + " images tagged")
                elif image2tag.tide:
                    print(site + ": " + str(i) + " images tagged with tide")
                else:
                    print(site + ": " + str(i) + " images tagged no tide")                  
                break
            else:
                if not os.path.exists(registered_year_path): # Chech that registered/year directory exists
                    os.makedirs(registered_year_path)     # if not, create it
                i += 1
                pathname = os.path.join(year_path, image_filename)
                image2tag = RegisteredImage(pathname, image_filename) # Create image object
                if i == 1: # Retrieve Tide Data once for each site
                    site_tide_data, ts = get_site_tide_data(parent_dir_ACTUAL, site)

                font = image2tag.get_font()
                    
                if site_tide_data:
                    image2tag.get_tide(site_tide_data, ts)
                    
                image2tag.create_tag()
                tag_image(image2tag)
 







    

"""
Tags images based on file name.
- Requires font file in coastsnap/fonts directory
"""

"""
def tag_images(folder):
    photoshop = True
    tide = False
    
    # Get image paths
    img_paths = [x for x in Path(folder).glob("*.jpg")]
    img_names = [str(x) for x in img_paths]
    logger.info(f"Tagging {len(img_names)} images")
    
    # Initialise white text box
    rect_height = 1
    image_width = 1
    
    # Check whether the directory 'tagged' exists or not
    path_name = img_names[0] 
    tagged_dir = str(Path(path_name).parent) + '/tagged'
    isExist = os.path.exists(tagged_dir)
    if not isExist:
      # Create a new directory because it does not exist 
      os.makedirs(tagged_dir)
    
    # Tide Data
    if tide:
        # Retrieve the site name from the first image
        filename = Path(img_names[0]).name
            
        filename_list = filename.split(".")
        site_name = filename_list[6]
        
        # Retrieve tide data for the given site
        db = openpyxl.load_workbook(parent_dir + "/Database/CoastSnapDB.xlsx")
        beach_data = db[site_name]
        tide_filename = beach_data["B24"].value
        mat_path = parent_dir + '/Tide Data/' + tide_filename
        mat = sio.loadmat(mat_path)
        tide_dict = mat['tide']
        ts = tide_dict[0][0] # Format of tide/time Matlab data is gross
        tt = list(ts[0])     # ts[0] = tides, ts[1] = times, ts[2] = 
        
        print("Loading tide data... (this may take half a minute)")     
        tide_times = [datenum_to_datetime(i) for i in tt] # THIS STEP TAKES A LONG TIME
    
        
    fontsize = 1
    for index, img in enumerate(sorted(img_names)):
        print("Image " + str(index+1))
        filename = str(Path(img).name)
        image = Image.open(img)
        draw = ImageDraw.Draw(image)
        
        
        # Retrieve tag information from file name
        filename_list = filename.split(".")
        posix_time = filename_list[0]
        date = filename_list[3].split("_")
        hour = date[1]
        minute = date[2]
        second = date[3]
        day = date[0]
        month = '{:02d}'.format(strptime(filename_list[2],'%b').tm_mon) # Ensure 2-digit format
        year = filename_list[5]
        timezone = filename_list[4]
        if 'snap' in filename_list:
            contributor = filename_list[8] # Mitch filename format
        else:
            contributor = filename_list[6] # Leaman filename format
        
        # Retrieve tide data 
        if tide:
    
            # Account for daylight savings
            # ASSUMPTION: All .mat tide files are in AEST. 
            if timezone == 'AEDT':
                hour = str(int(hour) - 1)
            date_string = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
            img_datetime = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S") # Image date/time as a datetime object
            
            tide_date = nearest(tide_times, img_datetime)
            mat_index = tide_times.index(tide_date)  # Retrieve the index of the .mat tide/time
            mat_tide = round(ts[1][mat_index][0], 2) # Associated tide
            
            print('Image date/time: ' + date_string)
            print('Tide record:     ' + str(tide_date))
        
        
        # Image tag
        if tide:
            txt = ('Date:' + year + '/' + month + '/' + day +  
                   ' Time:' + hour + ':' + minute + 
                   ' Contributor:' + contributor + 
                   ' Tide:' + str(mat_tide) + 'm AHD')
        else:
            txt = ('Date:' + year + '/' + month + '/' + day +  
                   ' Time:' + hour + ':' + minute + 
                   ' Contributor:' + contributor)
    
        
        # Set the fontsize, such that the tag covers 50% the width of the first image
        if index == 0:
            img_fraction = 0.8 # Change this to change the font size
            
            # White Text Box 
            image_width, image_height = image.size
            rect_height = image_height/20
            rect_width = image_width/1.5            
            font = ImageFont.truetype("fonts/Courier New Bold.ttf", fontsize) 
    
            while font.getsize(txt)[0] < img_fraction*image.size[0]:
                # iterate until the text size is just larger than the criteria
                fontsize += 1
                font = ImageFont.truetype("fonts/Courier New Bold.ttf", fontsize) 
        font = ImageFont.truetype("fonts/Courier New Bold.ttf", fontsize)
        
        # Create white text box
        draw.rectangle((0, 0, image_width, rect_height), fill='white')
    
        # Tag image with text
        draw.text((20, rect_height/4),txt, font = font, fill=(0, 0, 0)) 
        
        new_name = filename + '_registered.jpg' 
        print(new_name + '\n')
        new_path = tagged_dir + "/" + new_name
        image = image.convert('RGB')
        image.save(new_path)
        
    logger.info(f"Tagged Images for " +  + " Saved")


tag_images(folder)

"""


# # Set Image Label Info Based on Target Image (PUT AT LINE 90)
# target_image_path = str(Path(path_name).parent.parent.parent) + r"\Target Image\Target.JPG"
# print(target_image_path)
# image = Image.open(target_image_path)
# img_fraction = 0.6 # Change this to change the font size
# fontsize = 1

# # White Text Box 
# width, height = image.size
# rect_height = height/20
# rect_width = width/1.5            
# font = ImageFont.truetype("fonts/Courier New Bold.ttf", fontsize) 

# while font.getsize(txt)[0] < img_fraction*image.size[0]:
#     # iterate until the text size is just larger than the criteria
#     fontsize += 1
#     font = ImageFont.truetype("fonts/Courier New Bold.ttf", fontsize) 

#"C:\Users\z5079346\OneDrive - UNSW\My files\CoastSnap\Images\cathieillaroo\Registered\2022"
#"C:\Users\z5079346\OneDrive - UNSW\My files\CoastSnap\Images\cathieillaroo\Target Image\Target.JPG"

