""" Script to process Matching_Vendor_Artikelanlage. """
#!/usr/bin/env python3.7

import codecs
import csv
from datetime import datetime
import logging
import os
import shutil
import re
import time
import xlsxwriter

from common import get_data_root, setup_logging

REF_COL_ITEM_NO = 'ItemNo'
REF_COL_EAN = 'EAN'
REF_COL_ITEM_FREE1 = 'ItemFree1'
REF_COL_ITEM_FREE2 = 'ItemFree2'
REF_COL_ITEM_FREE4 = 'ItemFree4'
REF_COL_ITEM_FREE15 = 'ItemFree15'
REF_COL_ITEM_FREE18 = 'ItemFree18'
REF_COL_ITEM_FREE19 = 'ItemFree19'
REF_COL_ITEM_FREE20 = 'ItemFree20'
REF_COL_ITEM_PRODUCER = 'ItemProducer'
REF_COL_VAR_STRING = 'VariationString'
REF_COL_IMG_URL = 'ItemImageURL'
REF_COL_FEDAS = 'Fedas'
REF_COL_TEXT = "ItemTextDescription"
REF_COL_TEXTILE_TEXT = "ItemTextTechnicalData"
REF_COL_LABEL = "Etikett"
REF_COL_PRODUCT_TYPE = "Product-Type"

DATA_FOLDER_NAME = "Matching_Vendor_Artikelanlage"

# pylint: disable=invalid-name

remove_tag_regex = re.compile('<.*?>')


def remove_html_tags(html_text: str) -> str:
    """ Strip HTML tags from string """
    return re.sub(r'\s+', ' ', re.sub(remove_tag_regex, ' ',
                                      html_text)).strip()


def read_ref_combined(ref_path):
    """ Read combined reference file.

        Expects combined.csv file to exist in ref folder
    """
    ref_dict = {}

    header_read = False
    ean_idx = -1

    full_ref_path = os.path.join(ref_path, "combined.csv")

    with codecs.open(full_ref_path, "r", encoding='utf-8',
                     errors='ignore') as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            print(row)
            if header_read:
                ean = row[ean_idx]
                row.pop(ean_idx)

                ref_dict[ean] = row
            else:
                if "Externe_Produkt_ID" in row:
                    ean_idx = row.index("Externe_Produkt_ID")
                if ean_idx < 0:
                    raise Exception(
                        "Externe_Produkt_ID not found in combined.csv")

                row.pop(ean_idx)
                header_row = row
                header_read = True

    return ref_dict, header_row


def read_ref_colors(ref_path):
    """ Read color reference file.

        Expects Ref_Farben.csv file to exist in ref folder
    """
    ref_dict = {}

    header_read = False

    full_ref_path = os.path.join(ref_path, "Ref_Farben.csv")

    with codecs.open(full_ref_path, "r", encoding='utf-8',
                     errors='ignore') as f:
        reader = csv.reader(f, delimiter=';')
        for row in reader:
            # print(row)
            if header_read:
                ref_dict[row[0]] = row[1]
            else:
                header_read = True

    return ref_dict


def read_ref_quantities(ref_path):
    """ Read the order quantities ref.

        Expects Ref_Temp_Bestellmenge.csv file to exist in ref folder
    """
    ref_dict = {}

    header_read = False

    full_ref_path = os.path.join(ref_path, "Ref_Temp_Bestellmenge.csv")

    with codecs.open(full_ref_path, "r", encoding='utf-8',
                     errors='ignore') as f:
        reader = csv.reader(f, delimiter=';')
        for row in reader:
            # print(row)
            if header_read:
                ref_dict[row[0]] = row[1]
            else:
                header_read = True

    return ref_dict


