# PC 204 Final Project
# Python 3.4

import module1, module2, module3
from tkinter import *
import numpy as np
import pylab as pl
import csv

#Model A is for one compartment, direct model with oral absorption.
#need to enter parameters to define the one compartment model with oral
#absorption.
#keo is for direct model

#Model B is for two compartment model.  No effect compartment.

value = []

#action for model A (module 1)
def action_a():

    def calculate():
        a = module1.A(entry1.get(),entry2.get(),entry3.get(),
                        entry4.get(),entry5.get(), entry6.get())
        Matrix = a.calculate()
        new_window.destroy()
        value = final_calculation(Matrix)
        save_and_plot(value)

    new_window = Tk()
    new_window.title("Enter numbers")
    prompt.destroy()

    label = Label(new_window, text="La", relief=RAISED, width = 20)
    labe2 = Label(new_window, text="L2", relief=RAISED, width = 20)
    labe3 = Label(new_window, text="ka", relief=RAISED, width = 20)
    labe4 = Label(new_window, text="k", relief=RAISED, width = 20)
    labe5 = Label(new_window, text="keo", relief=RAISED, width = 20)
    labe6 = Label(new_window, text="tau", relief=RAISED, width = 20)

    entry1 = Entry(new_window, width=25)
    entry2 = Entry(new_window, width=25)
    entry3 = Entry(new_window, width=25)
    entry4 = Entry(new_window, width=25)
    entry5 = Entry(new_window, width=25)
    entry6 = Entry(new_window, width=25)

    label.grid(row=0, column=0)
    entry1.grid(row=0, column=1)
    labe2.grid(row=1, column=0)
    entry2.grid(row=1, column=1)
    labe3.grid(row=2, column=0)
    entry3.grid(row=2, column=1)
    labe4.grid(row=3, column=0)
    entry4.grid(row=3, column=1)
    labe5.grid(row=4, column=0)
    entry5.grid(row=4, column=1)
    labe6.grid(row=5, column=0)
    entry6.grid(row=5, column=1)

    button = Button(new_window, text = "OK", command = calculate, width = 10)
    button.grid(row=6, column=0)
    #print(button)

#action for model B (module 2)
def action_b():

    def calculate():
        b = module2.B(entry1.get(), entry2.get(), entry3.get(),
                        entry4.get(), entry5.get(), entry6.get(),
                        entry7.get())
        value = b.calculate()
        new_window.destroy()
        value = final_calculation(b.calculate())
        save_and_plot(value)

    new_window = Tk()
    new_window.title("Enter numbers")
    prompt.destroy()

    label = Label(new_window, text="L1", relief=RAISED, width = 20)
    labe2 = Label(new_window, text="L2", relief=RAISED, width = 20)
    labe3 = Label(new_window, text="La", relief=RAISED, width = 20)
    labe4 = Label(new_window, text="lambda1", relief=RAISED, width = 20)
    labe5 = Label(new_window, text="lambda2", relief=RAISED, width = 20)
    labe6 = Label(new_window, text="ka", relief=RAISED, width = 20)
    labe7 = Label(new_window, text="tau", relief=RAISED, width = 20)

    entry1 = Entry(new_window, width=25)
    entry2 = Entry(new_window, width=25)
    entry3 = Entry(new_window, width=25)
    entry4 = Entry(new_window, width=25)
    entry5 = Entry(new_window, width=25)
    entry6 = Entry(new_window, width=25)
    entry7 = Entry(new_window, width=25)

    label.grid(row=0, column=0)
    entry1.grid(row=0, column=1)
    labe2.grid(row=1, column=0)
    entry2.grid(row=1, column=1)
    labe3.grid(row=2, column=0)
    entry3.grid(row=2, column=1)
    labe4.grid(row=3, column=0)
    entry4.grid(row=3, column=1)
    labe5.grid(row=4, column=0)
    entry5.grid(row=4, column=1)
    labe6.grid(row=5, column=0)
    entry6.grid(row=5, column=1)
    labe7.grid(row=6, column=0)
    entry7.grid(row=6, column=1)

    button = Button(new_window, text = "OK", command = calculate, width = 10)
    button.grid(row=7, column=0)

#final calculation done by module 3
def final_calculation(data):
    final_set = module3.Final(data)
    return final_set.calculate()

def display_buttons():
    a = Button(prompt, text = "A", command = action_a, width = 10)
    b = Button(prompt, text = "B", command = action_b, width = 10)

    # Message in the text box
    message = "Model A for One-Compartment; model B for Two-Compartment"
    m1 = Message(prompt, text = message, width = 1000)
    m1.pack()
    a.pack(side=LEFT)
    b.pack(side=RIGHT)

def save_and_plot(value):
    CL = [x for x in np.arange(1,3,0.1)]
    if len(value) == 3:
        # Option B save (Option B has 3 matrices as output)
        print(value)
        with open("output_modelB1.csv", "w") as f:
            saved1 = csv.writer(f)
            saved1.writerow(list(value[0]))
        with open("output_modelB2.csv", "w") as f:
            saved2 = csv.writer(f)
            saved2.writerow(list(value[1]))
        with open("output_modelB3.csv", "w") as f:
            saved3 = csv.writer(f)
            saved3.writerow(list(value[2]))
        pl.figure(1)
        pl.subplot(311)
        pl.plot(CL,list(value[0]))
        pl.title("genotype1")
        pl.ylabel("Concentration")

        pl.subplot(312)
        pl.plot(CL,list(value[1]))
        pl.title("genotype2")
        pl.ylabel("Concentration")

        pl.subplot(313)
        pl.plot(CL,list(value[2]))
        pl.title("genotype3")
        pl.ylabel("Concentration")
        pl.xlabel("Serum Creatinine Level[mg/dL]")
        pl.show()
    else:
        # Option A save
        print(value)
        with open("output_modelA.csv", "w") as f:
            saved = csv.writer(f)
            saved.writerow(value)
        pl.plot(CL,value)
        pl.title('Safety Levels vs Renal Clearance')
        pl.ylabel('Concentration [given unit]')
        pl.xlabel('Serum Creatinine Level[mg/dL]')
        pl.show()

if __name__ == "__main__":
    prompt = Tk()
    prompt.title("Choose A or B")
    display_buttons()
    mainloop()