#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 5.0.3
#  in conjunction with Tcl version 8.6
#    May 01, 2020 06:36:35 PM CEST  platform: Linux

import sys

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

from tkinter import filedialog, END, IntVar, messagebox
import os
from shutil import copy, rmtree
#import annotations_support

def startChangeAnn(genomeAnn):

    if genomeAnn == "NA" or genomeAnn == "":
        return
    root = tk.Tk()
    top = ChangeAnnotation(root, genomeAnn)
    root.mainloop()
    root.quit()

class ChangeAnnotation:
    def __init__(self, top=None, genomeAnn=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#ececec' # Closest X11 color: 'gray92'

        top.geometry("600x224+625+181")
        top.minsize(1, 1)
        top.maxsize(1825, 970)
        top.resizable(1, 1)
        top.title("Change Annotation File")
        self.root = top

        self.FrameFile = tk.Frame(top)
        self.FrameFile.place(relx=0.012, rely=0.045, relheight=0.344
                , relwidth=0.975)
        self.FrameFile.configure(relief='flat')
        self.FrameFile.configure(borderwidth="2")

        """
        self.LabelFile = tk.Label(self.FrameFile)
        self.LabelFile.place(relx=0.017, rely=0.13, height=15, width=165)
        self.LabelFile.configure(text='''Choose annotation file:''')
        """

        self.ButtonFile = tk.Button(self.FrameFile)
        self.ButtonFile.place(relx=0.012, rely=0.251) #, height=35, width=125)
        self.ButtonFile.configure(text='Choose a new \nAnnotation file', command=self.pickAnnFile)

        self.TextFile = tk.Text(self.FrameFile)
        self.TextFile.place(relx=0.38, rely=0.255, relheight=0.39, relwidth=0.597)
        self.TextFile.configure(background="#d9d9d9")
        self.TextFile.configure(font="TkTextFont")
        self.TextFile.configure(selectbackground="#c4c4c4")
        self.TextFile.configure(wrap="word")
        self.TextFile.configure(state="disabled")

        self.FrameChoice = tk.Frame(top)
        self.FrameChoice.place(relx=0.012, rely=0.411, relheight=0.344
                , relwidth=0.975)
        self.FrameChoice.configure(relief='groove')
        self.FrameChoice.configure(borderwidth="2")
        self.FrameChoice.configure(relief="groove")

        self.LabelChoice = tk.Label(self.FrameChoice)
        self.LabelChoice.place(relx=0.017, rely=0.13, height=15, width=130)
        self.LabelChoice.configure(text='''Choose one option:''')

        self.radioVar = IntVar()
        self.RadioOver = tk.Radiobutton(self.FrameChoice)
        self.RadioOver.place(relx=0.137, rely=0.519, relheight=0.221
                , relwidth=0.323)
        self.RadioOver.configure(justify='left')
        self.RadioOver.configure(text='''Overwrite previous file''')
        self.RadioOver.configure(variable=self.radioVar, value=0)

        self.RadioAppend = tk.Radiobutton(self.FrameChoice)
        self.RadioAppend.place(relx=0.564, rely=0.519, relheight=0.221
                , relwidth=0.28)
        self.RadioAppend.configure(justify='left')
        self.RadioAppend.configure(text='''Extend previous file''')
        self.RadioAppend.configure(variable=self.radioVar, value=1)

        self.ButtonConfirm = tk.Button(top)
        self.ButtonConfirm.place(relx=0.45, rely=0.804, height=25, width=56)
        self.ButtonConfirm.configure(text='''Next''', command=self.confirm)
        
        self.ann = ""
        self.originalAnn = genomeAnn
        
    def pickAnnFile(self):
        self.ann = filedialog.askopenfilename()
        self.TextFile.configure(state="normal")
        self.TextFile.delete(1.0, END)
        self.TextFile.insert(END, self.ann)
        self.TextFile.configure(state="disabled")
        
    def confirm(self):
        if not self.ann :
            messagebox.showerror("Missing!","Please choose an annotation file")
            return
        if self.radioVar.get() == 0:
            copy(self.ann, self.originalAnn)
        else:
            with open (self.originalAnn, 'a') as oldAnn:
                with open (self.ann, 'r') as newAnn:
                    for line in newAnn:
                        oldAnn.write("\n"+line.strip())
        messagebox.showinfo("Done!","Procedure finished")
        self.root.destroy()


if __name__ == '__main__':
    startChangeAnn(sys.argv[1])





