#!/usr/bin/env python

# in verband met bug:
import Tkinter
Tkinter.wantobjects = 0

from Tkinter import *
import tkFileDialog

import columns

padx = '6m'
pady = '2m'

def run():

    def browse1():
        f = tkFileDialog.askopenfilename(title='Select input file')
        if f:
            ent1.delete(0,END)
            ent1.insert(0,f)

    def browse2():
        f = tkFileDialog.asksaveasfilename(title='Select output file')
        if f:
            ent2.delete(0,END)
            ent2.insert(0,f)

    def browse4():
        f = tkFileDialog.askopenfilename(title='Select feature definition file')
        if f:
            ent4.delete(0,END)
            ent4.insert(0,f)

    def browse7():
        f = tkFileDialog.asksaveasfilename(title='Select feature error file')
        if f:
            ent7.delete(0,END)
            ent7.insert(0,f)

    def check():
        infile = ent1.get()
        outfile = ent2.get()
        tests = ent3.get()
        features = ent4.get()
        options = ent5.get()
        foptions = ent6.get()
        errorfile = ent7.get()
        overwrite = optR.get()
        keeptemp = optT.get()
        columns.run(infile, outfile, tests,
                    levenargs=options,
                    featurefile=features, featureargs=foptions, errorfile=errorfile,
                    keeptemp=keeptemp, overwrite=overwrite)

    root = Tk()
    root.title('RuG/L04 - Levenshtein differences by columns')

    f = Frame(root)
    f.pack(side=TOP, fill=X, padx=padx, pady=pady)

    lbl = Label(f, text='Input file:', anchor=W)
    lbl.pack(side=TOP,fill=X)
    fr2 = Frame(f)
    fr2.pack(side=TOP,fill=X)
    select = Button(fr2, text='Open', command=browse1)
    select.pack(side=RIGHT)
    ent1 = Entry(fr2,width=60)
    ent1.pack(side=LEFT, expand=YES, fill=X)

    f = Frame(root)
    f.pack(side=TOP, fill=X, padx=padx, pady=pady)

    lbl = Label(f, text='Output file:', anchor=W)
    lbl.pack(side=TOP,fill=X)
    fr2 = Frame(f)
    fr2.pack(side=TOP,fill=X)
    select = Button(fr2, text='Save as', command=browse2)
    select.pack(side=RIGHT)
    ent2 = Entry(fr2,width=60)
    ent2.insert(0,'out.data')
    ent2.pack(side=LEFT, expand=YES, fill=X)

    f = Frame(root)
    f.pack(side=TOP, fill=X, padx=padx, pady=pady)

    lbl = Label(f, text='OPTIONAL - Test column number(s):', anchor=W)
    lbl.pack(side=TOP,fill=X)
    ent3 = Entry(f,width=60)
    ent3.insert(0,'1')
    ent3.pack(side=LEFT, expand=YES, fill=X)

    f = Frame(root)
    f.pack(side=TOP, fill=X, padx=padx, pady=pady)

    lbl = Label(f, text="OPTIONAL - Extra arguments for 'leven' program:", anchor=W)
    lbl.pack(side=TOP,fill=X)
    ent5 = Entry(f,width=60)
    ent5.pack(side=LEFT, expand=YES, fill=X)

    f = Frame(root)
    f.pack(side=TOP, fill=X, padx=padx, pady=pady)

    lbl = Label(f, text='OPTIONAL - Feature definition file:', anchor=W)
    lbl.pack(side=TOP,fill=X)
    fr2 = Frame(f)
    fr2.pack(side=TOP,fill=X)
    select = Button(fr2, text='Open', command=browse4)
    select.pack(side=RIGHT)
    ent4 = Entry(fr2,width=60)
    ent4.pack(side=LEFT, expand=YES, fill=X)

    f = Frame(root)
    f.pack(side=TOP, fill=X, padx=padx, pady=pady)

    lbl = Label(f, text='OPTIONAL - Feature error file:', anchor=W)
    lbl.pack(side=TOP,fill=X)
    fr2 = Frame(f)
    fr2.pack(side=TOP,fill=X)
    select = Button(fr2, text='Save as', command=browse7)
    select.pack(side=RIGHT)
    ent7 = Entry(fr2,width=60)
    ent7.pack(side=LEFT, expand=YES, fill=X)

    f = Frame(root)
    f.pack(side=TOP, fill=X, padx=padx, pady=pady)

    lbl = Label(f, text="OPTIONAL - Extra arguments for 'features' program:", anchor=W)
    lbl.pack(side=TOP,fill=X)
    ent6 = Entry(f,width=60)
    ent6.pack(side=LEFT, expand=YES, fill=X)

    f = Frame(root)
    f.pack(side=TOP, fill=X, padx=padx, pady=pady)

    fr2 = Frame(f)
    fr2.pack(side=TOP,fill=X)

    optR = IntVar()
    optR.set(0)
    Checkbutton(fr2, text='Replace existing files', variable=optR).pack(side=LEFT)

    optT = IntVar()
    optT.set(0)
    Checkbutton(fr2, text='Keep temporary files', variable=optT).pack(side=LEFT)

    f = Frame(root)
    f.pack(side=TOP, fill=X, padx=padx, pady=pady)

    fr2 = Frame(f)
    fr2.pack(side=TOP,fill=X)

    b = Button(fr2, text='RUN', command=check)
    b.pack(side=LEFT)

    b = Button(fr2, text='EXIT', command=root.quit)
    b.pack(side=RIGHT)

    root.mainloop()

if __name__ == '__main__':
    run()
