#!/usr/bin/env python3.6

import os
import codecs
import csv
import logging
import logging.config
import shutil

import common

HDR_WEIGHT = "ITEM_WEIGHT"
HDR_EAN = "SELLER_SKU"

logger = None

def extract_weights(in_file, out_file):
    header_read = False
    weight_idx = -1
    ean_idx = -1
    eans = set()
    weight_net = None
    weight_gross = None

    with codecs.open(in_file, "r", encoding='utf-8') as f, \
            codecs.open(out_file, "w", encoding='utf-8') as of:

        rdr = csv.reader(f, delimiter='\t')
        wrt = csv.writer(of, delimiter=';')

        for row in rdr:
            if header_read:
                ean = row[ean_idx].strip().split("-")[0]
                if len(ean) > 13:
                    ean = ean[:13]

                if ean in eans:
                    logger.debug("Duplicate EAN %s found", ean)
                    continue

                weight_kg = row[weight_idx]
                if not weight_kg:
                    continue

                try:
                    weight_net = int(float(weight_kg) * 1000.0)
                    weight_gross = round(weight_net + 50)
                except ValueError as ve:
                    logger.error(ve)
                    continue

                eans.add(ean)

                wrt.writerow([ean, weight_net, weight_gross])
            else:
                if not HDR_EAN in row:
                    raise Exception(
                        "Column '{}' not found in file '{}'".format(
                            HDR_EAN, in_file))

                if not HDR_WEIGHT in row:
                    raise Exception(
                        "Column '{}' not found in file '{}'".format(
                            HDR_WEIGHT, in_file))

                weight_idx = row.index(HDR_WEIGHT)
                ean_idx = row.index(HDR_EAN)

                wrt.writerow(["EAN","Gewicht_netto", "Gewicht_brutto"])

                header_read = True


def process_files(data_root):
    global logger

    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")

    log_file = os.path.join(path_log, os.path.basename(__file__) + ".log")
    logging.config.dictConfig(common.get_logging_config(log_file))

    logger = logging.getLogger(__file__)

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

    logger.info("Found {0} file(s)...".format(num_files))

    if num_files == 0:
        return

    for f in sorted(files):
        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)

        if os.path.isfile(in_file):
            logger.info("Matching Plenty IDs %s...", in_file)

            try:
                extract_weights(in_file, out_file)
                shutil.move(in_file, prc_file)
            except Exception as e:
                logger.error(e)
                shutil.move(in_file, err_file)

    logger.info("Finished")


def main():
    if os.path.exists(r"/ftproot/"):
        data_root = r"/ftproot/"
    elif os.path.exists(r"/home/holger/git/bitbucket/thelen/data/"):
        data_root = r"/home/holger/git/bitbucket/thelen/data/"
    else:
        print(
            "Unknown environment. Please set data_root to data folder location. Aborting."
        )
        return

    script_data_root = os.path.join(data_root, "Matching_Gewichte")
    process_files(script_data_root)


if __name__ == "__main__":
    main()
