from directories import *
from shutil import copy
import os, time, fnmatch, collections
from datetime import datetime


# Construct shared file paths
def make_shared_filesys(test_path):
    # Split full path into array, then make one path at a time
    (dirname, filename) = os.path.split(shared_path_1)
    os.makedirs(test_path + dirname)

    # Create more shared paths
    for dir in shared_path_2:
        os.makedirs(test_path + shared_path_1 + dir)

# Construct file paths and reports
def make_filesys(TEST_type, SUBTEST_type):
    # generate date and time
    date = time.strftime("%d-%m-%Y_%H-%M-%S")

    # Create filesys format
    for i in range(len(SUBTEST_type)):
        path_name = my_desktop + shared_path_1 + shared_path_2[TEST_type] + \
        SUBTEST_type[i] + "/" + SUBTEST_type[i] + FORMAT_1 + FORMAT_2 + FORMAT_3 + \
        date + FORMAT_0 + SUBTEST_type[i] + FORMAT_4 + FORMAT_1 + FORMAT_2 + FORMAT_3 + \
        date + FORMAT_0 + SUBTEST_type[i] + FORMAT_5

        # Split full path into array, then make one path at a time
        (dir_name, file_name) = os.path.split(path_name)
        os.makedirs(dir_name)

        # Create report
        os.system("touch " + path_name)

#find pattern
def find(pattern, path):
    result = []
    for root, dirs, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                result.append(os.path.join(root, name))
    return result

# Create list of files to fetch
def fetch_setup(TEST_type, SUBTEST_type):
    fetch_list = []
    for test in range(len(SUBTEST_type)):
        path_to_dir = my_desktop + shared_path_1 + shared_path_2[TEST_type] + SUBTEST_type[test]
        l = find('*report.pdf', path_to_dir)

        time_list = []
        for i in l:
            split_1 = i.split("/")
            split_2 = str(split_1[15]).split("_")
            time = split_2[0] + "_" + split_2[1]
            time_list.append(time)
        print(time_list)

        # create dictionary
        mydict = {}

        # add all values into dictionary
        j = 0
        for i in time_list:
            mydict[i] = l[j]
            j += 1
        print(mydict)

        # sort dictionary
        ordered = collections.OrderedDict(sorted(mydict.items(), reverse=True, key=lambda t: t[0]))
        print("\n\n")
        print(ordered)
        print("\n\n")

        # determine newest files
        new_file = (ordered[ordered.keys()[0]])

        # store newest file into array for future fetch
        fetch_list.append(new_file)

    # print and return list
    print(fetch_list)
    return fetch_list

# Fetch List of files
def fetch(list_input):
    # Loop through each list to grab and copy to destination
    current_time = datetime.now().strftime("%Y-%m-%d %H:%M")
    os.mkdir(dst_my_desktop + current_time)

    for i in list_input:
        copy(i, dst_my_desktop + current_time)

# Select tests logs desired
def selection():
    x = raw_input("\nCreate new filesys? (y/n)\n")
    y = raw_input("\nCreate new reports? (y/n)\n")
    z = raw_input("(1): " + TEST_1_NAME + "\n(2): " + TEST_2_NAME + "\n(3): " + \
    TEST_3_NAME + "\n(4): " + TEST_4_NAME + "\n(5): " + TEST_5_NAME)

    if (x == 'y'):
        make_shared_filesys(my_desktop)

    if (y == 'y'):
        make_filesys(1, TEST_1)
        make_filesys(0, TEST_2)
        make_filesys(2, TEST_3)
        make_filesys(3, TEST_4)
        make_filesys(0, TEST_5)

    if (z == '1'):
        fetch(fetch_setup(1, TEST_1))
    elif (z == '2'):
        fetch(fetch_setup(0, TEST_2))
    elif (z == '3'):
        fetch(fetch_setup(2, TEST_3))
    elif (z == '4'):
        fetch(fetch_setup(3, TEST_4))
    elif (z == '5'):
        fetch(fetch_setup(0, TEST_5))

# Main
selection()
