import DB_manager3, sys

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_ResultsManager(QtGui.QWidget):
    def __init__(self, database, tableName):
        QtGui.QWidget.__init__(self)
        self.dbu = DB_manager3.DatabaseTool(database, tableName)
        self.setupUi(self)
        self.UpdateTree()
        self.UpdateCombo()
    def setupUi(self, ResultsManager):
        ResultsManager.setObjectName(_fromUtf8("ResultsManager"))
        ResultsManager.setFixedSize(742, 229)
        self.labelhome = QtGui.QLabel(ResultsManager)
        self.labelhome.setGeometry(QtCore.QRect(530, 10, 92, 17))
        self.labelhome.setObjectName(_fromUtf8("labelhome"))
        self.labelmessage = QtGui.QLabel(ResultsManager)
        self.labelmessage.setGeometry(QtCore.QRect(540, 90, 151, 20))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.labelmessage.setFont(font)
        self.labelmessage.setStyleSheet(_fromUtf8("color: rgb(255, 0, 0);"))
        self.labelmessage.setText(_fromUtf8(""))
        self.labelmessage.setObjectName(_fromUtf8("labelmessage"))
        self.homename = QtGui.QComboBox(ResultsManager)
        self.homename.setGeometry(QtCore.QRect(520, 30, 91, 27))
        self.homename.setObjectName(_fromUtf8("homename"))
        self.deletebutton = QtGui.QPushButton(ResultsManager)
        self.deletebutton.setGeometry(QtCore.QRect(480, 150, 261, 27))
        self.deletebutton.setObjectName(_fromUtf8("deletebutton"))


        self.addbutton = QtGui.QPushButton(ResultsManager)
        self.addbutton.setGeometry(QtCore.QRect(480, 110, 261, 27))
        self.addbutton.setObjectName(_fromUtf8("addbutton"))
        self.visitorname = QtGui.QComboBox(ResultsManager)
        self.visitorname.setGeometry(QtCore.QRect(650, 30, 91, 27))
        self.visitorname.setObjectName(_fromUtf8("visitorname"))
        self.labelteam_2 = QtGui.QLabel(ResultsManager)
        self.labelteam_2.setGeometry(QtCore.QRect(620, 40, 47, 17))
        self.labelteam_2.setObjectName(_fromUtf8("labelteam_2"))
        self.labelvisitor = QtGui.QLabel(ResultsManager)
        self.labelvisitor.setGeometry(QtCore.QRect(660, 10, 92, 17))
        self.labelvisitor.setObjectName(_fromUtf8("labelvisitor"))
        self.labelresult = QtGui.QLabel(ResultsManager)
        self.labelresult.setGeometry(QtCore.QRect(500, 190, 201, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.labelresult.setFont(font)
        self.labelresult.setStyleSheet(_fromUtf8("color: rgb(0, 170, 0);"))
        self.labelresult.setText(_fromUtf8(""))
        self.labelresult.setObjectName(_fromUtf8("labelresult"))
        self.treeWidget = QtGui.QTreeWidget(ResultsManager)
        self.treeWidget.setGeometry(QtCore.QRect(10, 10, 451, 211))
        self.treeWidget.setObjectName(_fromUtf8("treeWidget"))
        self.treeWidget.headerItem().setText(0, _fromUtf8("1"))
        self.homescore = QtGui.QLineEdit(ResultsManager)
        self.homescore.setGeometry(QtCore.QRect(520, 60, 91, 27))
        self.homescore.setObjectName(_fromUtf8("homescore"))
        self.visitorscore = QtGui.QLineEdit(ResultsManager)
        self.visitorscore.setGeometry(QtCore.QRect(650, 60, 91, 27))
        self.visitorscore.setObjectName(_fromUtf8("visitorscore"))
        self.label = QtGui.QLabel(ResultsManager)
        self.label.setGeometry(QtCore.QRect(470, 30, 66, 21))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(ResultsManager)
        self.label_2.setGeometry(QtCore.QRect(470, 60, 66, 21))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.labelteam_3 = QtGui.QLabel(ResultsManager)
        self.labelteam_3.setGeometry(QtCore.QRect(620, 60, 47, 17))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.labelteam_3.setFont(font)
        self.labelteam_3.setObjectName(_fromUtf8("labelteam_3"))

        self.retranslateUi(ResultsManager)
        QtCore.QMetaObject.connectSlotsByName(ResultsManager)

    def retranslateUi(self, ResultsManager):
        ResultsManager.setWindowTitle(_translate("ResultsManager", "ResultsManager", None))
        self.labelhome.setText(_translate("ResultsManager", "Home", None))
        self.deletebutton.setText(_translate("ResultsManager", "Delete", None))
        self.addbutton.setText(_translate("ResultsManager", "Add", None))
        self.labelteam_2.setText(_translate("ResultsManager", " Vs", None))
        self.labelvisitor.setText(_translate("ResultsManager", "Visitor", None))
        self.label.setText(_translate("ResultsManager", "Teams", None))
        self.label_2.setText(_translate("ResultsManager", "Scores", None))
        self.labelteam_3.setText(_translate("ResultsManager", " :", None))



    @QtCore.pyqtSignature("on_addbutton_clicked()")
    def addbuttonf(self):
        if((self.homename.currentText() != 'Choose Team') and (self.visitorname.currentText() != 'Choose Team') and (self.homename.currentText() != self.visitorname.currentText())):
            Home = self.homename.currentText()
            Visitor = self.visitorname.currentText()
            Hscore = self.homescore.text()
            Vscore = self.visitorscore.text()
            self.dbu.AddEntryToTable(Home,Hscore,Vscore,Visitor)
            self.UpdateTree()
            self.labelresult.setText(_translate("ResultsManager", "Done!", None))
            self.labelresult.setStyleSheet('QLabel#label {color: green}')
        else :
            self.labelmessage.setText(_translate("ResultsManager", "Fill The Form!", None))
            self.labelmessage.setStyleSheet('QLabel#label {color: red}')
    
    @QtCore.pyqtSignature("on_deletebutton_clicked()")
    def delbutton(self):
        idl = self.treeWidget.currentItem()
        idd = idl.text(4)
        self.dbu.DelEntry(str(idd))
        self.UpdateTree()    

    def UpdateTree(self):
        col = self.dbu.GetColumns()
        table = self.dbu.GetTable()
        
        for c in range(len(col)):
            self.treeWidget.headerItem().setText(c, col[c][0])
        
        self.treeWidget.clear()
        
        for item in range(len(table)):
            QtGui.QTreeWidgetItem(self.treeWidget)
            for value in range(len(table[item])):
                self.treeWidget.topLevelItem(item).setText(value, str(table[item][value]))

    def UpdateCombo(self):
        c = []
        f = open('teams.txt','r')
        line = f.readlines()
        for i in range(len(line)):
            x = line[i]
            c.append(x[:len(x)-1])
        f.close()
        self.homename.clear()
        self.visitorname.clear()
        self.homename.addItems(c)
        self.visitorname.addItems(c)