#!/usr/bin/env python

import os
import shutil

def process_files(root_folder):
    path_in = os.path.join(root_folder, "in")
    path_out = os.path.join(root_folder, "out")
    path_prc = os.path.join(root_folder, "processed")
    path_err = os.path.join(root_folder, "error")
    path_log = os.path.join(root_folder, "log")

    files = os.listdir(path_in)
    num_files = len(files)

    if num_files == 0:
        print("Retieving items file...")
        try:
            from urllib.request import urlretrieve
            url = "http://www.sport-versand24.de/plenty/api/external.php?PlentyWebToken=" + \
                  "1742***N3dd0VNA129dDZ5ac1GqVHOCE9CRg6TtxuKvGYt5NrVOEPnhFtLYzYY4UbJ1Wc" + \
                  "hIMq4QEmKPRfbWtE2wwCLlYjcsAliVBEdO&format=StarSellerArticleRepricingCSV&lang=de&amazon_account=0"
            dst = os.path.join(path_in, "items.csv")
            urlretrieve(url, dst)
            print("File retrieved.")
        except Exception as ex:
            print(ex)
            exit(1)

    files = os.listdir(path_in)
    num_files = len(files)

    print("Found {0} files...".format(num_files))

    if num_files == 0:
        return

    no_adidas_out = os.path.join(path_out, "noadidas.csv")
    no_adidas_no_nike_out = os.path.join(path_out, "noadi_nike.csv")

    for f in files:
        try:
            in_file = os.path.join(path_in, f)
            proc_file = os.path.join(path_prc, f)
            err_file = os.path.join(path_err, f)
            with open(in_file, "r", encoding="cp1252") as f, \
                open(no_adidas_out, "w", encoding="utf-8") as noaf, \
                open(no_adidas_no_nike_out, "w", encoding="utf-8") as nonf:

                lines = f.readlines()

                for line in lines:
                    lower_case_line = line.lower()
                    if not "adidas" in lower_case_line:
                        # Write non Adidas items
                        noaf.write(line)
                        if not "nike" in lower_case_line:
                            # Write non Adidas/non Nike items
                            nonf.write(line)

            shutil.move(in_file, proc_file)
        except Exception as e:
            print(e)
            if os.path.exists(err_file):
                os.remove(err_file)
            shutil.move(in_file, err_file)

def main():
    data_root = r"/ftproot/"
    # data_root = r"/home/holger/git/bitbucket/thelen/data/"

    inventory_data_root = os.path.join(data_root, "Matching_NoAdidas_Bestand")
    process_files(inventory_data_root)

if __name__ == "__main__":
    main()
