#!/usr/bin/env python

import codecs
import csv
import logging.config
import os
import shutil
import time


def setup_logging(log_path):
    # Set up logging
    log_file = os.path.join(log_path, '{}.log'.format(time.strftime("%Y%m%d-%H%M%S")))

    logging.config.dictConfig({
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'standard': {
                'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
            },
        },
        'handlers': {
            'default': {
                'level': 'NOTSET',
                'class': 'logging.StreamHandler',
                'formatter': 'standard'
            },
            'logfile': {
                'filename': log_file,
                'class': 'logging.FileHandler',
                'formatter': 'standard'
            }
        },
        'loggers': {
            '': {
                'handlers': ['default', 'logfile'],
                'level': 'NOTSET',
                'propagate': True
            }
        }
    })


def parse_numeric(value):
    value_numeric = 0.0

    if value:
        try:
            value_us_fmt = value.replace(".", "").replace(",", ".")
            value_numeric = float(value_us_fmt)
        except ValueError as ve:
            logging.error(ve)
            logging.error("could not convert " + value)

    return value_numeric


def filter_inventory(in_file, out_file):
    with codecs.open(in_file, "r", encoding='iso-8859-1') 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 len(row) < 9:
                continue

            if row[6] != "in_stock":
                continue

            if parse_numeric(row[8]) > 0:
                writer.writerow(row)


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")
    in_filename = "CSV_availability_.csv"
    out_filename = 'CSV_availability_out.csv'

    setup_logging(path_log)


    source_file = os.path.join(data_root, "puma", in_filename)
    in_file = os.path.join(path_in, in_filename)
    out_file = os.path.join(path_out, out_filename)
    err_file = os.path.join(path_err, in_file)
    proc_file = os.path.join(path_proc, in_filename)

    try:
        if not os.path.isfile(source_file):
            logging.warning("File {} not found".format(source_file))
            return

        if os.path.exists(in_file):
            os.remove(in_file)

        logging.debug("Moving source file {} to {}".format(source_file, in_file))
        shutil.move(source_file, in_file)

        if not os.path.isfile(in_file):
            logging.warning("File {} not found".format(in_file))
            return

        logging.debug("Filtering inventory for {0}...".format(in_file))
        filter_inventory(in_file, out_file)

        if os.path.exists(proc_file):
            os.remove(proc_file)

        shutil.move(in_file, proc_file)
    except Exception as e:
        logging.error(e)

        if os.path.exists(err_file):
            os.remove(err_file)

        shutil.move(in_file, err_file)
        exit(1)


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

    project_data_root = os.path.join(data_root, "Puma")
    process(project_data_root)
    print("Done.")


if __name__ == "__main__":
    main()
