"""
AIRLINE RESERVATION SYSTEM
DEVELOPERS
    Stephen Tafoya
    Ryan Hamilton

**** All comments will have ST or RH for ****
**** Stephen Tafoya or Ryan Hamilton     ****

GUI CREATION FILE - CREATES AND HANDLES GUI ELEMENTS
"""

# class WindowObject()


###############################
# IMPORT STATEMENTS
#       This handles all imports required for
#       the GUI to function.
###############################
from mainGui import mainGui                 # ST import mainGui
from mainGui import window_edit             # ST import window_edit
from mainGui import intro                   # ST import intro

import locale                               # ST for money format

from PyQt5 import QtWidgets as QWgts        # ST import QtWidgets (for gui)
# ST import QtMultimedia (for sound)
from PyQt5 import QtMultimedia as QMedia
# ST import QtGui (for animated gif)
from PyQt5 import QtGui
# ST import QtCore (to hide borders for animation)
from PyQt5 import QtCore


locale.setlocale(locale.LC_ALL, '')  # ST set localtime


def findTbl(tbl, item):  # ST simple find in table function
    """
        CODER: Stephen Tafoya
        FUNCTION:
            Used by several Querries from sql database
            returns if item is found
    """
    for index in tbl:  # ST itterate each item
        if index == item:  # ST check if index is item
            return True  # ST return True

    return False  # ST return False

###############################
# INTRO_WINDOW
#   programmer: Ryan Hamilton
#   desc: Create an object to handle intro gui
#   Documentation:
#       Creates the intro window, handles display of gif image and playing of audio file
#       Closes and runs main window. takes main window as an argument
###############################


class INTRO_WINDOW(QWgts.QMainWindow, intro.Ui_MainWindow):
    def __init__(self, master, parent=None):
        """
            CODER: Stephen Tafoya
            Function called on creation of an
                Intro_Window
        """
        super(INTRO_WINDOW, self).__init__(parent)  # ST
        self.setupUi(self)  # ST

        self.master = master  # ST stores master

        # ST make it borderless
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        # add playable gif animation
        self.movie = QtGui.QMovie(
            "resource/planecrash.gif")  # RH load animated

        # RH apply to viewarea
        self.ViewArea.setMovie(self.movie)
        # RH change gif animation speed
        self.movie.setSpeed(100)
        self.movie.start()  # RH start the movie playback
        self.movie.setScaledSize(QtCore.QSize(500, 500))  # RH setScale

        self.PlaneCrashSound = QMedia.QSound(
            "resource/introswell.wav")  # RH load a QSound from Resource
        self.PlaneCrashSound.play()  # RH make sound play

        self.timer = QtCore.QTimer(self)  # ST create timer
        self.timer.setInterval(4000)      # ST make it sleep for 4 seconds
        # ST connect its timeout function
        self.timer.timeout.connect(self.finishtimer)
        self.timer.start()            # ST start timer

    def finishtimer(self):  # called on finish timer
        self.timer.stop()  # stops timer
        self.hide()  # hides widget
        self.master.show()  # shows master form


###############################
# COMBOBOX
#   programmer: Stephen Tafoya
#   desc: combobox class
#   Documentation:
#       created to handle currentindexchanged
#       used in the qtablewidget to handle updates.
###############################

class COMBOBOX(QWgts.QComboBox):
    def __init__(self, index, master):  # ST
        # ST call parent function
        super(COMBOBOX, self).__init__()
        self.index = index  # ST store index
        self.master = master  # ST store master
        super(COMBOBOX, self).currentIndexChanged[
            int].connect(self._currentIndexChanged)  # ST connect parent function

    # ST accessor for index
    def getPersonIndex(self):
        return self.index  # ST

    # ST cloned parent function
    def _currentIndexChanged(self, index):
        self.master.personList[self.index].setSeatType(index)  # ST changes value in person class


