# -*- coding: utf-8 -*-

import hashlib
from uuid import getnode as get_mac

from django.conf import settings
from django.core.management.base import BaseCommand

from mtp.apps.sync.models import ClientVersion, Task


def check_hash(file_name, h):
    hash = ""
    try:
        f = open(file_name, 'rb')
        md5 = hashlib.md5()
        while True:
            data = f.read(8192)
            if not data:
                break
            md5.update(data)
        hash = md5.hexdigest()
        f.close()
    except:
        print("Error with file %s and hash %s" % (file_name, h))

    if hash == h:
        return True
    else:
        return False


class Command(BaseCommand):
    args = ''
    help = 'Check files integrity.'

    def handle(self, *args, **options):
        count_good = 0
        count_bad = 0
        for df in DownloadableFile.objects.all():
            if df.img.name == "":
                continue
            url = settings.MASTER_INSTANCE_URL + \
                settings.MEDIA_URL + df.img.name + "?" + "%d" % (get_mac())
            file_name = settings.MEDIA_ROOT + "/" + df.img.name
            if not check_hash(file_name, df.hash):
                count_bad += 1
                t = Task(path='curl -f -o %s %s' % (file_name, url),
                         finish_path='echo "%s  %s" | md5sum -c -' % (df.hash, file_name))
                t.save()
            else:
                count_good += 1

        for df in DocumentFile.objects.all():
            if df.orig_file.name == "":
                continue
            url = settings.MASTER_INSTANCE_URL + \
                settings.MEDIA_URL + df.orig_file.name + "?" + "%d" % (get_mac())
            file_name = settings.MEDIA_ROOT + "/" + df.orig_file.name
            if not check_hash(file_name, df.hash):
                count_bad += 1
                t = Task(path='curl -f -o %s %s' % (file_name, url),
                         finish_path='echo "%s  %s" | md5sum -c -' % (df.hash, file_name))
                t.save()
            else:
                count_good += 1

        for df in DocumentCommentFile.objects.all():
            if df.orig_file.name == "":
                continue
            url = settings.MASTER_INSTANCE_URL + \
                settings.MEDIA_URL + df.orig_file.name + "?" + "%d" % (get_mac())
            file_name = settings.MEDIA_ROOT + "/" + df.orig_file.name
            if not check_hash(file_name, df.hash):
                count_bad += 1
                t = Task(path='curl -f -o %s %s' % (file_name, url),
                         finish_path='echo "%s  %s" | md5sum -c -' % (df.hash, file_name))
                t.save()
            else:
                count_good += 1

        for df in ClientVersion.objects.all():
            if df.file.name == "":
                continue
            url = settings.MASTER_INSTANCE_URL + \
                settings.MEDIA_URL + df.file.name + "?" + "%d" % (get_mac())
            file_name = settings.MEDIA_ROOT + "/" + df.file.name
            if not check_hash(file_name, df.hash):
                count_bad += 1
                t = Task(path='curl -f -o %s %s' % (file_name, url),
                         finish_path='echo "%s  %s" | md5sum -c -' % (df.hash, file_name))
                t.save()
            else:
                count_good += 1

        print("good: %d" % count_good)
        print("bad: %d" % count_bad)
