#!/usr/bin/env python

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

__author__ = 'holger'

def process_retoure_file(in_file, out_file):
    # Check if file already exists
    write_header = not os.path.isfile(out_file)

    with codecs.open(in_file, "r", encoding='cp1252') as f, \
            codecs.open(out_file, "a", encoding='cp1252') as of:
        reader = csv.reader(f, delimiter=';')
        writer = csv.writer(of, delimiter=';')

        if write_header:
            writer.writerow(['CustomerSign','ExternalOrderID','OrderID','CustomerID'])

        for row in reader:
            write_row = False
            cust_sign = row[1]
            ext_order_id = ''
            order_id = ''
            customer_id = ''
            len_order_id = len(row[5])
            if len_order_id > 0:
                order_id_raw = row[5]
                search_object =  re.search(r'(\d\d\d-\d\d\d\d\d\d\d-\d\d\d\d\d\d\d)', order_id_raw)
                if search_object:
                    ext_order_id = search_object.group(1)
                    write_row = True
                else:
                    search_object =  re.search(r'(2\d\d\d\d\d\d)', order_id_raw)
                    if search_object:
                        order_id = search_object.group(1)
                        write_row = True
                    else:
                        search_object =  re.search(r'(3\d\d\d\d\d\d)', order_id_raw)
                        if search_object:
                            customer_id = search_object.group(1)
                            write_row = True


            if write_row:
                writer.writerow([cust_sign, ext_order_id, order_id, customer_id])

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
    time_file_name = time.strftime("%Y%m%d-%H%M%S")
    log_file_name = time_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

    output_file_name = 'Sync_Order_ID' + '_1.csv'
    output_file_name_2 = 'Sync_Extern_ID' + '_2.csv'
    output_file_name_3 = 'Sync_Kunden_ID' + '_3.csv'

    out_file = os.path.join(path_out, output_file_name)
    out_file_2 = os.path.join(path_out, output_file_name_2)
    out_file_3 = os.path.join(path_out, output_file_name_3)

    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)

        if os.path.isfile(in_file):
            try:
                logging.debug("Processing {0}...".format(in_file))
                process_retoure_file(in_file, out_file)
                if os.path.isfile(out_file):
                    shutil.copy(out_file, out_file_2)
                    shutil.copy(out_file, out_file_3)
                shutil.move(in_file, proc_file)
            except Exception as e:
                logging.error(e)
                shutil.move(in_file, err_file)

def main():
    data_root = r"/ftproot/"
    #data_root = r"/data/projects/pycharm/thelen/conversion/data/"
    #data_root = r"c:/temp/thelen/"

    dupe_data_root = os.path.join(data_root, "Matching_Retoure_DHL")
    process_files(dupe_data_root)

if __name__ == "__main__":
    main()
