#!/usr/bin/env python

import codecs
import csv
import locale
import logging
import os
import shutil
import time

__author__ = 'holger.knust'


def read_ref(ref_path):
    ref_dict = dict()

    order_number_index = -1
    model_name_index = -1
    print_index = -1
    ean_index = -1
    purchase_price_index = -1
    retail_price_index = -1
    cat1_index = -1
    cat2_index = -1
    cat3_index = -1

    header_read = False

    with codecs.open(ref_path, "rb", encoding='utf-8') as f:

        reader = csv.reader(f, delimiter=';')
        item_idx = -1
        url_idx = -1
        for row in reader:
            # print(row)
            if header_read:
                ean = row[ean_index]

                purchase_price = row[purchase_price_index].replace(',', '.')

                try:
                    purchase_price_num = locale.atof(purchase_price)
                except Exception as ex:
                    logging.error(ex)
                    purchase_price_num = 0.0

                if ean in ref_dict:
                    existing = ref_dict[ean]

                    if existing:
                        if purchase_price_num > existing[3]:
                            # Replace existing row with new one if purchase price is higher.

                            ref_dict[ean] = [row[order_number_index], row[model_name_index], row[print_index],
                                             row[purchase_price_index], row[retail_price_index], row[cat1_index],
                                             row[cat2_index], row[cat3_index]]
                else:
                    ref_dict[ean] = [row[order_number_index], row[model_name_index], row[print_index],
                                     row[purchase_price_index], row[retail_price_index], row[cat1_index],
                                     row[cat2_index], row[cat3_index]]

            else:
                if 'ns1:ordernumber' in row:
                    order_number_index = row.index('ns1:ordernumber')
                if 'ns1:modelname' in row:
                        model_name_index = row.index('ns1:modelname')
                if 'ns1:print' in row:
                    print_index = row.index('ns1:print')
                if 'ns1:ean' in row:
                    ean_index = row.index('ns1:ean')
                if 'ns1:EK' in row:
                    purchase_price_index = row.index('ns1:EK')
                if 'ns1:VK' in row:
                    retail_price_index = row.index('ns1:VK')
                if 'ns1:kat1' in row:
                    cat1_index = row.index('ns1:kat1')
                if 'ns1:kat2' in row:
                    cat2_index = row.index('ns1:kat2')
                if 'ns1:kat3' in row:
                    cat3_index = row.index('ns1:kat3')

                if ean_index < 0:
                    raise('Missing EAN')

                header_read = True

    return ref_dict


def read_refs(ref_path):
    refs_dict = {}


    header_names = ['ns1:ordernumber', 'ns1:modelname', 'ns1:print', 'ns1:EK', 'ns1:VK', 'ns1:kat1',
                    'ns1:kat2', 'ns1:kat3']

    for path, subdirs, files in os.walk(ref_path):
        for filename in files:
            f = os.path.join(path, filename)
            file_refs = read_ref(f)
            refs_dict.update(file_refs)

    return refs_dict, header_names

def convert_file(in_file, out_file, ref_dict, header_names):
    header_read = False
    ean_index = -1

    with codecs.open(in_file, "r", encoding ='utf-8') as f, codecs.open(out_file, "w", encoding='utf-8') as of:
        reader = csv.reader(f, delimiter=';')
        writer = csv.writer(of, delimiter=';', quoting=csv.QUOTE_NONNUMERIC)

        for row in reader:
            if header_read:
                ean = row[ean_index]

                if ean in ref_dict:
                    writer.writerow(row + ref_dict[ean])
                else:
                    row.append("ERROR")
                    writer.writerow(row)
            else:
                header_read = True

                if "ean" in row:
                    ean_index = row.index("ean")

                if ean_index < 0:
                    raise Exception("Could not find EAN")

                writer.writerow(row + header_names)

def process(data_root):
    path_ref = os.path.join(data_root, "ref")
    path_in = os.path.join(data_root, "in")
    path_out = os.path.join(data_root, "out")
    path_proc = os.path.join(data_root, "processed")
    path_err = os.path.join(data_root, "error")
    path_log = os.path.join(data_root, "log")

    files = os.listdir(path_in)
    num_files = len(files)

    print("Found {0} files...".format(num_files))

    if num_files == 0:
        return

    ref_dict, header_names = read_refs(path_ref)

    for f in files:
        log_file_name = time.strftime("%Y%m%d-%H%M%S") + '.log'

        in_file = os.path.join(path_in, f)
        out_file_name = (os.path.splitext(f)[0] + '.csv').upper()

        out_file = os.path. join(path_out, out_file_name)
        proc_file = os.path.join(path_proc, f)
        err_file = os.path.join(path_err, f)
        log_file = os.path.join(path_log, log_file_name)

        logging.basicConfig(filename=log_file, level=logging.DEBUG)

        logging.debug("Matching item ids for {0}...".format(in_file))

        if os.path.isfile(in_file):
            try:
                print("Converting {0} to {1}...".format(in_file, out_file))
                convert_file(in_file, out_file, ref_dict, header_names)
                shutil.move(in_file, proc_file)
            except Exception as e:
                logging.error(e)
                shutil.move(in_file, err_file)
                os.remove(out_file)


def main():
    # data_root = r"/home/holger/git/thelen/data/"
    data_root = r"/ftproot/"
    #data_root = r"c:/temp/thelen/"

    project_data_root = os.path.join(data_root, "Matching_Missing")

    process(project_data_root)

    print("Done.")


if __name__ == "__main__":
    main()

