#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 4.15
# In conjunction with Tcl version 8.6
#    Oct 03, 2018 10:29:07 PM

import sys
import dao
import matplotlib.pyplot as plt

try:
    from Tkinter import *
except ImportError:
    from tkinter import *
    from tkinter import messagebox

try:
    import ttk

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

    py3 = True

import transactions_support


def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = Tk()
    top = Transactions(root)
    transactions_support.init(root, top)
    root.mainloop()


w = None


def create_Transactions(root, *args, **kwargs):
    '''Starting point when module is imported by another program.'''
    global w, w_win, rt
    rt = root
    w = Toplevel(root)
    top = Transactions(w)
    transactions_support.init(w, top, *args, **kwargs)
    return (w, top)


def destroy_Transactions():
    global w
    w.destroy()
    w = None


def plot_data(amount, pmode):
    global x, y2
    y2 = amount
    x = pmode


class Transactions:
    def plot_graph(self):
        global x, y2
        try:
            plt.bar(x, y2, label='Total Amount')
            plt.legend()
            plt.show()
        except Exception:
            messagebox.showerror("Plotting Graph", "First Select Sort By Payment Mode")

    def show_all_transactions(self):
        self.Listbox1.delete(0, self.Listbox1.size())
        self.Listbox1.insert(END,
                             "{:9s} {:9s} {:15s} {:20s}".format('Bill ID', 'Trip ID', 'Amount', 'Payment Mode'))
        for item in dao.get_transactions():
            self.Listbox1.insert(END,
                                 "{:9s} {:9s} {:15s} {:20s}".format(str(item[0]), str(round(item[1], 2)), str(item[2]),
                                                                    str(item[3])))

    def show_pmode_transactions(self):
        amount = []
        pmode = []
        self.Listbox1.delete(0, self.Listbox1.size())
        self.Listbox1.insert(END,
                             "{:9s} {:9s} {:15s}".format('Total Transactions', 'Total Amount', 'Payment Mode'))
        for item in dao.get_transactions_by_pmode():
            self.Listbox1.insert(END,
                                 "{:30s} {:30s} {:15s}".format(str(item[0]), str(round(item[1], 2)), str(item[2])))
            amount.append(round(item[1], 2))
            pmode.append(item[2])
        plot_data(amount, pmode)

    def __init__(self, top=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 = '#d9d9d9'  # X11 color: 'gray85'
        font9 = "-family Roboto -size 20 -weight normal -slant roman " \
                "-underline 0 -overstrike 0"

        top.geometry("931x693+567+153")
        top.title("Transactions")
        top.configure(background="#d4fff2")

        self.Frame1 = Frame(top)
        self.Frame1.place(relx=0.0, rely=0.0, relheight=0.25, relwidth=1.02)
        self.Frame1.configure(relief=GROOVE)
        self.Frame1.configure(borderwidth="2")
        self.Frame1.configure(relief=GROOVE)
        self.Frame1.configure(background="#3e29ff")
        self.Frame1.configure(width=945)

        self.Label1 = Label(self.Frame1)
        self.Label1.place(relx=0.02, rely=0.11, height=138, width=155)
        self.Label1.configure(background="#3e29ff")
        self._img1 = PhotoImage(file="../../../../media/viplove/COMMON FILE/DBMSProject/images/transactions.png")
        self.Label1.configure(image=self._img1)
        self.Label1.configure(text='''Label''')
        self.Label1.configure(width=155)

        self.Label2 = Label(self.Frame1)
        self.Label2.place(relx=0.24, rely=0.4, height=44, width=105)
        self.Label2.configure(background="#3e29ff")
        self.Label2.configure(font=font9)
        self.Label2.configure(foreground="#ffffff")
        self.Label2.configure(text='''Sort By''')

        self.allButton = Button(self.Frame1)
        self.allButton.place(relx=0.41, rely=0.4, height=52, width=63)
        self.allButton.configure(activebackground="#d9d9d9")
        self.allButton.configure(background="#1eadd8")
        self.allButton.configure(font=font9)
        self.allButton.configure(foreground="#ffffff")
        self.allButton.configure(text='''All''')
        self.allButton.configure(command=self.show_all_transactions)

        self.pmodeButton = Button(self.Frame1)
        self.pmodeButton.place(relx=0.53, rely=0.4, height=52, width=237)
        self.pmodeButton.configure(activebackground="#d9d9d9")
        self.pmodeButton.configure(background="#1eadd8")
        self.pmodeButton.configure(font=font9)
        self.pmodeButton.configure(foreground="#ffffff")
        self.pmodeButton.configure(text='''Payment Mode''')
        self.pmodeButton.configure(command=self.show_pmode_transactions)

        self.Listbox1 = Listbox(top)
        self.Listbox1.place(relx=0.02, rely=0.27, relheight=0.57, relwidth=0.95)
        self.Listbox1.configure(background="#ffffffffffff")
        self.Listbox1.configure(font=font9)
        self.Listbox1.configure(width=884)
        self.Listbox1.insert(END, "Please Select One of Two Buttons to View Data")

        self.plotButton = Button(top)
        self.plotButton.place(relx=0.0, rely=0.88, height=86, width=941)
        self.plotButton.configure(activebackground="#d9d9d9")
        self.plotButton.configure(background="#3e29ff")
        self.plotButton.configure(font=font9)
        self.plotButton.configure(foreground="#ffffff")
        self.plotButton.configure(highlightbackground="#8a72d8")
        self.plotButton.configure(text='''Plot By Payment Mode''')
        self.plotButton.configure(width=941)
        self.plotButton.configure(command=self.plot_graph)


if __name__ == '__main__':
    vp_start_gui()
