#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# sobtc_app.py
#
# Replicated by Billy Wilson Arante
# Replicated on 2016/11/20 EDT
#
# Attribution: GUIs with Tkinter (intermediate) by sentdex (YouTube)

import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, \
        NavigationToolbar2TkAgg
from matplotlib.figure import Figure

import matplotlib.animation as animation
from matplotlib import style

import tkinter as tk
from tkinter import ttk

style.use("ggplot")
matplotlib.use("TkAgg")

f = Figure(figsize=(5,5), dpi=100)
a = f.add_subplot(111)

class SeaOfBTCApp(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Sea of BTC Client")

        # _tkinter.TclError: error reading bitmap file "home.xbm"
        # Error encountered in Xubuntu 16.04
        #tk.Tk.wm_iconbitmap(self, bitmap="@home.xbm")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for f in (StartPage, PageOne, PageTwo, PageThree):
            frame = f(container, self)
            self.frames[f] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, controller):

        frame = self.frames[controller]
        frame.tkraise()

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        
        tk.Frame.__init__(self, parent)

        label = ttk.Label(self, text="Start page")
        label.pack(padx=10, pady=10)

        button = ttk.Button(self, text="Visit page 1",
                command=lambda: controller.show_frame(PageOne))
        button.pack()

        button1 = ttk.Button(self, text="Visit page 2",
                command=lambda: controller.show_frame(PageTwo))
        button1.pack()

        button2 = ttk.Button(self, text="Visit graph page",
                command=lambda: controller.show_frame(PageThree))
        button2.pack()

class PageOne(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text="Page one")
        label.pack(padx=10, pady=10)

        button = ttk.Button(self, text="Home",
                command=lambda: controller.show_frame(StartPage))
        button.pack()

        button1 = ttk.Button(self, text="Visit page 2",
                command=lambda: controller.show_frame(PageTwo))
        button1.pack()

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text="Page two")
        label.pack(padx=10, pady=10)

        button = ttk.Button(self, text="Home",
                command=lambda: controller.show_frame(StartPage))
        button.pack()

        button1 = ttk.Button(self, text="Visit page 1",
                command=lambda: controller.show_frame(PageOne))
        button1.pack()

class PageThree(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text="Page three")
        label.pack(padx=10, pady=10)

        button = ttk.Button(self, text="Home",
                command=lambda: controller.show_frame(StartPage))
        button.pack()

        # Data to plot
        #f = Figure(figsize=(5, 5), dpi=100)
        #a = f.add_subplot(111)
        #a.plot([0,1,2,3,4,5,6,7,8], [0,2,7,3,5,1,7,8,2])

        # Create a simple canvas
        canvas = FigureCanvasTkAgg(f, self)
        canvas.show()
        canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)

        # Navigation bar
        toolbar = NavigationToolbar2TkAgg(canvas, self)
        toolbar.update()
        canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

def animate(i):

    pull_data = open("sample_data.txt", "r").read()
    data_list = pull_data.split("\n")
    x_list = []
    y_list = []

    for each_line in data_list:
        if len(each_line) > 1:
            x, y = each_line.split(",")
            x_list.append(int(x))
            y_list.append(int(y))

    a.clear()
    a.plot(x_list, y_list)


def main():
    """Main program"""

    app = SeaOfBTCApp()
    page_refresh = animation.FuncAnimation(f, animate, interval=1000)
    app.mainloop()

if __name__ == "__main__":
    # Call main when run as script
    main()
