import os
import xxhash

class FileHash:
    def __init__(self,files,*args,**kwargs):
        super(FileHash,self).__init__(*args,**kwargs)
        self.files              = files
        self.chunk_size         = kwargs['chunk_size']

    @staticmethod
    def xx_hash(self,f,progress_callback):
        size                    = os.path.getsize(f)
        hasher                  = xxhash.xxh64()

        #todo: add callbacks
        chunk_count             = 0
        with open(f, 'rb') as f:
            for chunk in iter(lambda: f.read(self.chunk_size),b""):
                hasher.update(chunk)
                chunk_count     += 1
                hashed_size     = chunk_count * self.chunk_size
                progress_callback.emit(round((hashed_size / size) * 100, 1))

        file_hash               = hasher.hexdigest()
        return file_hash

    @staticmethod
    def t_verify_checksum(self,
                          progress_callback,
                          import_progress_callback,
                          current_file_progress_callback,
                          imported_file_count_callback,
                          found_file,
                          total_file_count):

        for file in self.files:
            i                                           = 0
            c                                           = {}
            for checksum in self.files[file]['xx_checksum']:
                c[i]                                    = self.files[file]['xx_checksum'][checksum]
                if i > 0:
                    p                                   = i - 1
                    if c[i] == c[p]:
                        self.files[file]['checksum_match']   = True
                    else:
                        self.files[file]['checksum_match']   = False
                i                                       += 1
        return self.files