#!/usr/bin/env python

__author__ = 'holger.knust'

import codecs
import csv
import logging
import os
import shutil

FILE_REF_FEDAS = 'Overview_GERMAN.csv'
COL_HEADER_FEDAS = 'FEDAS'

def read_fedas_lookup(ref_path):
    ref_dict = {}

    header_row = []
    header_read = False
    fedas_idx = -1
    full_ref_path = os.path.join(ref_path, FILE_REF_FEDAS)

    with codecs.open(full_ref_path, "rb", encoding='cp1252') as f:
        reader = csv.reader(f, delimiter=';')

        for row in reader:
            if header_read and fedas_idx < 0:
                raise Exception("FEDAS column NOT found.")
            print(row) # = header_row.index(COL_HEADER_FEDAS)

            if header_read:
                ean = row.pop(fedas_idx)
                ref_dict[ean] = row
            else:
                header_row = row
                if "FEDAS" in row:
                   fedas_idx = header_row.index(COL_HEADER_FEDAS)
                   print(fedas_idx) # = header_row.index(COL_HEADER_FEDAS)
                header_row.pop(fedas_idx)
                header_read = True

    return ref_dict, header_row

def convert_fedas(in_file, out_file, ref_dict, header_row):
    header_read = False
    fedas_col_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=';')

        for row in reader:
            if any(row):
                if header_read:
                    item_id = "ERROR"
                    row2 = []
                    fedas_num = row[fedas_col_index]
                    if fedas_num in ref_dict:
                        row2 = ref_dict[fedas_num]
                        if len(row2) > 1:
                            start_pos = len(row) + 1
                            for col in row2:
                                row.insert(start_pos, col)
                                start_pos = start_pos + 1
                else:
                    header_read = True
                    fedas_col_index = row.index(COL_HEADER_FEDAS)
                    start_pos = len(row) + 1
                    for col in header_row:
                        row.insert(start_pos, col)
                        start_pos = start_pos + 1

                writer.writerow(row)


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_row = read_fedas_lookup(path_ref)

    for f in files:
        log_file_name = os.path.splitext(f)[0] + '.log'

        in_file = os.path.join(path_in, f)
        out_file = os.path.join(path_out, f)
        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)

        if os.path.isfile(in_file):
            logging.basicConfig(filename=log_file, level=logging.DEBUG)

            logging.debug("Performing FEDAS Lookup...".format(in_file))

            try:
                convert_fedas(in_file, out_file, ref_dict, header_row)
                shutil.move(in_file, proc_file)
            except Exception as e:
                print(e)
                logging.error(e)
                shutil.move(in_file, err_file)


def main():
    data_root = r"/ftproot/"
    #data_root = r"/data/projects/pycharm/thelen/conversion/data/"
    #data_root = r"c:/temp/thelen/"

    fedas_data_root = os.path.join(data_root, "Matching_Fedas")

    process(fedas_data_root)

    print("Done.")

if __name__ == "__main__":
    main() 
