#!/usr/bin/env python

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

__author__ = 'holger.knust'


def convert_file(in_file, out_file):
    header_read = False
    sku_index = -1
    sku_dict = dict()

    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:
            assert len(row) == 2, "Rows must have at least 2 columns"

            if header_read:
                sku = row[sku_index]
                url = row[sku_index + 1]

                if sku in sku_dict:
                    sku_dict[sku].append(url)
                else:
                    sku_dict[sku] = [url]
            else:
                header_read = True

                if "sku" in row:
                    sku_index = row.index("sku")

                if sku_index < 0:
                    raise Exception("Could not find SKU")

        # max_urls = 0
        # for k in sku_dict.keys():
        #     max_urls = max(max_urls, len(sku_dict[k]))
        #
        # print("Maximum columns is {}".format(max_urls))
        #
        # header = ["{}{}".format(a_, b_) for a_, b_ in zip(["URL"] * max_urls, map(str, range(max_urls)))]
        # header.insert(0,"SKU")
        # writer.writerow(header)

        writer.writerow(['SKU','URLS'])
        for k in sku_dict.keys():
            writer.writerow([k] + sku_dict[k])


def process(data_root):
    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

    log_file_name = time.strftime("%Y%m%d-%H%M%S") + '.log'
    log_file = os.path.join(path_log, log_file_name)
    logging.basicConfig(filename=log_file, level=logging.DEBUG)

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

        logging.debug("Grouping 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)
                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_Gruppieren_in_Spalten")

    process(project_data_root)

    print("Done.")


if __name__ == "__main__":
    main()

