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

# Form implementation generated from reading ui file 'Main.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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.QtCore import QObject, QThread, pyqtSignal
from PyQt5 import QtCore, QtGui, QtWidgets, Qt
from typing import List, Dict, Optional
import pandas as pd
import time
import Web_Scraping

# Classe para executar a tarefa em uma thread
# impede o travamento da interface
# Executa a função de web scraping em paralelo a interface
class Tarefa(QObject):
        # Sinal progress bar
        bar = pyqtSignal(int)
        # sinal da função de web scraping
        status = pyqtSignal()
        # Sinal de finalização 
        finished = pyqtSignal()

        # Construtor com os dados
        def __init__(self, df):
                super().__init__()
                self.data = df

        # Barra de progresso
        def progress_bar(self):
                for i in range(101):
                        time.sleep(0.03)
                        self.bar.emit(i)
                self.finished.emit()
        # função Web Scraping
        def run(self):
                Web_Scraping.Buscar_game(self.data)
                self.status.emit()

class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
                MainWindow.setObjectName("MainWindow")
                MainWindow.resize(750, 600)
                MainWindow.setMinimumSize(QtCore.QSize(750, 600))
                MainWindow.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
                icon = QtGui.QIcon()
                icon.addPixmap(QtGui.QPixmap("icones/excel.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
                MainWindow.setWindowIcon(icon)
                MainWindow.setStyleSheet("color: rgb(255, 255, 255);")
                self.centralwidget = QtWidgets.QWidget(MainWindow)
                self.centralwidget.setObjectName("centralwidget")
                self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
                self.verticalLayout.setContentsMargins(0, 0, 0, 0)
                self.verticalLayout.setSpacing(0)
                self.verticalLayout.setObjectName("verticalLayout")
                self.frame_top = QtWidgets.QFrame(self.centralwidget)
                self.frame_top.setMinimumSize(QtCore.QSize(0, 30))
                self.frame_top.setMaximumSize(QtCore.QSize(16777215, 50))
                self.frame_top.setStyleSheet("background-color: rgb(3, 0, 33);")
                self.frame_top.setFrameShape(QtWidgets.QFrame.NoFrame)
                self.frame_top.setFrameShadow(QtWidgets.QFrame.Raised)
                self.frame_top.setObjectName("frame_top")
                self.hboxlayout = QtWidgets.QHBoxLayout(self.frame_top)
                self.hboxlayout.setContentsMargins(0, 0, 0, 0)
                self.hboxlayout.setSpacing(0)
                self.hboxlayout.setObjectName("hboxlayout")
                self.frame_error = QtWidgets.QFrame(self.frame_top)
                self.frame_error.setMaximumSize(QtCore.QSize(450, 30))
                self.frame_error.setStyleSheet("background-color: rgb(253, 118, 118);\n"
        "border-radius:10px;")
                self.frame_error.setFrameShape(QtWidgets.QFrame.NoFrame)
                self.frame_error.setFrameShadow(QtWidgets.QFrame.Raised)
                self.frame_error.setObjectName("frame_error")
                self.horizontalLayout = QtWidgets.QHBoxLayout(self.frame_error)
                self.horizontalLayout.setContentsMargins(5, 5, 5, 5)
                self.horizontalLayout.setObjectName("horizontalLayout")
                self.label_error = QtWidgets.QLabel(self.frame_error)
                font = QtGui.QFont()
                font.setFamily("DejaVu")
                font.setBold(True)
                font.setWeight(75)
                self.label_error.setFont(font)
                self.label_error.setStyleSheet("color: rgb(0, 0, 0);")
                self.label_error.setAlignment(QtCore.Qt.AlignCenter)
                self.label_error.setObjectName("label_error")
                self.horizontalLayout.addWidget(self.label_error)
                self.pushButton_Error = QtWidgets.QPushButton(self.frame_error)
                self.pushButton_Error.setMaximumSize(QtCore.QSize(30, 30))
                self.pushButton_Error.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
                self.pushButton_Error.setStyleSheet("QPushButton#pushButton_Error\n"
        "{\n"
        "color: rgb(0,0,0);\n"
        "    border-radius:5px;\n"
        "    background-color: rgb(253, 118, 118);\n"
        "}\n"
        "QPushButton:hover#pushButton_Error\n"
        "{\n"
        "    color: rgb(255, 255, 255);\n"
        "    background-color: rgb(3, 0, 33);\n"
        "}")
                self.pushButton_Error.setObjectName("pushButton_Error")
                self.horizontalLayout.addWidget(self.pushButton_Error)
                self.hboxlayout.addWidget(self.frame_error)
                self.verticalLayout.addWidget(self.frame_top)
                self.frame_Info = QtWidgets.QFrame(self.centralwidget)
                self.frame_Info.setMaximumSize(QtCore.QSize(16777215, 80))
                self.frame_Info.setStyleSheet("background-color: rgb(3, 0, 33);")
                self.frame_Info.setFrameShape(QtWidgets.QFrame.NoFrame)
                self.frame_Info.setFrameShadow(QtWidgets.QFrame.Raised)
                self.frame_Info.setObjectName("frame_Info")
                self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.frame_Info)
                self.horizontalLayout_2.setObjectName("horizontalLayout_2")
                self.frame_2 = QtWidgets.QFrame(self.frame_Info)
                self.frame_2.setMaximumSize(QtCore.QSize(650, 16777215))
                self.frame_2.setStyleSheet("background-color: rgb(3, 0, 33);")
                self.frame_2.setFrameShape(QtWidgets.QFrame.NoFrame)
                self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
                self.frame_2.setObjectName("frame_2")
                self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.frame_2)
                self.horizontalLayout_3.setObjectName("horizontalLayout_3")
                self.lineEdit_Marc = QtWidgets.QLineEdit(self.frame_2)
                font = QtGui.QFont()
                font.setFamily("DejaVu")
                self.lineEdit_Marc.setFont(font)
                self.lineEdit_Marc.setStyleSheet("background-color: rgb(255, 255, 255);\n"
        "color: rgb(0, 0, 0);")
                self.lineEdit_Marc.setObjectName("lineEdit_Marc")
                self.horizontalLayout_3.addWidget(self.lineEdit_Marc)
                self.lineEdit_Jogo = QtWidgets.QLineEdit(self.frame_2)
                font = QtGui.QFont()
                font.setFamily("DejaVu")
                self.lineEdit_Jogo.setFont(font)
                self.lineEdit_Jogo.setStyleSheet("background-color: rgb(255, 255, 255);\n"
        "color: rgb(0, 0, 0);")
                self.lineEdit_Jogo.setObjectName("lineEdit_Jogo")
                self.horizontalLayout_3.addWidget(self.lineEdit_Jogo)
                self.lineEdit_Plat = QtWidgets.QLineEdit(self.frame_2)
                font = QtGui.QFont()
                font.setFamily("DejaVu")
                self.lineEdit_Plat.setFont(font)
                self.lineEdit_Plat.setStyleSheet("background-color: rgb(255, 255, 255);\n"
        "color: rgb(0, 0, 0);")
                self.lineEdit_Plat.setObjectName("lineEdit_Plat")
                self.horizontalLayout_3.addWidget(self.lineEdit_Plat)
                self.spinBox_QTD = QtWidgets.QSpinBox(self.frame_2)
                font = QtGui.QFont()
                font.setFamily("DejaVu")
                self.spinBox_QTD.setFont(font)
                self.spinBox_QTD.setStyleSheet("background-color: rgb(255, 255, 255);\n"
        "color: rgb(0, 0, 0);")
                self.spinBox_QTD.setObjectName("spinBox_QTD")
                self.horizontalLayout_3.addWidget(self.spinBox_QTD)
                self.horizontalLayout_2.addWidget(self.frame_2)
                self.verticalLayout.addWidget(self.frame_Info)
                self.frame_botao = QtWidgets.QFrame(self.centralwidget)
                self.frame_botao.setMaximumSize(QtCore.QSize(16777215, 60))
                self.frame_botao.setStyleSheet("background-color: rgb(3, 0, 33);")
                self.frame_botao.setFrameShape(QtWidgets.QFrame.NoFrame)
                self.frame_botao.setFrameShadow(QtWidgets.QFrame.Raised)
                self.frame_botao.setObjectName("frame_botao")
                self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.frame_botao)
                self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
                self.horizontalLayout_4.setSpacing(0)
                self.horizontalLayout_4.setObjectName("horizontalLayout_4")
                self.frame_3 = QtWidgets.QFrame(self.frame_botao)
                self.frame_3.setMinimumSize(QtCore.QSize(0, 50))
                self.frame_3.setMaximumSize(QtCore.QSize(200, 50))
                self.frame_3.setStyleSheet("background-color: rgb(3, 0, 33);")
                self.frame_3.setFrameShape(QtWidgets.QFrame.NoFrame)
                self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
                self.frame_3.setObjectName("frame_3")
                self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.frame_3)
                self.horizontalLayout_5.setObjectName("horizontalLayout_5")
                self.pushButton_Add = QtWidgets.QPushButton(self.frame_3)
                self.pushButton_Add.setMinimumSize(QtCore.QSize(60, 30))
                self.pushButton_Add.setMaximumSize(QtCore.QSize(60, 30))
                font = QtGui.QFont()
                font.setFamily("DejaVu")
                font.setBold(True)
                font.setWeight(75)
                self.pushButton_Add.setFont(font)
                self.pushButton_Add.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
                self.pushButton_Add.setStyleSheet("QPushButton#pushButton_Add\n"
        "{\n"
        "    \n"
        "    color: rgb(0, 0, 0);\n"
        "    background-color: rgb(150,255,150);\n"
        "    border-radius:10px;\n"
        "}\n"
        "QPushButton:hover#pushButton_Add\n"
        "{\n"
        "    background-color: rgb(40, 140, 30);\n"
        "}")
                self.pushButton_Add.setObjectName("pushButton_Add")
                self.horizontalLayout_5.addWidget(self.pushButton_Add)
                self.pushButton_Del = QtWidgets.QPushButton(self.frame_3)
                self.pushButton_Del.setMinimumSize(QtCore.QSize(60, 30))
                self.pushButton_Del.setMaximumSize(QtCore.QSize(60, 30))
                font = QtGui.QFont()
                font.setFamily("DejaVu")
                font.setBold(True)
                font.setWeight(75)
                self.pushButton_Del.setFont(font)
                self.pushButton_Del.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
                self.pushButton_Del.setStyleSheet("QPushButton#pushButton_Del\n"
        "{\n"
        "    color: rgb(0, 0, 0);\n"
        "    background-color: rgb(255,150,150);\n"
        "    border-radius:10px;\n"
        "}\n"
        "QPushButton:hover#pushButton_Del\n"
        "{\n"
        "    background-color: rgb(140, 40, 30);\n"
        "}")
                self.pushButton_Del.setObjectName("pushButton_Del")
                self.horizontalLayout_5.addWidget(self.pushButton_Del)
                self.horizontalLayout_4.addWidget(self.frame_3)
                self.verticalLayout.addWidget(self.frame_botao)
                self.frame_view = QtWidgets.QFrame(self.centralwidget)
                self.frame_view.setStyleSheet("background-color: rgb(3, 0, 33);")
                self.frame_view.setFrameShape(QtWidgets.QFrame.NoFrame)
                self.frame_view.setFrameShadow(QtWidgets.QFrame.Raised)
                self.frame_view.setObjectName("frame_view") 
                self.horizontalLayout_6 = QtWidgets.QHBoxLayout(self.frame_view)
                self.horizontalLayout_6.setContentsMargins(50, 9, 50, -1)
                self.horizontalLayout_6.setObjectName("horizontalLayout_6")
                # Table View
                self.tableView = QtWidgets.QTableWidget(self.frame_view)
                self.tableView.setMinimumSize(QtCore.QSize(200, 200))
                self.tableView.setStyleSheet("background-color: rgb(255, 255, 255);\n"
        "color: rgb(0, 0, 0);")
                self.tableView.setObjectName("tableView")
                self.tableView.setColumnCount(4)
                self.tableView.setRowCount(0)
                self.tableView.setColumnWidth(0,200)
                self.tableView.setColumnWidth(1,280)
                self.tableView.setColumnWidth(2,100)
                self.tableView.setColumnWidth(3,50)
                self.tableView.setHorizontalHeaderLabels(('Distribuidora', 'Jogo', 'Plataforma', 'QTD'))
                header = self.tableView.horizontalHeader()
                header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
                header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
                header.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)

                self.horizontalLayout_6.addWidget(self.tableView)
                self.verticalLayout.addWidget(self.frame_view)
                self.frame_Pesq = QtWidgets.QFrame(self.centralwidget)
                self.frame_Pesq.setMinimumSize(QtCore.QSize(0, 50))
                self.frame_Pesq.setMaximumSize(QtCore.QSize(16777215, 50))
                self.frame_Pesq.setStyleSheet("background-color: rgb(3, 0, 33);")
                self.frame_Pesq.setFrameShape(QtWidgets.QFrame.NoFrame)
                self.frame_Pesq.setFrameShadow(QtWidgets.QFrame.Raised)
                self.frame_Pesq.setObjectName("frame_Pesq")
                self.horizontalLayout_7 = QtWidgets.QHBoxLayout(self.frame_Pesq)
                self.horizontalLayout_7.setContentsMargins(-1, 5, -1, 5)
                self.horizontalLayout_7.setObjectName("horizontalLayout_7")
                self.progressBar = QtWidgets.QProgressBar(self.frame_Pesq)
                self.progressBar.setMaximumSize(QtCore.QSize(450, 25))
                self.progressBar.setStyleSheet("QProgressBar {\n"
        "    background-color: rgb(255, 255, 255);\n"
        "    color: rgb(0, 0, 0);\n"
        "}\n"
        "QProgressBar::chunk {\n"
        "    \n"
        "     background-color: rgb(0, 255, 0);\n"
        "         \n"
        " }")
                self.progressBar.setProperty("value", 0)
                self.progressBar.setAlignment(QtCore.Qt.AlignCenter)
                self.progressBar.setObjectName("progressBar")
                self.horizontalLayout_7.addWidget(self.progressBar)
                self.pushButton_Pes = QtWidgets.QPushButton(self.frame_Pesq)
                self.pushButton_Pes.setMinimumSize(QtCore.QSize(150, 40))
                self.pushButton_Pes.setMaximumSize(QtCore.QSize(100, 16777215))
                font = QtGui.QFont()
                font.setFamily("DejaVu")
                font.setBold(True)
                font.setWeight(75)
                self.pushButton_Pes.setFont(font)
                self.pushButton_Pes.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
                self.pushButton_Pes.setStyleSheet("QPushButton#pushButton_Pes\n"
        "{\n"
        "    \n"
        "    color: rgb(0, 0, 0);\n"
        "    background-color: rgb(150,150,255);\n"
        "    border-radius:10px;\n"
        "}\n"
        "QPushButton:hover#pushButton_Pes\n"
        "{\n"
        "    background-color: rgb(30,40, 140);\n"
        "}")
                self.pushButton_Pes.setObjectName("pushButton_Pes")
                self.horizontalLayout_7.addWidget(self.pushButton_Pes)
                self.verticalLayout.addWidget(self.frame_Pesq)
                self.frame_Bottom = QtWidgets.QFrame(self.centralwidget)
                self.frame_Bottom.setMinimumSize(QtCore.QSize(0, 30))
                self.frame_Bottom.setStyleSheet("background-color: rgb(3, 0, 33);")
                self.frame_Bottom.setFrameShape(QtWidgets.QFrame.NoFrame)
                self.frame_Bottom.setFrameShadow(QtWidgets.QFrame.Raised)
                self.frame_Bottom.setObjectName("frame_Bottom")
                self.horizontalLayout_8 = QtWidgets.QHBoxLayout(self.frame_Bottom)
                self.horizontalLayout_8.setObjectName("horizontalLayout_8")
                self.label = QtWidgets.QLabel(self.frame_Bottom)
                self.label.setMinimumSize(QtCore.QSize(200, 0))
                font = QtGui.QFont()
                font.setFamily("DejaVu")
                self.label.setFont(font)
                self.label.setLayoutDirection(QtCore.Qt.RightToLeft)
                self.label.setStyleSheet("color: rgb(67, 67, 67);")
                self.label.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
                self.label.setObjectName("label")
                self.horizontalLayout_8.addWidget(self.label)
                self.verticalLayout.addWidget(self.frame_Bottom)
                MainWindow.setCentralWidget(self.centralwidget)
                self.menubar = QtWidgets.QMenuBar(MainWindow)
                self.menubar.setGeometry(QtCore.QRect(0, 0, 750, 20))
                self.menubar.setObjectName("menubar")
                MainWindow.setMenuBar(self.menubar)
                self.statusbar = QtWidgets.QStatusBar(MainWindow)
                self.statusbar.setObjectName("statusbar")
                MainWindow.setStatusBar(self.statusbar)

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

                # Click Botões

                # Ocultar POPUP
                self.frame_error.hide()

                # Ocultar Barra de Progesso
                self.progressBar.hide()

                # Fechar POPUP
                self.pushButton_Error.clicked.connect(lambda: self.frame_error.hide())

                # Click do botão adicionar 
                self.pushButton_Add.clicked.connect(self.validar)


                #Click do botão excluir
                self.pushButton_Del.clicked.connect(self.del_table)

                #Click do botão Pesquisar
                self.pushButton_Pes.clicked.connect(self.get_value)

                self.retranslateUi(MainWindow)
                QtCore.QMetaObject.connectSlotsByName(MainWindow)
        
        #Funções
        # Validar Campos
        def validar(self):
                marca = None
                jogo = None
                Plat = None
                qtd = None

                qtd = int(self.spinBox_QTD.value())

                if (self.lineEdit_Marc.text() != '' and self.lineEdit_Marc.text() != None):
                        marca = str(self.lineEdit_Marc.text())

                if (self.lineEdit_Jogo.text() != '' and self.lineEdit_Jogo.text() != None):
                        jogo = str(self.lineEdit_Jogo.text())

                if (self.lineEdit_Plat.text() != '' and self.lineEdit_Plat.text() != None):
                        Plat = str(self.lineEdit_Plat.text())


                if(marca==None or jogo ==None or Plat==None or qtd==None):
                        self.label_error.setText('Erro valor inválido')
                        self.frame_error.show()
                else:
                        self.add_table(marca, jogo, Plat, qtd)

        # Adicionar dados a tabela
        def add_table(self, Marca: str, Jogo: str, Plat:str, QTD:int):
                rowCount = self.tableView.rowCount()
                self.tableView.insertRow(rowCount)
                self.tableView.setItem(rowCount, 0, QtWidgets.QTableWidgetItem(str(Marca)))
                self.tableView.setItem(rowCount, 1, QtWidgets.QTableWidgetItem(str(Jogo)))
                self.tableView.setItem(rowCount, 2, QtWidgets.QTableWidgetItem(str(Plat)))
                self.tableView.setItem(rowCount, 3, QtWidgets.QTableWidgetItem(str(QTD)))

        # Deletar dados da tabela
        def del_table(self):
                try:
                        indices = self.tableView.selectionModel().selectedRows() 
                        for index in indices:
                                self.tableView.removeRow(index.row())
                except:
                        pass
        
        # obter valores da tabela 
        def get_value(self):

                lista =  [[]]
                lista.clear()
                headers = [self.tableView.horizontalHeaderItem(c).text() 
                                for c in range(self.tableView.columnCount())]

                lista = [[self.tableView.item(row, col).text() 
                        for col in range(self.tableView.columnCount())]  
                        for row in range(self.tableView.rowCount())]

                if(lista):
                        self.pushButton_Pes.hide()
                        self.progressBar.show()
                        data = pd.DataFrame(data= lista, columns=headers)
                        time.sleep(0.05)
                        self.load_bar(data)
        
        # função que executa as tarefas em uma thread e carrega a progressbar
        def load_bar(self, data):
                
                self.thread = QThread()
                self.task = Tarefa(df=data)
                
                # Sinais da Thread
                self.task.moveToThread(self.thread)
                self.thread.started.connect(self.task.run)
                self.task.status.connect(self.task.progress_bar)
                self.task.bar.connect(self.set_bar)
                self.task.finished.connect(self.thread.quit)
                self.task.finished.connect(self.task.deleteLater)
                self.thread.finished.connect(self.thread.deleteLater)
                
                self.thread.start()

                self.pushButton_Pes.setEnabled(False)
                self.thread.finished.connect(lambda: [
                        self.pushButton_Pes.setEnabled(True), self.pushButton_Pes.show(), 
                        self.progressBar.hide(), self.progressBar.setValue(0)])

        def set_bar(self, progresso : int):
                self.progressBar.setValue(progresso)

        def retranslateUi(self, MainWindow):
                _translate = QtCore.QCoreApplication.translate
                MainWindow.setWindowTitle(_translate("MainWindow", "Planilha_Estoque"))
                self.label_error.setText(_translate("MainWindow", "Erro Dados Inválidos"))
                self.pushButton_Error.setText(_translate("MainWindow", "X"))
                self.lineEdit_Marc.setPlaceholderText(_translate("MainWindow", "Distribuidora"))
                self.lineEdit_Jogo.setPlaceholderText(_translate("MainWindow", "Jogo"))
                self.lineEdit_Plat.setPlaceholderText(_translate("MainWindow", "Plataforma"))
                self.pushButton_Add.setText(_translate("MainWindow", "+"))
                self.pushButton_Del.setText(_translate("MainWindow", "-"))
                self.pushButton_Pes.setText(_translate("MainWindow", "Pesquisar"))
                self.label.setText(_translate("MainWindow", "Desenvolvido por Alisson_Tech"))


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_())
