# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'evaluate.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox
import module
import sqlite3
MyCricket=sqlite3.connect('Database_cricket.db')
curs=MyCricket.cursor()

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(589, 496)
        Dialog.setStyleSheet("background-color: rgb(199, 199, 199);")
        self.title = QtWidgets.QLabel(Dialog)
        self.title.setGeometry(QtCore.QRect(100, 30, 401, 20))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(True)
        font.setItalic(True)
        font.setWeight(75)
        self.title.setFont(font)
        self.title.setObjectName("title")
        self.combo_team = QtWidgets.QComboBox(Dialog)
        self.combo_team.setGeometry(QtCore.QRect(90, 80, 131, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(9)
        self.combo_team.setFont(font)
        self.combo_team.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.combo_team.setObjectName("combo_team")
        self.combo_team.addItem("")
        self.combo_match = QtWidgets.QComboBox(Dialog)
        self.combo_match.setGeometry(QtCore.QRect(370, 80, 141, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(9)
        self.combo_match.setFont(font)
        self.combo_match.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.combo_match.setObjectName("combo_match")
        self.combo_match.addItem("")
        self.line = QtWidgets.QFrame(Dialog)
        self.line.setGeometry(QtCore.QRect(60, 129, 471, 31))
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(70, 160, 111, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(340, 160, 81, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.list_Players = QtWidgets.QListWidget(Dialog)
        self.list_Players.setGeometry(QtCore.QRect(70, 201, 191, 221))
        font = QtGui.QFont()
        font.setFamily("Arial")
        self.list_Players.setFont(font)
        self.list_Players.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.list_Players.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.list_Players.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.list_Players.setObjectName("list_Players")
        self.list_Points = QtWidgets.QListWidget(Dialog)
        self.list_Points.setGeometry(QtCore.QRect(335, 200, 191, 221))
        font = QtGui.QFont()
        font.setFamily("Arial")
        self.list_Points.setFont(font)
        self.list_Points.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.list_Points.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.list_Points.setObjectName("list_Points")
        self.btn_Score = QtWidgets.QPushButton(Dialog)
        self.btn_Score.setGeometry(QtCore.QRect(210, 440, 181, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(11)
        self.btn_Score.setFont(font)
        self.btn_Score.setObjectName("btn_Score")

        self.list_name=[]

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

        self.btn_Score.clicked.connect(self.display_score)

        #fetching the team names from table
        curs.execute("SELECT DISTINCT name FROM teams;")
        result=curs.fetchall()
        for i in result:
            self.combo_team.addItem(i[0])
            self.list_name.append(i[0])

        self.combo_match.addItem("Match1")
        self.combo_match.currentTextChanged.connect(self.load_names)
        self.combo_match.currentTextChanged.connect(self.load_names)


    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.title.setText(_translate("Dialog", "Evalute the Performance of your fantasy"))
        self.combo_team.setItemText(0, _translate("Dialog", "select team"))
        self.combo_match.setItemText(0, _translate("Dialog", "select match"))
        self.label.setText(_translate("Dialog", "Players"))
        self.label_2.setText(_translate("Dialog", "Points"))
        self.btn_Score.setText(_translate("Dialog", "Calculate Score"))

    def load_names(self):
        selected_team=self.combo_team.currentText()
        selected_match=self.combo_match.currentText()
        if selected_match != "select match":
            self.list_Players.clear()
            self.list_Points.clear()
            curs.execute("SELECT Players FROM teams WHERE Name='"+selected_team+"';")
            result=curs.fetchall()
               #curs.execute("SELECT Value FROM match WHERE Name='"+selected_team+"';")     
            curs.execute("SELECT * FROM match;")
            players=curs.fetchall()
            player_Points={}
            for i in players:
                player_Points[i[0]]=module.calculate_Points(i)
            self.sum=0
            if(result):
                for i in result:
                    name=i[0]
                    self.list_Players.addItem(i[0])
                    self.list_Points.addItem(str(int(player_Points[name])))       
                    self.sum+=int(player_Points[name])
    
            else:
                msg=QMessageBox()
                msg.setWindowTitle("ERROR!!!1")
                msg.setText("Please select the team name")
                msg.exec()
                msg.close()
        else:
            msg=QMessageBox()
            msg.setWindowTitle("ERROR!!!1")
            msg.setText("Please select the match")
            msg.exec()
            msg.close()

    def display_score(self):
        msg=QMessageBox()
        msg.setWindowTitle("Score")
        msg.setText("Your Score is "+str(self.sum))
        msg.exec()
        msg.close()




if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())
