#Author: Spencer Healy 
import time
import mysql.connector
from mysql.connector import errorcode
import shutil
import os
import glob
import os.path

#where large images end up 
source_dir = "C:\python27"
#where you prefer them to be moved
dst = 'C:\python27\largeSize'

#Running totals of failed and successful image conversions
totalSuccessLarge=0
totalFailLarge=0
totalSuccessThumb=0
totalFailThumb=0

print("WELCOME TO SUPER MEGA AWESOME IMAGE CONVERTER!")

#Create list that will hold variable values from config file
configVariables = []
#Read file line by line into variables
filepath = 'imgConvert-Config.txt'  
f = open(filepath)
line = f.readline()
while line:
	#print(line)
	configVariables.append(line)
	# use realine() to read next line
	line = f.readline()
f.close()

#print(len(configVariables))
#print "List contents"
#for x in configVariables:
#	print(x)

print("Loading config file...")
pw="Iborg5618!@#$"
database=configVariables[0]
user=configVariables[1]
host=configVariables[2]
pathFull=configVariables[3]
#print("Path full =",pathFull)
pathSmall=configVariables[4]

time.sleep(5)

print("Connecting to DB...")

time.sleep(2)

try:
	cnx = mysql.connector.connect(user=user, password=pw, host=host, port=3306, database=database)

	print("executing queries...")

	#Query 1:images
	sql_select_Query = "select * from image"
	cursor = cnx.cursor()
	cursor.execute(sql_select_Query)
	records = cursor.fetchall()

	print("Total number of rows in image is - ", cursor.rowcount)
	print("Printing each row's column values i.e.  image record")
	for row in records:
		print("Id = ", row[0])
		print("Full Size image = ", row[1])
		print("Thumbnail image  = ", row[2])
		print("recordID  = ", row[3], "\n")

		#Name each image product.num-part.num.jpg
		#The image recordID is coorelated to the id in both product and part
		#Query 2: Product num
		# sql_select_Query2 = "select num from product where id = %s"
		imgId = (row[3],)#must be a tuple, so include the comma

		# cursor.execute(sql_select_Query2, imgId)
		# records2 = cursor.fetchall()
		# for row2 in records2:
		# 	#prodNum = row2[0]
		# 	#print ("Product number for this image = ", prodNum)
		# 	#finalImageName = pathFull + prodNum
		# 	#print ("Final image name (where large images are saved) = ", finalImageName)
		# 	#time.sleep(10)
		# 	print("Not concerned with product number...")

		#Query3 : Part num
		sql_select_Query3 = "select num from part where id = %s"
		cursor.execute(sql_select_Query3, imgId)
		records3 = cursor.fetchall()
		for row3 in records3:
			partNum = row3[0]
			print("Part number for this image = ", partNum)
			finalImageName = ''.join((partNum, ".png"))#converts the tuples to strings
			print("Combined part num with .png = ",finalImageName)
			finalImageName2 = pathSmall + finalImageName #the path where to save the thumbnail images
			print ("Final image name 2 (where small images are saved) = ", finalImageName2)
			#time.sleep(10)
			#finalImageName3 = pathFull + finalImageName

		#some images fail due to bad data in the db (specifically product num), so continue the process even if an image fails
		try:
			#Convert image string to its png form
			print("Converting full size image...")
			str = row[1]#full size image
			fh = open(finalImageName, "wb")
			fh.write(str.decode('base64'))
			fh.close()
			totalSuccessLarge+=1
		except:
			print("This large image failed...skipping and continuing process...")
			totalFailLarge+=1
			time.sleep(5)
			pass
		#Convert image string to its png form
		# print("Converting full size image...")
		# str = row[1]#full size image
		# fh = open(finalImageName, "wb")
		# fh.write(str.decode('base64'))
		# fh.close()
		try:
			print("Converting thumbnail size image...")
			str = row[2]#thumb image
			fh = open(finalImageName2, "wb")
			fh.write(str.decode('base64'))
			fh.close()
			totalSuccessThumb+=1
		except:
			print("This thumbnail image failed...skipping and continuing process...")
			totalFailThumb+=1
			time.sleep(5)
			pass

		# print("Converting thumbnail size image...")
		# str = row[2]#thumb image
		# fh = open(finalImageName2, "wb")
		# fh.write(str.decode('base64'))
		# fh.close()
		#cursor.close()

except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()

print("Moving large images into their folder...")
#move all large images into its folder
try:
	files = glob.iglob(os.path.join(source_dir, "*.png"))
	for file in files:
		if os.path.isfile(file):
			shutil.move(file, dst)
except:
	print("File already exists, skipping and continuing operations")
	pass

# files = glob.iglob(os.path.join(source_dir, "*.png"))
# for file in files:
# 	if os.path.isfile(file):
# 		shutil.move(file, dst)

#delete all png files in root folder (these are duplicates)
filelist = glob.glob(os.path.join(source_dir, "*.png"))
for f in filelist:
    os.remove(f)

print("PROCESS COMPLETE!")
print ("Large Image Success total = ", totalSuccessLarge)
print ("Large Image Failed Total = ", totalFailLarge)
print ("Thumb Image Success total = ", totalSuccessThumb)
print ("Thumb Image Failed Total = ", totalFailThumb)
