import os
from Tkinter import *
import zipfile
import shutil
import tkFont
"""
This script sets the directory for the four scripts required for file association

 - FileAssociation.py
 - manualMatherFA.py
 - plotterFA.py
 - dataQuality.py

1. Change the SubjectID.
2. Open the 'readme.txt'
3. Open the 'FileAssociationPNG' folder
4. Check/verify validity of each match
5. Match the last png files in the PNG folder to associated binary number.
6. Fill in brackets with associated files as pairs in each row
7. Copy/paste bracketed numbers below after 'pairs = ' ...
8. Run plotterFA.py
13. Run manualMatcherFA.py
14. Run dataQuality.py

"""

def setDirectory(unzip):
    SubjectID = "CMULTIS" + "011-1"
    # print('Move "Export" folder to MULTIS_trials')
    # ans = raw_input('Unpack files? [y/n] :')

    class introApp():
        def __init__(self, master):
            # self.title("File Association")
            self.helv = tkFont.Font(size=36)
            # self.L1 = Label(master, text="SubjectID i.e.'001-1'", font=self.helv).grid(row=1, columnspan=1, sticky=E)
            # self.L2 = Label(master, text="Unpack").grid(row=2, sticky='nsew')

            # self.e1 = Entry(master)
            # self.e1.grid(row=1, column=2, columnspan=1, sticky=E)

            self.v = IntVar()
            self.c1 = Checkbutton(master, text="Config Subject Directory from 'Export' folder", font=self.helv,
                                  variable=self.v,
                                  onvalue=1, offvalue=0).grid(row=2, columnspan=3, sticky=W)
            self.b1 = Button(master, text='GO', fg="Green", command=master.quit).grid(row=3, columnspan=4,
                                                                                      sticky='nsew',
                                                                                      pady=10, padx=10)
            master.grid_rowconfigure(0, weight=1)
            master.grid_rowconfigure(3, weight=1)
            master.grid_columnconfigure(0, weight=1)
            master.grid_columnconfigure(3, weight=1)

            def center(master):
                master.update_idletasks()
                w = master.winfo_screenwidth()
                h = master.winfo_screenheight()
                size = tuple(int(_) for _ in master.geometry().split('+')[0].split('x'))
                x = w / 2 - size[0] / 2
                y = h / 2 - size[1] / 2
                master.geometry("%dx%d+%d+%d" % (size + (x, y)))

            center(master)



    # SubjectID = "MULTIS" + app.e1.get()
    if unzip == 1:
        root = Tk()
        root.title("File Association")
        app = introApp(root)
        root.mainloop()
        if app.v.get() == 1:
            zip_ref = zipfile.ZipFile('../MULTIS_trials/EXPORT/' + SubjectID + '.zip', 'r')
            zip_ref.extractall(path='../MULTIS_trials')
            zip_ref.close()
            shutil.move('../MULTIS_trials/EXPORT', '../MULTIS_trials/' + SubjectID)
            os.rename('../MULTIS_trials/' + SubjectID + '/EXPORT', '../MULTIS_trials/' + SubjectID + '/Ultrasound')

      # change Directory for each subject
    Directory = "../MULTIS_trials/" + SubjectID
    tdmsDirectory = Directory + "/Data/"
    dicomDirectory = Directory + "/Ultrasound/"

    return Directory, SubjectID, tdmsDirectory, dicomDirectory

# this function is used by another script to run test plots on same files withtout having to change filenames
def setFileNames(iter):
    Directory, SubjectID, tdmsDirectory, dicomDirectory = setDirectory(0)
    dcmList = sorted(os.listdir(dicomDirectory))
    tdmsList = sorted(os.listdir(tdmsDirectory))
    # tdms, dcm  # copy/paste below after pairs =
    pairs = [['029','29']]





    for tdmsFiles in tdmsList:
        if pairs[iter][0] + '_' in tdmsFiles:
            # print tdmsFiles
            break

    for dcmFiles in dcmList:
        if pairs[iter][1] + '.2017' in dcmFiles:  #update year when neccessary
            # print dcmFiles

            break

    return dcmFiles, tdmsFiles, pairs

def rwaveSelection():
    theChosenRwaves = [(0,1,2,3,4,5,6)]
    return theChosenRwaves

if __name__ == '__main__':
    import AutoMatcherFA.py
