# -*- coding: utf-8 -*-
import os
import codecs
import difflib

from ppms.migration.packet import *
import startup


class CheckDifferencePacket(BasePacket):
    '''Compare the /customer/ folder to /planta_de/ and list differences

    This packet can be used to compare files that are placed in the /customer/
    folder to those in the /planta_de/ folder. This is useful if you've 
    modified planta files and now need to find which changes to merge.

    Simply recreate the /planta_de/ structure under /customer/ and when the same
    file is found on both sides it is compared and the results written to the
    logfile.
    '''
    wi_number = 10297
    version = 1
    category = PacketCategory.CATEGORY_HELPER

    def fix(self, runner):
        self.log('Comparing the customer and planta folders')
        self.log('Characters that can\'t be encoded will be replaced with "\uFFFD"')
        customizing_version = startup.customizing_version

        customer_folder = os.path.join('py','api','ppms','wrapper', 'customer')
        planta_folder = os.path.join('py','api','ppms', 'customizing', customizing_version)

        if not os.path.isdir(customer_folder):
            return self.skip('No folder at "%s"' % os.path.abspath(customer_folder))

        if not os.path.isdir(planta_folder):
            return self.skip('No folder at "%s"' % os.path.abspath(planta_folder))

        found_differences = 0
        for dirpath, dirnames, filenames in os.walk(planta_folder):
            if '__pycache__' in dirnames:
                dirnames.remove('__pycache__')

            for fname in filenames:
                planta_path = os.path.join(dirpath, fname)
                customer_path = planta_path.replace(planta_folder, customer_folder)

                if not os.path.isfile(customer_path):
                    self.log('"%s" does not exist - skipping (OK)' % customer_path)
                    continue

                text = 'Comparing "%s" to "%s": ' % (planta_path, customer_path)

                comparision = self.compare(planta_path, customer_path)
                if comparision:
                    self.log(text + 'NOK!')
                    self.log(comparision)
                    found_differences += 1
                else:
                    self.log(text + 'OK!')

        if found_differences:
            return self.fail('Found %i differences!' % found_differences)

        return self.success()

    def compare(self, sfile, tfile):
        fname_source = os.path.basename(sfile)
        fname_target = os.path.basename(tfile)

        with codecs.open(sfile, 'r', encoding='utf-8', errors='replace') as f:
            content_source = f.readlines()

        with codecs.open(tfile, 'r', encoding='utf-8', errors='replace') as f:
            content_target = f.readlines()

        diff = difflib.context_diff(content_source, content_target,
                                    fname_source, fname_target)

        return ''.join([line for line in diff])