def read_ref_pricing(ref_path):
    """ Read pricing reference file.

        Expects Ref_Temp_Preise_individual.csv file to exist in ref folder
    """
    ref_dict = {}
    price1_idx = -1
    price2_idx = -1
    price3_idx = -1
    price4_idx = -1

    header_read = False

    full_ref_path = os.path.join(ref_path, "Ref_Temp_Preise_individual.csv")

    with codecs.open(full_ref_path, "r", encoding='utf-8',
                     errors='ignore') as f:
        reader = csv.reader(f, delimiter=';')
        for row in reader:
            # print(row)
            if header_read:
                try:
                    # Parse the amount
                    row[price1_idx] = float(row[price1_idx].replace(",", "."))
                    row[price2_idx] = float(row[price2_idx].replace(",", "."))
                    row[price3_idx] = float(row[price3_idx].replace(",", "."))
                    row[price4_idx] = float(row[price4_idx].replace(",", "."))
                except ValueError as ex:
                    logging.warning(
                        "Price conversion from Ref_Temp_Preise_individual.csv failed for %s ",
                        row[0])
                    logging.error(ex)

                ref_dict[row[0]] = row[1:]
            else:
                header_read = True

                if "EK_2" in row:
                    price1_idx = row.index("EK_2")

                if "VK_2" in row:
                    price2_idx = row.index("VK_2")

                if "Preis_Nachorder_final" in row:
                    price3_idx = row.index("Preis_Nachorder_final")

                if "Preis_Vororder_final" in row:
                    price4_idx = row.index("Preis_Vororder_final")

                if price1_idx < 0 or price2_idx < 0 or price3_idx < 0 or price4_idx < 0:
                    raise Exception(
                        "Individual Price file is missing a column")

    return ref_dict


def read_ref_delivery_dates(ref_path):
    """ Read delivery dates reference file.

        Expects Ref_Temp_Lieferdatum.csv file to exist in ref folder
    """
    ref_dict = {}

    header_read = False

    full_ref_path = os.path.join(ref_path, "Ref_Temp_Bestellmenge.csv")

    with codecs.open(full_ref_path, "r", encoding='utf-8',
                     errors='ignore') as f:
        reader = csv.reader(f, delimiter=';')
        for row in reader:
            # print(row)
            if header_read:
                ref_dict[row[0]] = row[1]
            else:
                header_read = True

    return ref_dict


def read_ref_manufacturers(ref_path):
    """ Read manufacturer reference file.
        Expects Ref_Vendor2_Hersteller.csv file to exist in ref folder
    """
    ref_dict = {}

    header_read = False

    full_ref_path = os.path.join(ref_path, "Ref_Vendor2_Hersteller.csv")

    with codecs.open(full_ref_path, "r", encoding='utf-8',
                     errors='ignore') as f:
        reader = csv.reader(f, delimiter=';')
        for row in reader:
            # print(row)
            if header_read:
                ref_dict[row[0]] = row[1]
            else:
                header_read = True

    return ref_dict


def read_ref_sizes(ref_path):
    """ Read size reference.

        Expects Referenz_Groessen.csv file to exist in ref folder
    """
    ref_dict = {}
    header_row = []
    fedas_idx = -1
    prod_idx = -1
    tag_idx = -1
    gender_idx = -1
    override_idx = -1

    header_read = False

    full_ref_path = os.path.join(ref_path, "Ref_Groessen.csv")

    with codecs.open(full_ref_path, "r", encoding='utf-8',
                     errors='ignore') as f:
        reader = csv.reader(f, delimiter=';')
        for row in reader:
            if header_read:
                if override_idx > -1:
                    # Remove the override value from the row
                    gender_override = row.pop(override_idx)

                combo_key = "-".join([
                    row[fedas_idx], row[prod_idx], row[gender_idx],
                    row[tag_idx].replace(",", ".")
                ])
                combo_key2 = "-".join([
                    row[fedas_idx], row[prod_idx], gender_override,
                    row[tag_idx].replace(",", ".")
                ])
                if combo_key in ref_dict:
                    logging.warning("Duplicate key found : %s", combo_key)
                else:
                    ref_dict[combo_key] = row[gender_idx + 1:]

                if combo_key2 in ref_dict:
                    logging.warning("Duplicate correction key found : %s",
                                    combo_key)
                else:
                    ref_dict[combo_key2] = row[gender_idx + 1:]

            else:
                # Fedas;Hersteller;Gender;Product-Type;Etikett;UK;US;EU;CM;Brust;Taille;Hüfte
                if "Fedas" not in row:
                    raise Exception('Fedas not found')

                if "Hersteller" not in row:
                    raise Exception('Hersteller not found')

                if "Gender" not in row:
                    raise Exception('Gender not found')

                if "Etikett" not in row:
                    raise Exception('Etikett not found')

                if "Gender_Korrektur" in row:
                    override_idx = row.index("Gender_Korrektur")
                    row.pop(override_idx)

                fedas_idx = row.index("Fedas")
                prod_idx = row.index("Hersteller")
                gender_idx = row.index("Gender")
                tag_idx = row.index("Etikett")

                header_row = row[gender_idx + 1:]
                header_read = True
    return ref_dict, header_row


