#!/usr/bin/env python

__author__ = 'holger'

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

def filter_prices(in_file, out_file):
    header_read = False
    header_row = ['EAN','PurchasePrice','RecommendedRetailPrice']

    ean_col_index = -1
    price_col_index = -1
    msrp_col_index = -1

    max_price = 0.0
    item_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:
            if header_read:
                ean = row[ean_col_index]
                sales_price = row[price_col_index].replace(',','.')
                msrp = row[msrp_col_index].replace(',','.')

                try:
                    sales_price_num = locale.atof(sales_price)
                except Exception as ex:
                    logging.error(ex)
                    sales_price_num = 0.0

                try:
                    msrp_num = locale.atof(msrp)
                except Exception as ex:
                    logging.error(ex)
                    msrp_num = 0.0

                if ean in item_dict:
                    current_price = item_dict[ean][1]
                    if current_price < sales_price_num:
                        item_dict[ean] = [ean, sales_price_num, msrp_num]
                else:
                    item_dict[ean] = [ean, sales_price_num, msrp_num]

            else:
                header_read = True

                if 'EAN' in row:
                    ean_col_index = row.index('EAN')

                if 'Sales price' in row:
                    price_col_index = row.index('Sales price')

                if 'Recommended retail price' in row:
                    msrp_col_index = row.index('Recommended retail price')


        writer.writerow(header_row)
        for k, v in item_dict.items():
            writer.writerow(v)

def process_files(data_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")

    # Set up logging
    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 {0} files...".format(num_files))

    if num_files == 0:
        return


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

        if os.path.isfile(in_file):
            try:
                logging.debug("Processing {0}...".format(in_file))

                filter_prices(in_file, out_file)

                if os.path.isfile(in_file):
                    shutil.move(in_file, proc_file)

            except Exception as e:
                logging.error(e)

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

                # Clean up. Remove broken out files if they exist
                if os.path.isfile(out_file):
                    os.remove(out_file)

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

    data_root = os.path.join(data_root, "Matching_Preise")
    process_files(data_root)

if __name__ == "__main__":
    main()

