import sys, glob, csv, os

# Get the right version of urlretrieve and input
if sys.version_info.major == 2:
  # using python 2
  from urllib2 import urlretrieve
  input = raw_input # rename it 
elif sys.version_info.major == 3:
  from urllib.request import urlretrieve


def download_images(urls, names, folder):

  number_of_images = len(urls)
  print("Now I will download {} images.".format(number_of_images))

  ii = 0
  for url, name in zip(urls, names):
    ii = ii + 1
    full_url = "https://materialsinpaintings.tudelft.nl/media/CACHE/images/" + url
    suffix = url.split('.')[-1]
    target = os.path.join(folder, name + '.' +suffix)
    print(full_url)
    print(target)
    urlretrieve(full_url, target)
    print("Image {} out {} downloaded".format(ii, number_of_images))

def get_input(string):
  reply = input(string)
  if reply == 'y':
    return 1
  elif reply == 'n':
    return 0
  else:
    print("Reply was neither 'y' nor 'n'. Be sure not to include the quotation marks.")
    return get_input(string)

def read_file(csv_file):
  urls, names = [], []
  padding = 0
  with open(csv_file, newline='', encoding='utf-8') as file:
      reader = csv.reader(file, delimiter=',')
      headers = next(reader, None) # skip header
      paintings_or_boxes = 'paintings' if headers[0] == 'painting_id' else 'boxes' # is this selection for paintings or for boxes?
      url_idx = 8 if paintings_or_boxes == 'paintings' else 5
      if paintings_or_boxes == 'boxes':
        padding = get_input("Do you want to download the boxes with padding? Hit the 'y' key for yes, else hit 'n' to download without padding.")
        url_idx = url_idx + padding # offset by 0 or 1
      for line in reader:
          names.append(line[0])
          urls.append(line[url_idx])
  
  return urls, names, paintings_or_boxes, padding

def make_download_location(download_location):
  print(download_location)
  if os.path.exists(download_location):
    print("I found an existing download folder. I will download new images to the existing folder.")
  else:
    try:
      os.makedirs(download_location)
    except: # except everything - if it doesn't work, request the user to do it manually
      raise_error("For some reason, I couldn't create a directory to store the downloaded images in. Please create a directory in this folder with this exact name:'{}' ".format(download_location)) 

def check_if_file_found():
  # get the right file
  csv_files = glob.glob('MIP_*dataset*.csv')
  if not csv_files:
    if glob.glob('MIP_painting_dataset_*.csv'): # this name is only found when a user downloads ALL paintings of this material
      return raise_error("If you want to download such a large selection of paintings, it is better to download them directly from https://data.4tu.nl/account/articles/13679200.")
    elif glob.glob('MIP_boxes_dataset_*.csv'): # this name is only found when a user downloads ALL boxes of this material
      return raise_error("If you want to download such a large selection of boxes, it is better to download them directly from https://data.4tu.nl/account/articles/13679200.")
    else:      
      return raise_error("No valid CSV files found. Please place a CSV downloaded from MIP in this folder.") 
  if len(csv_files) > 1:
    return raise_error("We found more then csv files from the MIP in this folder. Please place only one CSV file in this folder")
  else:
    return csv_files[0]


def raise_error(msg):
  # Raising exceptions on windows can automatically close the window. 
  # So, 'raise' them like so to make sure the user can actually see them. 
  print(msg)
  input("Press any key to close this window.")


if __name__ == "__main__":
  print(""" 
Hi! 
This script is supposed to automatically download images(paintings) from the Materials In Painting Dataset.
For this script to work, the CSV file downloaded from the MIP dataset needs be in the same folder as this script.

Written by Mitchell J.P. van Zuijlen. For any questions, you can contact me at m.j.p.vanzuijlen@tudelft.nl
""")

  csv_file = check_if_file_found() # get the csv file

  # the location from which to download the objects and the name to save them as
  urls, names, paintings_or_boxes, padding = read_file(csv_file)

  # create a directory and a folder for either paintings or bounding boxes
  target = 'MIP_images'
  make_download_location(target)
  folder = os.path.join(target, paintings_or_boxes)
  make_download_location(folder)
  if paintings_or_boxes == 'boxes':
    if padding:
      folder = os.path.join(folder, 'pbox')
      make_download_location(folder)
    else:
      folder = os.path.join(folder, 'bbox')
      make_download_location(folder)



  download_images(urls, names, folder)

  input("Press any key to close this window")