def read_ref_items(ref_path, ref_sizes, hdr_sizes):
    """ Read item reference.

        Expects Referenz_Matching_Vendor_2.csv file to exist in ref folder
    """
    ref_dict = {}
    header_row = []
    item_number_idx = -1
    size_idx = -1
    fedas_idx = -1
    prod_idx = -1
    gender_idx = -1
    gender_override_idx = -1
    size = ""

    header_read = False

    full_ref_path = os.path.join(ref_path, "Referenz_Matching_Vendor_2.csv")

    with codecs.open(full_ref_path, "r", encoding='utf-8',
                     errors='ignore') as f:
        reader = csv.reader(f, delimiter=';')
        for row in reader:
            if not row:
                continue

            if header_read:
                idx = row[item_number_idx]

                var_string = row[size_idx]
                if var_string:
                    var_string_list = var_string.split(':')

                    if len(var_string_list) > 1:
                        size = var_string_list[1]
                    else:
                        size = ""

                fedas = row[fedas_idx]
                if len(fedas) > 1:
                    fedas_key = fedas[0]
                else:
                    fedas_key = ""

                combo_key = "-".join(
                    [fedas_key, row[prod_idx], row[gender_idx], size])
                if combo_key in ref_sizes:
                    size_cols = ref_sizes[combo_key]
                else:
                    # For investigation
                    # if idx=="ADI033905_000":
                    #     print(combo_key)
                    size_cols = ["ERROR"] * (len(hdr_sizes) -
                                             1 if gender_override_idx > -1 else
                                             len(hdr_sizes))

                if idx in ref_dict:
                    curr_list = ref_dict[idx]
                    curr_list.append(row + size_cols)
                    ref_dict[idx] = curr_list
                else:
                    ref_dict[idx] = [row + size_cols]
            else:
                if REF_COL_ITEM_NO in row:
                    item_number_idx = row.index(REF_COL_ITEM_NO)
                else:
                    raise Exception('ItemNo not found')

                if "ItemFree4" not in row:
                    raise Exception('ItemFree4 not found')

                if "Asin" not in row:
                    raise Exception('Asin not found')

                if "EAN" not in row:
                    raise Exception('EAN not found')

                # ItemProducerID;Fedas;Gender
                if "ItemProducerID" not in row:
                    raise Exception("ItemProducerID not found")

                if "Fedas" not in row:
                    raise Exception("Fedas not found")

                if "Gender" not in row:
                    raise Exception("Gender not found")

                prod_idx = row.index("ItemProducerID")
                fedas_idx = row.index("Fedas")
                gender_idx = row.index("Gender")
                size_idx = row.index(REF_COL_VAR_STRING)

                header_row = row + hdr_sizes
                header_read = True

    return ref_dict, header_row


