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

# Form implementation generated from reading ui file 'main_window_UI.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
#importing Ui_Dialog from new for opening new window.
from new import Ui_Dialog as new
from open import Ui_Dialog as open_team
from evaluate import Ui_Dialog as evaluate

import sqlite3
MyCricket=sqlite3.connect('Database_cricket.db')
curs=MyCricket.cursor()


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        MainWindow.setStyleSheet("background-color: rgb(255, 255, 255);")
        MainWindow.setAnimated(False)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(20, 11, 751, 71))
        self.frame.setStyleSheet("background-color: rgb(235, 235, 235);")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Plain)
        self.frame.setObjectName("frame")
        self.gridLayout = QtWidgets.QGridLayout(self.frame)
        self.gridLayout.setObjectName("gridLayout")
        self.L_Bat_S = QtWidgets.QLabel(self.frame)
        self.L_Bat_S.setEnabled(False)
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.L_Bat_S.setFont(font)
        self.L_Bat_S.setStyleSheet("color: rgb(0, 170, 255);")
        self.L_Bat_S.setObjectName("L_Bat_S")
        self.gridLayout.addWidget(self.L_Bat_S, 2, 1, 1, 1)
        self.L_Bat = QtWidgets.QLabel(self.frame)
        self.L_Bat.setEnabled(True)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.L_Bat.setFont(font)
        self.L_Bat.setObjectName("L_Bat")
        self.gridLayout.addWidget(self.L_Bat, 2, 0, 1, 1)
        self.L_Ar_S = QtWidgets.QLabel(self.frame)
        self.L_Ar_S.setEnabled(False)
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.L_Ar_S.setFont(font)
        self.L_Ar_S.setStyleSheet("\n"
"color: rgb(0, 170, 255);")
        self.L_Ar_S.setObjectName("L_Ar_S")
        self.gridLayout.addWidget(self.L_Ar_S, 2, 5, 1, 1)
        self.L_Bow_S = QtWidgets.QLabel(self.frame)
        self.L_Bow_S.setEnabled(False)
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.L_Bow_S.setFont(font)
        self.L_Bow_S.setStyleSheet("\n"
"color: rgb(0, 170, 255);")
        self.L_Bow_S.setObjectName("L_Bow_S")
        self.gridLayout.addWidget(self.L_Bow_S, 2, 3, 1, 1)
        self.L_Bow = QtWidgets.QLabel(self.frame)
        self.L_Bow.setEnabled(True)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.L_Bow.setFont(font)
        self.L_Bow.setObjectName("L_Bow")
        self.gridLayout.addWidget(self.L_Bow, 2, 2, 1, 1)
        self.L_Ar = QtWidgets.QLabel(self.frame)
        self.L_Ar.setEnabled(True)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.L_Ar.setFont(font)
        self.L_Ar.setObjectName("L_Ar")
        self.gridLayout.addWidget(self.L_Ar, 2, 4, 1, 1)
        self.L_WK_S = QtWidgets.QLabel(self.frame)
        self.L_WK_S.setEnabled(False)
        font = QtGui.QFont()
        font.setPointSize(9)
        self.L_WK_S.setFont(font)
        self.L_WK_S.setStyleSheet("color: rgb(0, 170, 255);")
        self.L_WK_S.setObjectName("L_WK_S")
        self.gridLayout.addWidget(self.L_WK_S, 2, 7, 1, 1)
        self.L_WK = QtWidgets.QLabel(self.frame)
        self.L_WK.setEnabled(True)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.L_WK.setFont(font)
        self.L_WK.setObjectName("L_WK")
        self.gridLayout.addWidget(self.L_WK, 2, 6, 1, 1)
        self.Label_S = QtWidgets.QLabel(self.frame)
        self.Label_S.setEnabled(True)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.Label_S.setFont(font)
        self.Label_S.setOpenExternalLinks(False)
        self.Label_S.setObjectName("Label_S")
        self.gridLayout.addWidget(self.Label_S, 0, 0, 1, 1)
        self.frame_2 = QtWidgets.QFrame(self.centralwidget)
        self.frame_2.setGeometry(QtCore.QRect(20, 90, 751, 41))
        self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_2.setObjectName("frame_2")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.frame_2)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.L_points_Avail = QtWidgets.QLabel(self.frame_2)
        self.L_points_Avail.setEnabled(True)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.L_points_Avail.setFont(font)
        self.L_points_Avail.setObjectName("L_points_Avail")
        self.horizontalLayout.addWidget(self.L_points_Avail)
        self.L_Points = QtWidgets.QLabel(self.frame_2)
        self.L_Points.setEnabled(False)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.L_Points.setFont(font)
        self.L_Points.setStyleSheet("color: rgb(0, 170, 255);")
        self.L_Points.setObjectName("L_Points")
        self.horizontalLayout.addWidget(self.L_Points)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.L_Points_Used = QtWidgets.QLabel(self.frame_2)
        self.L_Points_Used.setEnabled(True)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.L_Points_Used.setFont(font)
        self.L_Points_Used.setObjectName("L_Points_Used")
        self.horizontalLayout.addWidget(self.L_Points_Used)
        self.L_Points1 = QtWidgets.QLabel(self.frame_2)
        self.L_Points1.setEnabled(False)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.L_Points1.setFont(font)
        self.L_Points1.setStyleSheet("color: rgb(0, 170, 255);")
        self.L_Points1.setObjectName("L_Points1")
        self.horizontalLayout.addWidget(self.L_Points1)
        self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(20, 130, 751, 401))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.frame_3 = QtWidgets.QFrame(self.horizontalLayoutWidget)
        self.frame_3.setAutoFillBackground(False)
        self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_3.setObjectName("frame_3")
        self.frame_4 = QtWidgets.QFrame(self.frame_3)
        self.frame_4.setGeometry(QtCore.QRect(420, 10, 311, 391))
        self.frame_4.setAutoFillBackground(False)
        self.frame_4.setFrameShape(QtWidgets.QFrame.Panel)
        self.frame_4.setFrameShadow(QtWidgets.QFrame.Plain)
        self.frame_4.setObjectName("frame_4")
        self.frame_7 = QtWidgets.QFrame(self.frame_4)
        self.frame_7.setGeometry(QtCore.QRect(10, 10, 281, 40))
        self.frame_7.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_7.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_7.setObjectName("frame_7")
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.frame_7)
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.L_TeamName = QtWidgets.QLabel(self.frame_7)
        self.L_TeamName.setEnabled(True)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setBold(True)
        font.setWeight(75)
        self.L_TeamName.setFont(font)
        self.L_TeamName.setObjectName("L_TeamName")
        self.horizontalLayout_4.addWidget(self.L_TeamName)
        self.L_Team_Display = QtWidgets.QLabel(self.frame_7)
        self.L_Team_Display.setEnabled(False)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setBold(True)
        font.setWeight(75)
        self.L_Team_Display.setFont(font)
        self.L_Team_Display.setStyleSheet("color: rgb(0, 170, 255);")
        self.L_Team_Display.setObjectName("L_Team_Display")
        self.horizontalLayout_4.addWidget(self.L_Team_Display)
        self.selected_Players = QtWidgets.QListWidget(self.frame_4)
        self.selected_Players.setGeometry(QtCore.QRect(15, 61, 281, 321))
        self.selected_Players.setFrameShadow(QtWidgets.QFrame.Raised)
        self.selected_Players.setObjectName("selected_Players")
        self.frame_5 = QtWidgets.QFrame(self.frame_3)
        self.frame_5.setGeometry(QtCore.QRect(20, 9, 321, 391))
        self.frame_5.setAutoFillBackground(False)
        self.frame_5.setFrameShape(QtWidgets.QFrame.Panel)
        self.frame_5.setFrameShadow(QtWidgets.QFrame.Plain)
        self.frame_5.setObjectName("frame_5")
        self.frame_6 = QtWidgets.QFrame(self.frame_5)
        self.frame_6.setGeometry(QtCore.QRect(9, 10, 301, 44))
        self.frame_6.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_6.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_6.setObjectName("frame_6")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.frame_6)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.R_Bat = QtWidgets.QRadioButton(self.frame_6)
        self.R_Bat.setEnabled(False)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setBold(True)
        font.setWeight(75)
        self.R_Bat.setFont(font)
        self.R_Bat.setObjectName("R_Bat")
        self.horizontalLayout_3.addWidget(self.R_Bat)
        self.R_Bow = QtWidgets.QRadioButton(self.frame_6)
        self.R_Bow.setEnabled(False)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setBold(True)
        font.setWeight(75)
        self.R_Bow.setFont(font)
        self.R_Bow.setObjectName("R_Bow")
        self.horizontalLayout_3.addWidget(self.R_Bow)
        self.R_AR = QtWidgets.QRadioButton(self.frame_6)
        self.R_AR.setEnabled(False)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setBold(True)
        font.setWeight(75)
        self.R_AR.setFont(font)
        self.R_AR.setObjectName("R_AR")
        self.horizontalLayout_3.addWidget(self.R_AR)
        self.R_WK = QtWidgets.QRadioButton(self.frame_6)
        self.R_WK.setEnabled(False)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setBold(True)
        font.setWeight(75)
        self.R_WK.setFont(font)
        self.R_WK.setObjectName("R_WK")
        self.horizontalLayout_3.addWidget(self.R_WK)
        self.Avail_Players = QtWidgets.QListWidget(self.frame_5)
        self.Avail_Players.setGeometry(QtCore.QRect(15, 70, 291, 301))
        self.Avail_Players.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Avail_Players.setObjectName("Avail_Players")
        self.label = QtWidgets.QLabel(self.frame_3)
        self.label.setGeometry(QtCore.QRect(364, 15, 21, 381))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.horizontalLayout_2.addWidget(self.frame_3)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
        self.menubar.setObjectName("menubar")
        self.menuManage_Teams = QtWidgets.QMenu(self.menubar)
        self.menuManage_Teams.setMouseTracking(True)
        self.menuManage_Teams.setStyleSheet("selection-background-color: rgb(0, 170, 255);")
        self.menuManage_Teams.setTearOffEnabled(False)
        self.menuManage_Teams.setObjectName("menuManage_Teams")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.Menu_new = QtWidgets.QAction(MainWindow)
        self.Menu_new.setEnabled(True)
        self.Menu_new.setShortcut("Ctrl+N")
        self.Menu_new.setAutoRepeat(False)
        self.Menu_new.setVisible(True)
        self.Menu_new.setIconVisibleInMenu(True)
        self.Menu_new.setObjectName("Menu_new")
        self.Menu_Open = QtWidgets.QAction(MainWindow)
        self.Menu_Open.setObjectName("Menu_Open")
        self.Menu_Save = QtWidgets.QAction(MainWindow)
        self.Menu_Save.setObjectName("Menu_Save")
        self.Menu_Evaluate = QtWidgets.QAction(MainWindow)
        self.Menu_Evaluate.setObjectName("Menu_Evaluate")
        self.actionEXIT = QtWidgets.QAction(MainWindow)
        self.actionEXIT.setObjectName("actionEXIT")
        self.menuManage_Teams.addAction(self.Menu_new)
        self.menuManage_Teams.addAction(self.Menu_Open)
        self.menuManage_Teams.addAction(self.Menu_Save)
        self.menuManage_Teams.addAction(self.Menu_Evaluate)
        self.menuManage_Teams.addAction(self.actionEXIT)
        self.menubar.addAction(self.menuManage_Teams.menuAction())

        #triggering Action
        self.Menu_new.triggered.connect(self.new_team)
        self.Menu_Save.triggered.connect(self.save_team)
        self.Menu_Open.triggered.connect(self.open_team)
        self.Menu_Evaluate.triggered.connect(self.evaluate_team)
        self.actionEXIT.triggered.connect(self.quit)
        
        self.R_Bow.toggled.connect(self.display_Players)
        self.R_Bat.toggled.connect(self.display_Players)
        self.R_AR.toggled.connect(self.display_Players)
        self.R_WK.toggled.connect(self.display_Players)

        #creating dictionary for storing player name and their value
        self.value={}

        #creating list
        self.list=[]
        self.ctg_bow=[]
        self.ctg_bat=[]
        self.ctg_ar=[]
        self.ctg_wk=[]

        #event handling for list
        self.Avail_Players.itemDoubleClicked.connect(self.select_Players)
        self.selected_Players.itemDoubleClicked.connect(self.remove_Players)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.L_Bat_S.setText(_translate("MainWindow", "##"))
        self.L_Bat.setText(_translate("MainWindow", "Batsman (BAT)"))
        self.L_Ar_S.setText(_translate("MainWindow", "##"))
        self.L_Bow_S.setText(_translate("MainWindow", "##"))
        self.L_Bow.setText(_translate("MainWindow", "Bowlers (BOW)"))
        self.L_Ar.setText(_translate("MainWindow", "All Rounders (AR)"))
        self.L_WK_S.setText(_translate("MainWindow", "##"))
        self.L_WK.setText(_translate("MainWindow", "Wicket-Keeper (WK)"))
        self.Label_S.setText(_translate("MainWindow", "Your Selections"))
        self.L_points_Avail.setText(_translate("MainWindow", "     Points Available:"))
        self.L_Points.setText(_translate("MainWindow", "####"))
        self.L_Points_Used.setText(_translate("MainWindow", "      Points Used :"))
        self.L_Points1.setText(_translate("MainWindow", "####"))
        self.L_TeamName.setText(_translate("MainWindow", "        Team Name"))
        self.L_Team_Display.setText(_translate("MainWindow", "Displayed Here"))
        self.R_Bat.setText(_translate("MainWindow", "BAT"))
        self.R_Bow.setText(_translate("MainWindow", "BOW"))
        self.R_AR.setText(_translate("MainWindow", "AR"))
        self.R_WK.setText(_translate("MainWindow", "WK"))
        self.label.setText(_translate("MainWindow", " >"))
        self.menuManage_Teams.setTitle(_translate("MainWindow", "Manage Teams"))
        self.Menu_new.setText(_translate("MainWindow", "New Team"))
        self.Menu_Open.setText(_translate("MainWindow", "Open Team"))
        self.Menu_Open.setShortcut(_translate("MainWindow", "Ctrl+O"))
        self.Menu_Save.setText(_translate("MainWindow", "SAVE Team"))
        self.Menu_Save.setShortcut(_translate("MainWindow", "Ctrl+S"))
        self.Menu_Evaluate.setText(_translate("MainWindow", "EVALUATE Team"))
        self.Menu_Evaluate.setShortcut(_translate("MainWindow", "Ctrl+E"))
        self.actionEXIT.setText(_translate("MainWindow", "EXIT"))

    def quit(self):
        msg=QMessageBox()
        msg.setWindowTitle("Message")
        msg.setText("Thank You !!!")
        msg.exec()
        msg.close()
        sys.exit()
        
    #creating team
    def new_team(self):
        self.dialog_new=QtWidgets.QDialog()
        self.dialog_new.ui=new()
        self.dialog_new.ui.setupUi(self.dialog_new)
        self.dialog_new.show()
        self.dialog_new.ui.Btn_Save.clicked.connect(self.check_name)
        
        
    def save_team(self):
        if(self.check_team()):
            try:
                name1=self.L_Team_Display.text()
                curs.execute("SELECT DISTINCT Name FROM teams;")
                x = curs.fetchall()
                for i in x:
                    if name1 == i[0]:   # CHECKING IF THE TEAMNAME ALREADY EXISTS
                        curs.execute("DELETE  FROM teams WHERE Name='" + name1 + "';")
                        MyCricket.commit()
            except:
                print("Error")

            for i in self.list:
                try:
                    curs.execute("INSERT INTO teams (Name,Players,Value)VALUES(?,?,?);",(name1,i,self.value[i]))
                    
                except:
                    msg=QMessageBox()
                    msg.setWindowTitle("error")
                    msg.setText("Error in saving team!!!")
                    msg.exec()
                    msg.close()
                    return 0
            MyCricket.commit()
            msg=QMessageBox()
            msg.setWindowTitle("Message")
            msg.setText("Team saved sucessfully!!!")
            msg.exec()
            msg.close()

    def open_team(self):
        self.dialog_open=QtWidgets.QDialog()
        self.dialog_open.ui=open_team()
        self.dialog_open.ui.setupUi(self.dialog_open)
        self.dialog_open.show()
        self.dialog_open.ui.btn_open.clicked.connect(self.open)

    def evaluate_team(self):
        self.dialog_eva=QtWidgets.QDialog()
        self.dialog_eva.ui=evaluate()
        self.dialog_eva.ui.setupUi(self.dialog_eva)
        self.dialog_eva.show()
        

    def open(self):
        teamname = self.dialog_open.ui.combo_name.currentText()
        self.reset()
        self.L_Team_Display.setText(""+teamname)
        curs.execute("SELECT Players FROM teams WHERE Name='"+teamname+"';")
        players=curs.fetchall()
        score=[]
        for i in players:
            curs.execute("SELECT Value from stats WHERE Player='"+i[0]+"';")
            s=curs.fetchone()
            score.append(s[0])

        sum=0
        for i in score:
            sum+=i
        self.display_Players()
        for i in players:
            self.selected_Players.addItem(i[0])
            self.list.append(i[0])
            self.condition1(i[0])
        self.dialog_open.close()
        
        
    def check_name(self):
        name=self.dialog_new.ui.E_name.text()
        self.team_name=self.dialog_new.ui.E_name.text()
        import sqlite3
        MyCricket=sqlite3.connect('Database_cricket.db')
        curs=MyCricket.cursor()
        sql="SELECT Name from teams;"
        curs.execute(sql)
        result=curs.fetchall()
        if(result):
            for i in result:
                if i[0]==name:
                    msg=QMessageBox()
                    msg.setWindowTitle("Error")
                    msg.setText("Team name already exixts!! Try another name")
                    msg.exec()
                    msg.close()
                    return 0
        if(name.isnumeric()):
            msg=QMessageBox()
            msg.setWindowTitle("Error")
            msg.setText("Enter valid team name!!!")
            msg.exec()
            msg.close()
        elif len(name)==0:
            msg=QMessageBox()
            msg.setWindowTitle("Error")
            msg.setText("Please enter name!!!")
            msg.exec()
            msg.close()
        else:
            self.dialog_new.close()
            self.L_Team_Display.setText(""+name)
            self.reset()

    def reset(self):
        self.batscount=0
        self.bowcount=0
        self.ARcount=0
        self.wktcount=0
        self.avail_points=1000
        self.used_points=0
        self.set_points()
        self.totalcount=0
        self.R_AR.setEnabled(True)
        self.R_WK.setEnabled(True)
        self.R_Bow.setEnabled(True)
        self.R_Bat.setEnabled(True)
        self.Avail_Players.clear()
        self.selected_Players.clear()
        self.list.clear()

    def set_points(self):
        self.L_Bat_S.setText(str(self.batscount))
        self.L_Bow_S.setText(str(self.bowcount))
        self.L_Ar_S.setText(str(self.ARcount))
        self.L_WK_S.setText(str(self.wktcount))
        self.L_Points1.setText(str(self.used_points))
        self.L_Points.setText(str(self.avail_points))
        return 0

    def display_Players(self):
        bowlers=[]
        bow='BWL'
        sql="SELECT Player,Value FROM stats WHERE ctg='"+bow+"';"
        curs.execute(sql)
        a=curs.fetchall()
        for i in a:
            bowlers.append(i[0])
            self.ctg_bow.append(i[0])
            self.value[i[0]]=i[1]

        batsman=[]
        bat='BAT'
        sql="SELECT Player,Value FROM stats WHERE ctg='"+bat+"';"
        curs.execute(sql)
        b=curs.fetchall()
        for i in b:
            batsman.append(i[0])
            self.ctg_bat.append(i[0])
            self.value[i[0]]=i[1]

        All_Rounder=[]
        ar='AR'
        sql="SELECT Player,Value FROM stats WHERE ctg='"+ar+"';"
        curs.execute(sql)
        c=curs.fetchall()
        for i in c:
            All_Rounder.append(i[0])
            self.ctg_ar.append(i[0])
            self.value[i[0]]=i[1]

        wicket=[]
        wk='WK'
        sql="SELECT Player,Value FROM stats WHERE ctg='"+wk+"';"
        curs.execute(sql)
        d=curs.fetchall()
        for i in d:
            wicket.append(i[0])
            self.ctg_wk.append(i[0])
            self.value[i[0]]=i[1]


        for i in self.list:
            if i in bowlers:
                bowlers.remove(i)
            elif i in batsman:
                batsman.remove(i)
            elif i in All_Rounder:
                All_Rounder.remove(i)
            elif i in wicket:
                wicket.remove(i)
        
            
        if self.R_Bow.isChecked()==True:
            self.Avail_Players.clear()
            for k in range(len(bowlers)):
                item = QtWidgets.QListWidgetItem(bowlers[k])
                font = QtGui.QFont()
                font.setBold(True)
                font.setWeight(75)
                item.setFont(font)
                self.Avail_Players.addItem(item)
            return 0
                
        elif self.R_Bat.isChecked()==True:
            self.Avail_Players.clear()
            for k in range(len(batsman)):
                item = QtWidgets.QListWidgetItem(batsman[k])
                font = QtGui.QFont()
                font.setBold(True)
                font.setWeight(75)
                item.setFont(font)
                self.Avail_Players.addItem(item)
            return 0
            

        elif self.R_AR.isChecked()==True:
            self.Avail_Players.clear()
            for k in range(len(All_Rounder)):
                item = QtWidgets.QListWidgetItem(All_Rounder[k])
                font = QtGui.QFont()
                font.setBold(True)
                font.setWeight(75)
                item.setFont(font)
                self.Avail_Players.addItem(item)
            return 0
        
        elif self.R_WK.isChecked()==True:
            self.Avail_Players.clear()
            for k in range(len(wicket)):
                item = QtWidgets.QListWidgetItem(wicket[k])
                font = QtGui.QFont()
                font.setBold(True)
                font.setWeight(75)
                item.setFont(font)
                self.Avail_Players.addItem(item)
            return 0

    def select_Players(self,item):
        self.condition1(item.text())
        self.Avail_Players.takeItem(self.Avail_Players.row(item))
        self.selected_Players.addItem(item.text())
        self.list.append(item.text())
        self.totalcount=self.selected_Players.count()

    def remove_Players(self,item):
        self.selected_Players.takeItem(self.selected_Players.row(item))
        self.Avail_Players.addItem(item.text())
        self.totalcount=self.selected_Players.count()
        self.list.remove(item.text())
        self.condition2(item.text())
        

    def condition1(self,player):
        self.avail_points-=self.value[player]
        self.used_points+=self.value[player]
        if player in self.ctg_bow:
            self.bowcount+=1
        elif player in self.ctg_bat:
            self.batscount+=1
        elif player in self.ctg_ar:
            self.ARcount+=1
        elif player in self.ctg_wk:
            self.wktcount+=1

        self.set_points()

    def condition2(self,player):
        self.used_points-=self.value[player]
        self.avail_points+=self.value[player]
        if player in self.ctg_bow:
            self.bowcount-=1
        elif player in self.ctg_bat:
            self.batscount-=1
        elif player in self.ctg_ar:
            self.ARcount-=1
        elif player in self.ctg_wk:
            self.wktcount-=1

        self.set_points()

    def check_team(self):
        batcount=self.L_Bat_S.text()
        bowcount=self.L_Bow_S.text()
        arcount=self.L_Ar_S.text()
        wkcount=self.L_WK_S.text()
        avail_points=self.L_Points.text()
        totalcount=int(batcount)+int(bowcount)+int(arcount)+int(wkcount)
        if int(wkcount) >1:
            msg=QMessageBox()
            msg.setWindowTitle("Error")
            msg.setText("Only one wicket keeper is allowed!!!")
            msg.exec()
            msg.close()
            return 0
        elif int(wkcount) < 1:
            msg=QMessageBox()
            msg.setWindowTitle("Error")
            msg.setText("Please select one wicket keeper!!!")
            msg.exec()
            msg.close()
            return 0
        elif int(totalcount)<11:
            msg=QMessageBox()
            msg.setWindowTitle("Error")
            msg.setText("Not enough players!!!")
            msg.exec()
            msg.close()
            return 0
        elif int(totalcount) > 11:
            msg=QMessageBox()
            msg.setWindowTitle("Error")
            msg.setText("Number of players is more than 11")
            msg.exec()
            msg.close()
            return 0
        elif int(avail_points) <0:
            msg=QMessageBox()
            msg.setWindowTitle("Error")
            msg.setText("Not enought points")
            msg.exec()
            msg.close()
            return 0

        return 1
     


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
