#!/usr/bin/env python
import os
import shutil
import codecs
import csv
import logging


def read_ref(ref_path):
    ref_dict = {}

    header_read = False
    header_names = ["ItemFree1", "ItemFree3", "ItemFree10", \
        "ItemFree19", "ItemFree20", "RecommendedRetailPrice", "VariationString"]
    ean_index = -1
    item1_index = -1
    item3_index = -1
    item10_index = -1
    item19_index = -1
    item20_index = -1
    price_index = -1
    itemno_index = -1
    varstr_index = -1
    full_ref_path = os.path.join(ref_path, "Artikel.csv")
    print("Reading ref...")
    with codecs.open(full_ref_path, "rb", encoding='utf-8') as f:

        reader = csv.reader(f, delimiter=';')
        ean_index = -1
        for row in reader:
            if header_read:
                ean = row[ean_index].strip()
                item1 = row[item1_index]
                item3 = row[item3_index]
                item10 = row[item10_index]
                item19 = row[item19_index]
                item20 = row[item20_index]
                price = row[price_index]
                varstr = row[varstr_index]

                if item1:
                    if not item1.strip():
                        item1 = row[itemno_index]
                else:
                    item1 = row[itemno_index]

                if not item3:
                    item3 = "999999"

                if price:
                    price = price.replace(".", ",")

                ref_dict[ean] = [item1, item3, item10, item19, item20, price, varstr]
            else:
                lc_row = [x.lower() for x in row]
                if not "ean" in lc_row:
                    raise Exception("EAN column not found")

                ean_index = lc_row.index("ean")
                item1_index = lc_row.index("itemfree1")
                item3_index = lc_row.index("itemfree3")
                item10_index = lc_row.index("itemfree10")
                item19_index = lc_row.index("itemfree19")
                item20_index = lc_row.index("itemfree20")
                price_index = lc_row.index("recommendedretailprice")
                itemno_index = lc_row.index("itemno")
                varstr_index = lc_row.index("variationstring")

                if ean_index < 0 or item1_index < 0 or item3_index < 0 \
                    or item19_index < 0 or price_index < 0 or itemno_index < 0 \
                    or item10_index < 0 or item20_index < 0 or varstr_index < 0:
                    raise Exception("Missing necessary columns in Ref")

                header_read = True

    return ref_dict, header_names


def merge(in_file, out_file, ean_dict, ean_header):
    header_read = False
    ean_index = -1

    print("Starting merge...")
    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_MINIMAL)

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

                if not ean.strip():
                    continue

                if ean in ean_dict:
                    writer.writerow(row + ean_dict[ean])
                else:
                    logging.warning("EAN %s not found.", ean)
            else:

                lc_row = [x.lower() for x in row]
                if "ean" not in lc_row:
                    raise Exception("EAN column column not found")

                ean_index = lc_row.index('ean')
                writer.writerow(row + ean_header)
                header_read = True



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

    ean_dict, ean_hdr = read_ref(path_ref)

    f = "REALSTOCK_6658400_427.csv"

    log_filename = os.path.splitext(f)[0] + '.log'
    ftp_file = os.path.join(ftp_root, f)
    in_file = os.path.join(path_in, f)
    out_file = os.path.join(path_out, f)
    prc_file = os.path.join(path_prc, f)
    err_file = os.path.join(path_err, f)
    log_file = os.path.join(path_log, log_filename)

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

        logging.debug("Checking %s for duplicates.", in_file)
        try:
            shutil.copy(ftp_file, in_file)
            merge(in_file, out_file, ean_dict, ean_hdr)
            shutil.move(in_file, prc_file)
        except Exception as e:
            logging.error(e)
            shutil.move(in_file, err_file)


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

    ftp_root = os.path.join(data_root, "SSW_Importe", "Inventory_09_12")
    data_root = os.path.join(data_root, "Matching_Kasse")
    process_files(data_root, ftp_root)
    print("Done.")

if __name__ == "__main__":
    main()