def create_item_file(file_in, file_out, file_out2, file_templ, ref_dict,
                     ref_header, ref_colors, ref_manufacturers, ref_quants,
                     ref_delivery_dates, ref_pricing, ref_combined,
                     sizes_header, combined_header):
    """ Create new item file. """
    workbook = None
    header_read = False
    item_no_index = -1
    num_template_columns = 0
    template_columns = None
    current_row = 0
    current_row2 = 0
    item_free2_list = None
    current_nomatch_row = 0

    try:
        workbook = xlsxwriter.Workbook(file_out)
        workbook2 = xlsxwriter.Workbook(file_out2)
        worksheet = workbook.add_worksheet("Summary")
        worksheet2 = workbook2.add_worksheet("Summary")
        no_match_ws = workbook.add_worksheet("Not Matched")

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

            # Let's copy the template header first
            with codecs.open(file_templ, "r", encoding='cp1252') as tf:
                temp_reader = csv.reader(tf, delimiter=';')
                for row in temp_reader:
                    num_template_columns = len(row)
                    template_columns = row

                    worksheet.write_row(current_row, 0, row + combined_header)
                    worksheet2.write_row(current_row2, 0,
                                         row + combined_header)
                    current_row += 1
                    current_row2 += 1

            if "VendorCode" in template_columns:
                vendor_code_idx = template_columns.index("VendorCode")
            else:
                vendor_code_idx = 0

            if "Lieferanten-Artikelnummer" in template_columns:
                supplier_item_id_idx = template_columns.index(
                    "Lieferanten-Artikelnummer")
            else:
                supplier_item_id_idx = 1

            if "Hersteller-Artikelnummer" in template_columns:
                manufacturer_item_id_idx = template_columns.index(
                    "Hersteller-Artikelnummer")
            else:
                manufacturer_item_id_idx = 2

            if "Farbnummer" in template_columns:
                manufacturer_color_no_idx = template_columns.index(
                    "Farbnummer")
            else:
                manufacturer_color_no_idx = 3

            if "Externer ID Type" in template_columns:
                ext_id_type_idx = template_columns.index("Externer ID Type")
            else:
                ext_id_type_idx = 4

            if "EAN/UPC Code" in template_columns:
                ean_idx = template_columns.index("EAN/UPC Code")
            else:
                ean_idx = 5

            if "Ebene1-Sportart" in template_columns:
                level1_idx = template_columns.index("Ebene1-Sportart")
            else:
                level1_idx = 6

            if "Ebene2-Produktgruppe" in template_columns:
                level2_idx = template_columns.index("Ebene2-Produktgruppe")
            else:
                level2_idx = 7

            if "Ebene3-Subproduktgruppe" in template_columns:
                level3_idx = template_columns.index("Ebene3-Subproduktgruppe")
            else:
                level3_idx = 8

            if "Markenname" in template_columns:
                brand_name_idx = template_columns.index("Markenname")
            else:
                brand_name_idx = 9

            if "Artikelart" in template_columns:
                article_type_idx = template_columns.index("Artikelart")
            else:
                article_type_idx = 10

            if "Artikelbezeichnung / Modellname" in template_columns:
                article_name_idx = template_columns.index(
                    "Artikelbezeichnung / Modellname")
            else:
                article_name_idx = 11

            if "Farbbezeichnung" in template_columns:
                color_name_idx = template_columns.index("Farbbezeichnung")
            else:
                color_name_idx = 12

            std_color_idx = 13  # Column has a CR in the header
            target_group_idx = 14
            size_idx = 15
            std_size_idx = 16

            if "Keywords" in template_columns:
                keywords_idx = template_columns.index("Keywords")
            else:
                keywords_idx = 17

            if "Saison" in template_columns:
                season_idx = template_columns.index("Saison")
            else:
                season_idx = 18

            if "Modelljahr" in template_columns:
                model_year_idx = template_columns.index("Modelljahr")
            else:
                model_year_idx = 19

            available_idx = 20

            qty_first_delv_idx = 21
            dt_first_delv_idx = 22
            amaz_purch_price = 23
            purch_price_valid_from_idx = 24
            amaz_retail_price_idx = 25
            materials_idx = 26

            if "Einkaufspreis" in template_columns:
                purch_price_idx = template_columns.index("Einkaufspreis")
            else:
                purch_price_idx = 27

            if "Text" in template_columns:
                text_idx = template_columns.index("Text")
            else:
                text_idx = 28

            ext_id2_type_idx = 29

            if "Asin" in template_columns:
                asin_idx = template_columns.index("Asin")
            else:
                asin_idx = 30

            if "Bullet1" in template_columns:
                bullet1_idx = template_columns.index("Bullet1")
            else:
                bullet1_idx = 31

            if "Bullet2" in template_columns:
                bullet2_idx = template_columns.index("Bullet2")
            else:
                bullet2_idx = 32

            if "Bullet3" in template_columns:
                bullet3_idx = template_columns.index("Bullet3")
            else:
                bullet3_idx = 33

            if "Textilkennzeichnung" in template_columns:
                textile_id_idx = template_columns.index("Textilkennzeichnung")
            else:
                textile_id_idx = 34

            delivery_date_idx = 69

            pricing_idx = 36

            if "Gender" in template_columns:
                gender_out_idx = template_columns.index("Gender")
                gender_in_idx = ref_header.index("Gender")

            # Bildpfad
            if "Bildpfad" in template_columns:
                image_url_idx = template_columns.index("Bildpfad")
            else:
                image_url_idx = 68

            if "Product-Type" in template_columns:
                product_type_idx = template_columns.index("Product-Type")

            already_processed = set()
            line_num = 1

            # Let's read the ItemNos from the input file and grab the ref file content
            for row in reader:
                if header_read:
                    item_no = row[item_no_index]

                    if item_no in already_processed:
                        logging.debug("Line %d : %s was already processed.",
                                        line_num, item_no)
                        continue
                    else:
                        already_processed.add(item_no)

                    item_found = True
                    if not item_no in ref_dict:
                        if len(item_no) > 2 and item_no[:2] in [
                                'NI', 'PU', 'JA', 'SK'
                        ]:
                            orig_item_no = item_no
                            item_no = "SV24_" + item_no[2:]
                            if not item_no in ref_dict:
                                item_found = False
                        else:
                            orig_item_no = item_no
                            item_no = None
                            item_found = False

                    if not item_found:
                        no_match_ws.write_row(current_nomatch_row, 0,
                                              [orig_item_no, item_no])
                        current_nomatch_row += 1
                        continue

                    ref_lists = ref_dict[item_no]

                    for ref_list in ref_lists:
                        # Initialize row to template size
                        out_row = [''] * num_template_columns

                        out_row[
                            vendor_code_idx] = 'Sport-Versand24 GmbH, de sporting goods, WEIND'
                        out_row[supplier_item_id_idx] = item_no
                        mfr_item_no = item_no
                        if mfr_item_no:
                            # Split the color from the item number ADI<item number>_<color>
                            mfr_item_list = mfr_item_no.split("_")
                            mfr_item_no_wo_color = mfr_item_list[0]

                            # Remove ADI, NI, PU, JA, SK prefixes from manufacturer item number
                            if len(mfr_item_no_wo_color) > 1:
                                prefix = mfr_item_no_wo_color[:2].upper()
                                if prefix in ['NI', 'PU', 'JA', 'SK']:
                                    mfr_item_no_wo_color = mfr_item_no_wo_color[
                                        2:]
                                elif len(mfr_item_no_wo_color) > 2:
                                    if mfr_item_no_wo_color[:3].upper(
                                    ) == 'ADI':
                                        mfr_item_no_wo_color = mfr_item_no_wo_color[
                                            3:]
                            out_row[
                                manufacturer_item_id_idx] = mfr_item_no_wo_color

                            # See if we have a color code
                            if len(mfr_item_list) > 1:
                                out_row[
                                    manufacturer_color_no_idx] = mfr_item_list[
                                        1]

                        out_row[ext_id_type_idx] = 'EAN'
                        ean = ref_list[ref_header.index(REF_COL_EAN)]
                        out_row[ean_idx] = ean

                        item_free2 = ref_list[ref_header.index(
                            REF_COL_ITEM_FREE2)]
                        if item_free2:
                            if '>' in item_free2:
                                item_free2_list = item_free2.split('>')
                            elif '<' in item_free2:
                                item_free2_list = item_free2.split('<')
                            else:
                                item_free2_list = list()

                            # Column M removed
                            # if len(item_free2_list) > 3:
                            #     out_row[12] = item_free2_list[3]
                            if len(item_free2_list) > 2:
                                out_row[level3_idx] = item_free2_list[2]
                                out_row[article_type_idx] = item_free2_list[2]
                            if len(item_free2_list) > 1:
                                out_row[level2_idx] = item_free2_list[1]
                            if len(item_free2_list) > 0:
                                out_row[level1_idx] = item_free2_list[0]

                        producer = ref_list[ref_header.index(
                            REF_COL_ITEM_PRODUCER)]
                        if producer in ref_manufacturers:
                            out_row[brand_name_idx] = ref_manufacturers[
                                producer]
                        else:
                            out_row[brand_name_idx] = "Not_in_Ref"

                        out_row[article_name_idx] = ref_list[ref_header.index(
                            REF_COL_ITEM_FREE1)]
                        out_row[color_name_idx] = str(ref_list[
                            ref_header.index(REF_COL_ITEM_FREE20)]).lower()

                        colors = ref_list[ref_header.index(
                            REF_COL_ITEM_FREE20)]
                        if "/" in colors:
                            # Multi-color item
                            colors = "mehrfarbig"
                        else:
                            if colors in ref_colors:
                                # translate the color
                                colors = ref_colors[colors]
                            else:
                                logging.debug(
                                    "Line %d : Item number %s - Color %s could not be matched.",
                                    line_num, item_no, colors)
                                colors = "Not_in_Ref"

                        out_row[std_color_idx] = colors

                        # Target group
                        target_group = ref_list[ref_header.index(
                            REF_COL_ITEM_FREE19)]
                        if target_group:
                            out_row[target_group_idx] = target_group
                        else:
                            out_row[target_group_idx] = "Uni"

                        var_string = ref_list[ref_header.index(
                            REF_COL_VAR_STRING)]
                        if var_string:
                            var_string_list = var_string.split(':')

                            if len(var_string_list) > 1:
                                out_row[size_idx] = var_string_list[1]
                                out_row[std_size_idx] = var_string_list[1]

                        if item_free2:
                            out_row[keywords_idx] = ';'.join(item_free2_list)
                        out_row[season_idx] = 'Dauerstandard'
                        out_row[model_year_idx] = str(datetime.today().year)
                        out_row[available_idx] = datetime.today().strftime(
                            "%d/%m/%Y")
                        if ean in ref_quants:
                            # print("{} IN".format(ean))
                            try:
                                out_row[qty_first_delv_idx] = int(
                                    ref_quants[ean])
                            except ValueError as ex:
                                logging.warning(
                                    "Integer conversion failed for '%s'",
                                    ref_quants[ean])
                                logging.error(ex)
                                out_row[qty_first_delv_idx] = 0
                        else:
                            # print("{} OUT".format(ean))
                            out_row[qty_first_delv_idx] = 0

                        out_row[dt_first_delv_idx] = datetime.today().strftime(
                            "%d/%m/%Y")

                        # See if we have a price here. If so convert and multiply
                        item_free18 = ref_list[ref_header.index(
                            REF_COL_ITEM_FREE18)]
                        if item_free18:
                            try:
                                # Parse the amount
                                num_item_free18 = float(
                                    str(item_free18).replace(",", "."))

                            except ValueError as ex:
                                logging.debug("Conversion failed for '%s'",
                                                item_free18)
                                logging.error(ex)
                                num_item_free18 = 0.0
                        else:
                            num_item_free18 = 0.0

                        out_row[amaz_purch_price] = "{0:.2f}".format(
                            round(1.2709 * num_item_free18, 2))

                        out_row[purch_price_valid_from_idx] = datetime.today(
                        ).strftime("%d/%m/%Y")
                        out_row[amaz_retail_price_idx] = ref_list[
                            ref_header.index(REF_COL_ITEM_FREE15)]

                        bullet_text = ref_list[ref_header.index(
                            REF_COL_ITEM_FREE4)]
                        out_row[materials_idx] = bullet_text

                        if num_item_free18:
                            out_row[purch_price_idx] = "{0:.2f}".format(
                                round(num_item_free18, 2))

                        if bullet_text:
                            bullets = bullet_text.split("#")
                        else:
                            bullets = list()

                        if len(bullets) == 0:
                            out_row[bullet1_idx] = "Bullet_Error"
                            out_row[bullet2_idx] = "Bullet_Error"
                            out_row[bullet3_idx] = "Bullet_Error"
                        elif len(bullets) == 1:
                            out_row[bullet1_idx] = bullets[0]
                            out_row[bullet2_idx] = "Bullet_Error"
                            out_row[bullet3_idx] = "Bullet_Error"
                        elif len(bullets) == 2:
                            out_row[bullet1_idx] = bullets[0]
                            out_row[bullet2_idx] = bullets[1]
                            out_row[bullet3_idx] = "Bullet_Error"
                        elif len(bullets) == 3:
                            out_row[bullet1_idx] = bullets[0]
                            out_row[bullet2_idx] = bullets[1]
                            out_row[bullet3_idx] = bullets[2]
                        elif len(bullets) > 3:
                            if len(bullets[0]) == 0:
                                out_row[bullet1_idx] = bullets[1]
                                out_row[bullet2_idx] = bullets[2]
                                out_row[bullet3_idx] = bullets[3]
                            else:
                                out_row[bullet1_idx] = bullets[0]
                                out_row[bullet2_idx] = bullets[1]
                                out_row[bullet3_idx] = bullets[2]

                        asin = ref_list[ref_header.index("Asin")]

                        if asin:
                            out_row[ext_id2_type_idx] = "ASIN"
                        else:
                            out_row[ext_id2_type_idx] = "EAN"

                        out_row[asin_idx] = asin

                        out_row[text_idx] = remove_html_tags(
                            ref_list[ref_header.index(REF_COL_TEXT)])
                        out_row[textile_id_idx] = remove_html_tags(
                            ref_list[ref_header.index(REF_COL_TEXTILE_TEXT)])

                        if ean in ref_delivery_dates:
                            out_row[delivery_date_idx] = ref_delivery_dates[
                                ean].replace(".", "/")
                        else:
                            out_row[delivery_date_idx] = 0

                        if ean in ref_pricing:
                            pricing_cols = ref_pricing[ean]
                            pricing_len = len(pricing_cols)
                            out_row[pricing_idx:pricing_idx +
                                    pricing_len] = pricing_cols
                        else:
                            out_row[pricing_idx] = "not_in_Ref"

                        out_row[gender_out_idx] = ref_list[gender_in_idx]

                        out_start = product_type_idx
                        out_end = product_type_idx + len(sizes_header)
                        in_start = ref_header.index(REF_COL_PRODUCT_TYPE)
                        in_end = ref_header.index(REF_COL_PRODUCT_TYPE) + len(
                            sizes_header)
                        out_row[out_start:out_end] = ref_list[in_start:in_end]

                        out_row[image_url_idx] = ref_list[ref_header.index(
                            REF_COL_IMG_URL)]

                        fedas = ref_list[ref_header.index(REF_COL_FEDAS)]
                        out_row[-1] = fedas

                        if ean in ref_combined:
                            out_row = out_row + ref_combined[ean]
                        else:
                            out_row = out_row + [''] * len(combined_header)

                        worksheet.write_row(current_row, 0, out_row)
                        if out_row[qty_first_delv_idx] > 0:
                            worksheet2.write_row(current_row2, 0, out_row)
                            current_row2 += 1

                        current_row += 1
                else:
                    item_no_index = row.index(REF_COL_ITEM_NO)
                    header_read = True
                line_num += 1

    except Exception as ex:  # pylint: disable=broad-except
        logging.error(ex)

        if workbook:
            workbook.close()
            workbook2.close()

        # Clean up. Remove broken out files if they exist
        if os.path.isfile(file_out):
            os.remove(file_out)
    finally:
        if workbook:
            workbook.close()
            workbook2.close()


