#!/usr/bin/env python

__author__ = 'holger.knust'

import codecs
import csv
import datetime
from dateutil.parser import parse
import logging
import os
import shutil
import time


def convert(in_file, out_file, out_file2):
    header_read = False
    oid_index = -1
    pd_index = -1
    dpp_index = -1
    with codecs.open(in_file, "r", encoding='cp1252') as f, codecs.open(out_file, "w", encoding='utf-8') as of, \
            codecs.open(out_file2, "w", encoding='utf-8') as ofp:
        reader = csv.reader(f, delimiter='\t')
        writer = csv.writer(of, delimiter='\t')
        writer2 = csv.writer(ofp, delimiter=';')

        for row in reader:
            if header_read:
                order_id = row[oid_index]
                purch_date = row[pd_index]
                days_past_prom = row[dpp_index]

                try:
                    days = int(days_past_prom)
                except ValueError as ve:
                    days = 0

                if days > 2:
                    dt = parse(purch_date)

                    if type(dt) is datetime.datetime:
                        writer.writerow([order_id,'','',dt.strftime("%Y-%m-%d"),'','','',''])
                        writer2.writerow([order_id,'4.9','6'])
            else:
                header_read = True

                if "order-id" in row:
                    oid_index = row.index("order-id")

                if "purchase-date" in row:
                    pd_index = row.index("purchase-date")

                if "days-past-promise" in row:
                    dpp_index = row.index("days-past-promise")

                if oid_index < 0 or pd_index < 0 or dpp_index < 0:
                    raise Exception("Could not find needed columns")

                # Write new headers
                writer.writerow(['order-id','order-item-id','quantity','ship-date','carrier-code','carrier-name',
                                 'tracking-number','ship-method'])

                writer2.writerow(['ExternalOrderID','OrderStatus','Marking'])

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

    for f in files:
        log_file_name = time.strftime("%Y%m%d-%H%M%S") + '.log'

        in_file = os.path.join(path_in, f)
        out_file_name = 'no_shipping_amazon_' + time.strftime("%Y%m%d-%H%M%S") + '.txt'
        out_file_name2 = 'no_shipping_plenty_' + time.strftime("%Y%m%d-%H%M%S") + '.csv'
        out_file = os.path.join(path_out, out_file_name)
        out_file2 = os.path.join(path_out, out_file_name2)
        proc_file = os.path.join(path_proc, f)
        err_file = os.path.join(path_err, f)
        log_file = os.path.join(path_log, log_file_name)

        logging.basicConfig(filename=log_file, level=logging.DEBUG)

        logging.debug("Converting no shipping for {0}...".format(in_file))

        if os.path.isfile(in_file):
            try:
                print("Converting {0} to {1}...".format(in_file, out_file))
                convert(in_file, out_file, out_file2)
                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_Amazon_NO_Shipping")

    process(project_data_root)

    print("Done.")


if __name__ == "__main__":
    main()


