import os, shutil, win32api, pathlib, pymysql, timeit, ListFolders_functions, sys, hashlib

from pathlib import Path
from datetime import datetime
from ListFolders_functions import *

conn = pymysql.connect(host='192.168.1.76', user="root", password="Vlado9674+", database="Python", port=3307)
cur1 = conn.cursor()
set_sql_mode_no_escape(cur1)

# FolderToScan = r'\\192.168.1.121\public\winsusoft'
# FolderToScan = r'\\192.168.1.50\#Fotky'
FolderToScan = r'\\192.168.1.50\#ebooks'
# FolderToScan = r'd:\\onedrive'
# FolderToScan = r'\\192.168.1.121\public\puzzle'
ConditionFolderToScan=FolderToScan+"%"


print("Start cteni adresaru")
# update testing na 1
cur1.execute("update tFilesRepository set btesting=1 where cfilename like %s",ConditionFolderToScan)
conn.commit()

for root, dirs, files in os.walk(FolderToScan):
    # print(root)
    for file in files:
        print(file)
        fullfilename=os.path.join(root,file)
        # print(fullfilename)
        filesize=os.stat(fullfilename).st_size
        print(filesize)
        filectime=datetime.fromtimestamp(os.stat(fullfilename).st_ctime)
        filemtime=datetime.fromtimestamp(os.stat(fullfilename).st_mtime)
        # print(filemtime)
        # vloz.append((fullfilename,filesize,filectime,filemtime))

        cur1.execute("select id,cfilename,isize,mtime,ctime from tFilesRepository where cfilename=%s",(fullfilename))
        record=cur1.fetchone()
        if cur1.rowcount==0:
            entrycreated = datetime.now()
            cur1.execute("insert into tFilesRepository (cfilename,isize,mtime,ctime,entrycreated) values (%s,%s,%s,%s,%s)",(fullfilename,filesize,filemtime,filectime,entrycreated))
            conn.commit()
        else:
            # check if update needed
            # print(cur1.rowcount)
            # print(record[0])
            # print(record[1])
            # print(fullfilename)
            # print(record[2])
            # print(filesize)
            # print(record[3])
            # print(filemtime)
            # print(record[4])
            # print(filectime)
            if record[2]==filesize and record[3]==filemtime and record[4]==filectime:
                cur1.execute("update tFilesRepository set btesting=0 where id=%s",record[0])
                conn.commit()
            else:
                print("Zde zacina ELSE")
                entrycreated = datetime.now()
                # print(entrycreated)
                # print(filesize)
                # print(record[0])
                cur1.execute("update tFilesRepository set cfilename=%s,isize=%s,mtime=%s,ctime=%s,entrycreated=%s,btesting=0"
                             " where id=%s",(fullfilename, filesize, filemtime, filectime, entrycreated,record[0]))
                conn.commit()


cur1.execute("delete from tFilesRepository where cfilename like %s and btesting=1 ",ConditionFolderToScan)
conn.commit()

#update MD5
cur1.execute("select id, cfilename, isize from tFilesRepository where md5 is NULL")

FilesToHash=cur1.fetchall()
print(FilesToHash)
for Soubor in FilesToHash:
    with open(Soubor[1], "rb") as f:
        print(Soubor[1])
        file_hash = hashlib.md5()
        while chunk := f.read(8192):
            file_hash.update(chunk)
        print(file_hash.hexdigest())
        md5calculated = datetime.now()
        cur1.execute("update tFilesRepository set md5=%s, md5calculated=%s where id=%s",(file_hash.hexdigest(),md5calculated,Soubor[0]))
        conn.commit()