def process_files(data_root):
    """ Main loop for processing. """
    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_ref = os.path.join(data_root, "ref")
    path_err = os.path.join(data_root, "error")
    path_log = os.path.join(data_root, "log")

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

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

    logging.info("Found %d files...", num_files)

    if num_files == 0:
        return

    try:

        if os.path.isfile(os.path.join(path_ref, "combined.csv")):
            print("Reading combined ref...")
            # Read ref combined
            ref_combined_dict, combined_header = read_ref_combined(path_ref)
        else:
            ref_combined_dict = dict()
            combined_header = []

        print("Reading size ref...")
        # Read ref size
        ref_sizes_dict, sizes_header = read_ref_sizes(path_ref)

        print("Reading items ref...")
        # Read the ref items
        ref_items_dict, header = read_ref_items(path_ref, ref_sizes_dict,
                                                sizes_header)

        print("Reading colors ref...")
        # Read the ref colors
        ref_colors_dict = read_ref_colors(path_ref)

        print("Reading qty ref...")
        # Read the ref order quantities
        ref_quantity_dict = read_ref_quantities(path_ref)

        print("Reading delivery date ref...")
        # Read the ref delivery dates
        ref_delivery_dict = read_ref_delivery_dates(path_ref)

        print("Reading manufacturer ref...")
        # Read the ref manufacturers
        ref_manufacturer_dict = read_ref_manufacturers(path_ref)

        # Read ref pricing
        ref_pricing_dict = read_ref_pricing(path_ref)
    except Exception as ex:  # pylint: disable=broad-except
        print(ex)
        logging.exception("Something went wrong. See below")
        return

    out_template = os.path.join(path_ref, "AmazonItemTemplate.csv")

    for f in files:
        in_file = os.path.join(path_in, f)
        proc_file = os.path.join(path_prc, f)
        err_file = os.path.join(path_err, f)

        out_file_name = os.path.splitext(f)[0] + ".xlsx"
        out_file2_name = os.path.splitext(f)[0] + "_ohne_null.xlsx"
        out_file = os.path.join(path_out, out_file_name)
        out_file2 = os.path.join(path_out, out_file2_name)

        if os.path.isfile(in_file):
            try:
                logging.debug("Processing %s...", in_file)

                create_item_file(in_file,
                                 out_file,
                                 out_file2,
                                 out_template,
                                 ref_items_dict,
                                 header,
                                 ref_colors_dict,
                                 ref_manufacturer_dict,
                                 ref_quantity_dict,
                                 ref_delivery_dict,
                                 ref_pricing_dict,
                                 ref_combined_dict,
                                 sizes_header=sizes_header,
                                 combined_header=combined_header)

                if os.path.isfile(in_file):
                    shutil.move(in_file, proc_file)
            except Exception:  # pylint: disable=broad-except
                logging.exception("An error occurred")

                # Move in file to error
                shutil.move(in_file, err_file)


def main():
    """ Main app. """
    data_root = os.path.join(get_data_root(), DATA_FOLDER_NAME)
    process_files(data_root)


if __name__ == "__main__":
    main()
