# The code for changing pages was derived from: http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
# License: http://creativecommons.org/licenses/by-sa/3.0/	

from database import Table
from analogPressure.sdpArray import SdpArray
from analogPressure.mcp3008 import MCP3008
from digitalPressure.sdp610Array import Spd610Array
from wirelessLoadCell.loadCells import LoadCells
from ui import *

import tkinter as tk
import tk_tools
from time import *


class Main(tk.Tk, Table):
  def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)
    tk.Tk.wm_title(self, "Windkanal-Tool")

    self.adc = MCP3008(0,0)
    self.pressureSensors = Spd610Array()
    self.forceSensors = LoadCells()
    self.forceSensors.start()
    self.motorController = None
    
    Table.__init__(self, 
    ["time", "windspeed", "motor_pwm"] + 
    [f"pressure_{i}" for i in range(8)] + 
    [f"adc_{i}" for i in range(1)] + 
    [f"force_X_1", f"force_Y_1", f"force_Z_1"] +
    [f"force_X_2", f"force_Y_2", f"force_Z_2"] + 
    [f"force_X_3", f"force_Y_3", f"force_Z_3"])
    
    self.saveAsCsv("test.csv")

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


    menubar = tk.Menu(container)
    filemenu = tk.Menu(menubar, tearoff=0)
    filemenu.add_command(label="Save settings", command = lambda: self.popupmsg("Not supported just yet!"))
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=quit)
    menubar.add_cascade(label="File", menu=filemenu)

    tk.Tk.config(self, menu=menubar)

    label = tk.Label(self, text="Bedienelemente", font=LARGE_FONT)
    label.pack(pady=10,padx=10)
    # top menu
    top = tk.Frame(self, borderwidth=2, relief="solid")
    button1 = tk.Button(top, text="Bedienelemente", command=lambda: self.show_frame(Page_1))
    button1.pack(side=tk.LEFT)

    button2 = tk.Button(top, text="Kräfte", command=lambda: self.show_frame(Page_2))
    button2.pack(side=tk.LEFT)

    button3 = tk.Button(top, text="Druck", command=lambda: self.show_frame(Page_3))
    button3.pack(side=tk.LEFT)

    button4 = tk.Button(top,text="Einstellungen",command=lambda: self.show_frame(Page_4))
    button4.pack(side=tk.LEFT)

    button5 = tk.Button(top, text="QUIT", fg="red",command=quit)
    button5.pack(side=tk.LEFT)
    top.pack(side="top", expand=True, fill="both")

    self.frames = {}

    for F in (Page_1, Page_2, Page_3, Page_4):

      frame = F(container, self)

      self.frames[F] = frame

      frame.grid(row=0, column=0, sticky="nsew")

    self.show_frame(Page_1)

    self.interval()

  def show_frame(self, cont):
    frame = self.frames[cont]
    frame.tkraise()

  def popupmsg(self, msg=""):
      popup = tk.Toplevel(self.master)
      popup.wm_title("Error!")
      label = tk.Label(popup, text=msg, font=LARGE_FONT)
      label.pack(side="top", fill="x", pady=10)
      b1 = tk.Button(popup, text="Okay", command=popup.destroy)
      b1.pack()
  def interval(self):
    adcValues = self.adc.read()

    self.addRow(
      [time.time(), 0, 0] + 
      self.pressureSensors.getValues() +
      [self.adc.getVoltage(0)] +
      self.forceSensors.getForces(0) +
      self.forceSensors.getForces(1) +
      self.forceSensors.getForces(2)
    )
    for frame in self.frames:
      frame.update()

    self.after(300,self.interval)

          