###############################
# COMBOBOXSEAT
#   programmer: Stephen Tafoya
#   desc: combobox class
#   Documentation:
#       created to handle currentindexchanged
#       inherets from COMBOBOX
#       used to keep track of seating in qtablewidget
###############################

class COMBOBOXSEAT(COMBOBOX):  # ST
    def __init__(self, index, master):  # ST
        super(COMBOBOXSEAT, self).__init__(
            index, master)  # ST call parent function

    def _currentIndexChanged(self, index):  # ST
        self.master.personList[self.index].setIsIsle(
            {1: True, 0: False}[index])  # ST


###############################
# SPINBOX
#   programmer: Stephen Tafoya
#   desc: spinbox class
#   Documentation:
#       created to handle currentindexchanged
#       inherets from qspinbox
###############################

class SPINBOX(QWgts.QSpinBox):  # ST
    def __init__(self, index, master=None):  # ST call parent function
        super(SPINBOX, self).__init__()  # ST
        self.index = index  # ST
        self.master = master  # ST
        super(SPINBOX, self).valueChanged.connect(self._valueChanged)  # ST

    def _valueChanged(self):  # ST
        self.master.personList[self.index].setBags(self.value())  # ST


###############################
# NAMELINEEDIT
#   programmer: Stephen Tafoya
#   desc: spinbox class
#   Documentation:
#       Handle The name box for the tablewidget
#       simplify by putting 3 widgets in a layout together
#       adds 3 lineedits
###############################

class NAMELINEEDIT(QWgts.QWidget):  # ST
    def __init__(self, index, master, parent=None):  # ST
        super(NAMELINEEDIT, self).__init__(parent)  # 
ST

        self.index = index  # ST define index
        self.master = master  # ST define master

        # ST create a Horizonatl Layout
        layout = QWgts.QHBoxLayout()

        layout.setContentsMargins(0, 0, 0, 0)  # ST remove margins
        layout.setSpacing(0)  # ST remove spacing

        # ST add all three lineedit boxes
        self.firstN = QWgts.QLineEdit()
        self.middlN = QWgts.QLineEdit()  # ST
     
   self.lastN = QWgts.QLineEdit()  # ST

        # Connect all change functions
        self.firstN.textChanged.connect(self.fn_changed)  # ST
        self.middlN.textChanged.connect(self.mn_changed)  # ST
        self.lastN.textChanged.connect(self.ln_changed)  # ST

        # ST insert widgets to layout
        layout.addWidget(self.firstN)
        layout.addWidget(self.middlN)  # ST
        layout.addWidget(self.lastN)  # ST

        # ST apply the layout to this
        self.setLayout(layout)

    def fn_changed(self, str):                              # ST if first name changes
        self.master.personList[self.index].setFName(str)    # ST update person

    def mn_changed(self, str):                              # ST if middle name changes
        self.master.personList[self.index].setMName(str)    # ST update person

    def ln_changed(self, str):                              # ST if last name changes
        self.master.personList[self.index].setLName(str)    # ST update person

    def setName(self, f, m, l):  # ST applies all names to line edit boxes
        self.firstN.setText(f)  # ST
        self.middlN.setText(m)  # ST
        self.lastN.setText(l)  # ST


###############################
# MAIN_WINDOW
#   programmer: Stephen Tafoya and Ryan Hamilton
#   desc: Create a main object to handle gui creation
#   Documentation:
#       Uses several custom classes,
#       has 4 pages, page 1 handles flight details
#       page 2 handles passenger reservations
#       page 3 handles contact info and special instructions
#       page 4 handles payment and finish
###############################

class MAIN_WINDOW(QWgts.QMainWindow, mainGui.Ui_MainWindow):  # ST

    def __init__(self, database, parent=None):  # ST
        super(MAIN_WINDOW, self).__init__(parent)  # ST
        self.setupUi(self)  # ST

        self.db = database[0]  # ST  loa