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

# ***************************************************************************
#                                                                           *
# Copyright (C) 2019  Jose Fernando Diaz Benavides                          *
#                                                                           *
# Licensed under the Apache License, Version 2.0 (the "License");           *
# you may not use this file except in compliance with the License.          *
# You may obtain a copy of the License at                                   *
#                                                                           *
#    http://www.apache.org/licenses/LICENSE-2.0                             *
#                                                                           *
# Unless required by applicable law or agreed to in writing, software       *
# distributed under the License is distributed on an "AS IS" BASIS,         *
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
# See the License for the specific language governing permissions and       *
# limitations under the License.                                            *
# ***************************************************************************

# 2019-07-04

# Standard functions
# Numpy - Matplotlib - Tkinter
import numpy as np
import matplotlib.pyplot as plt
from tkinter import *
from tkinter import messagebox

# Own Fuctions
from harmGen            import *
from transformLibrary   import *
from visualization      import *
from preSettings        import preSet # Optional: copy another PreSet.py file with new default values to calculate

# === GUI ===


# Window

window = Tk()

# Menu Bar

def aboutMsg():

        content = ['Alpha–beta transformation',
              'of harmonics v1.0',
               ' '   ,
               'Jose Fernando Diaz Benavides',
              'Copyright © 2019',
               '  ',
              'This program is licenced under Apache 2.0']
        messagebox.showinfo('About', "\n".join(content))

menubar  = Menu(window)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=aboutMsg)
menubar.add_cascade(label="Help", menu=helpmenu)
window.config(menu=menubar)

# Put Labeles
putLabels(window)

# Put Entries
entries(window)

# Put Grid
putGrid(window)

# Pre-settings
preSet(window)


def calculate():

        plt.close()

        # Read Inputs

        fs          = float(window.efs.get())         # Sampling frequency
        time_total  = float(window.etime_total.get()) # Total sampled time
        time        = timeVector(fs,time_total)

        # Signal 1
        #L1
        a0_a        = float(window.e_a0_a.get())
        a0_ph       = float(window.e_a0_ph.get())
        a0_f        = float(window.e_a0_f.get())
        a0_dc       = float(window.e_a0_dc.get())

        #L2
        b0_a        = float(window.e_b0_a.get())
        b0_ph       = float(window.e_b0_ph.get())
        b0_f        = float(window.e_b0_f.get())
        b0_dc       = float(window.e_b0_dc.get())

        #L3
        c0_a        = float(window.e_c0_a.get())
        c0_ph       = float(window.e_c0_ph.get())
        c0_f        = float(window.e_c0_f.get())
        c0_dc       = float(window.e_c0_dc.get())


        # Signal 2
        #L1
        a1_a        = float(window.e_a1_a.get())
        a1_ph       = float(window.e_a1_ph.get())
        a1_f        = float(window.e_a1_f.get())
        a1_dc       = float(window.e_a1_dc.get())

        #L2
        b1_a        = float(window.e_b1_a.get())
        b1_ph       = float(window.e_b1_ph.get())
        b1_f        = float(window.e_b1_f.get())
        b1_dc       = float(window.e_b1_dc.get())

        #L3
        c1_a        = float(window.e_c1_a.get())
        c1_ph       = float(window.e_c1_ph.get())
        c1_f        = float(window.e_c1_f.get())
        c1_dc       = float(window.e_c1_dc.get())


        # Signal 3
        #L1
        a2_a        = float(window.e_a2_a.get())
        a2_ph       = float(window.e_a2_ph.get())
        a2_f        = float(window.e_a2_f.get())
        a2_dc       = float(window.e_a2_dc.get())

        #L2
        b2_a        = float(window.e_b2_a.get())
        b2_ph       = float(window.e_b2_ph.get())
        b2_f        = float(window.e_b2_f.get())
        b2_dc       = float(window.e_b2_dc.get())

        #L3
        c2_a        = float(window.e_c2_a.get())
        c2_ph       = float(window.e_c2_ph.get())
        c2_f        = float(window.e_c2_f.get())
        c2_dc       = float(window.e_c2_dc.get())

        # === LIST OF PARAMETERS ===

        # List of Parameters: amplitude,phase,frequency,time vector, dc

        a_0 = [a0_a,a0_ph,a0_f,time,a0_dc]   # L1 Signal 1
        b_0 = [b0_a,b0_ph,b0_f,time,b0_dc]   # L2
        c_0 = [c0_a,c0_ph,c0_f,time,c0_dc]   # L3

        a_1 = [a1_a,a1_ph,a1_f,time,a1_dc]   # Signal 2
        b_1 = [b1_a,b1_ph,b1_f,time,b1_dc]
        c_1 = [c1_a,c1_ph,c1_f,time,c1_dc]

        a_2 = [a2_a,a2_ph,a2_f,time,a2_dc]   # Signal 3
        b_2 = [b2_a,b2_ph,b2_f,time,b2_dc]
        c_2 = [c2_a,c2_ph,c2_f,time,c2_dc]


        ############# CALCULATE ##################

        # === SIGNAL 0 ===

        a_s_0 = sinusSignal(a_0)
        b_s_0 = sinusSignal(b_0)
        c_s_0 = sinusSignal(c_0)

        # === SIGNAL 1  ===

        a_s_1 = sinusSignal(a_1)
        b_s_1 = sinusSignal(b_1)
        c_s_1 = sinusSignal(c_1)

        # === SIGNAL 2 ===

        a_s_2 = sinusSignal(a_2)
        b_s_2 = sinusSignal(b_2)
        c_s_2 = sinusSignal(c_2)

        # === ADD OS ===

        a_total = a_s_0 + a_s_1 + a_s_2
        b_total = b_s_0 + b_s_1 + b_s_2
        c_total = c_s_0 + c_s_1 + c_s_2

        # === ALPHA BETTA GAMMA ===

        alpha, beta, gamma, amplitude, angle = clarkeTransformation(a_total,b_total,c_total)

        # === PLOT ===

        plotAll(a_total,b_total,c_total,alpha, beta, gamma, amplitude, angle, time)

        # === CALCULATE END ===

# Button

window.button = Button( master  = window,
                        text    = 'Show plots',
                        command = calculate)
window.button.grid(row=20, column=3)

window.mainloop()