import pickle
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton,
                             QHBoxLayout, QVBoxLayout, QApplication, QLabel,
                             QComboBox, QTextEdit, QLineEdit)
from PyQt5.QtCore import Qt


class ScoreDB(QWidget):
    def __init__(self):
        super().__init__()

        self.dbfilename = 'assignment6.dat'
        self.scdb = []
        self.scdb = self.readScoreDB()
        self.initUI()

    def initUI(self):
        # ??? ?
        self.namelabel = QLabel("Name:")
        self.agelabel = QLabel("Age:")
        self.scorelabel = QLabel("Score:")
        self.namebox = QLineEdit(self)
        self.agebox = QLineEdit(self)
        self.scorebox = QLineEdit(self)
        self.h1layout = QHBoxLayout()
        self.h1layout.addWidget(self.namelabel)
        self.h1layout.addWidget(self.namebox)
        self.h1layout.addWidget(self.agelabel)
        self.h1layout.addWidget(self.agebox)
        self.h1layout.addWidget(self.scorelabel)
        self.h1layout.addWidget(self.scorebox)

        # ??? ?
        self.amountlabel = QLabel("Amount:")
        self.amountbox = QLineEdit(self)
        self.keylabel = QLabel("Key:")
        self.keybox = QComboBox()
        self.keybox.addItem('Name')
        self.keybox.addItem('Age')
        self.keybox.addItem('Score')
        self.h2layout = QHBoxLayout()
        self.h2layout.addStretch(1)
        self.h2layout.addWidget(self.amountlabel)
        self.h2layout.addWidget(self.amountbox)
        self.h2layout.addWidget(self.keylabel)
        self.h2layout.addWidget(self.keybox)

        # ??? ?
        self.addbutton = QPushButton("Add")
        self.delbutton = QPushButton("Del")
        self.findbutton = QPushButton("Find")
        self.incbutton = QPushButton("Inc")
        self.showbutton = QPushButton("Show")
        self.h3layout = QHBoxLayout()
        self.h3layout.addStretch(0.5)
        self.h3layout.addWidget(self.addbutton)
        self.h3layout.addWidget(self.delbutton)
        self.h3layout.addWidget(self.findbutton)
        self.h3layout.addWidget(self.incbutton)
        self.h3layout.addWidget(self.showbutton)
        self.showbutton.clicked.connect(self.showResult)
        self.addbutton.clicked.connect(self.addResult)
        self.delbutton.clicked.connect(self.delResult)
        self.findbutton.clicked.connect(self.findResult)
        self.incbutton.clicked.connect(self.incResult)

        # ??? ?
        self.resultlabel = QLabel("Result:")
        self.h4layout = QHBoxLayout()
        self.h4layout.addWidget(self.resultlabel)

        # ???? ?
        self.resultbox = QTextEdit()
        self.h5layout = QHBoxLayout()
        self.h5layout.addWidget(self.resultbox)

        self.vbox = QVBoxLayout()
        self.vbox.addLayout(self.h1layout)
        self.vbox.addLayout(self.h2layout)
        self.vbox.addLayout(self.h3layout)
        self.vbox.addLayout(self.h4layout)
        self.vbox.addLayout(self.h5layout)

        self.setLayout(self.vbox)

        self.setGeometry(400, 400, 600, 350)
        self.setWindowTitle('Assignment6')
        self.show()

    # result
    def showResult(self):

        keyname = self.keybox.currentText()
        self.showScoreDB(keyname)

    def addResult(self):

        self.addScoreDB()

    def delResult(self):

        self.delScoreDB()

    def findResult(self):

        self.findScoreDB()

    def incResult(self):

        self.incScoreDB()

    # ///
    def closeEvent(self, event):

        self.writeScoreDB()

    def readScoreDB(self):
        try:
            fH = open(self.dbfilename, 'rb')
        except FileNotFoundError as e:

            self.scdb = []
            return
        self.scdb = []
        try:
            self.scdb = pickle.load(fH)
        except:
            pass
        else:
            pass
        fH.close()
        return self.scdb

    # write the data into person db
    def writeScoreDB(self):
        fH = open(self.dbfilename, 'wb')
        pickle.dump(self.scdb, fH)
        fH.close()


    # scoreDB
    def showScoreDB(self, keyname):