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

# Form implementation generated from reading ui file 'untitled.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.
import pyautogui as gui, pandas as pd, time, pymsgbox, subprocess, pyperclip, os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTime, QTimer
from PyQt5.QtWidgets import QFileDialog

gui.FAILSAFE = True


class Ui_mainWindow(object):
    phoneNumberList = None
    sendTime = QTime(0, 0, 0)
    csvFile = None

    def setupUi(self, mainWindow):
        self.timer = QTimer()
        self.timer.timeout.connect(self.showSingleTime)
        self.timer2 = QTimer()
        self.timer2.timeout.connect(self.showMultipleTime)
        self.timer3 = QTimer()
        self.timer3.timeout.connect(self.showSingleTime2)
        self.timer4 = QTimer()
        self.timer4.timeout.connect(self.showMultipleTime2)
        mainWindow.setObjectName("mainWindow")
        mainWindow.setEnabled(True)
        mainWindow.resize(800, 600)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(mainWindow.sizePolicy().hasHeightForWidth())
        mainWindow.setSizePolicy(sizePolicy)
        mainWindow.setMinimumSize(QtCore.QSize(800, 600))
        mainWindow.setMaximumSize(QtCore.QSize(800, 600))
        mainWindow.setSizeIncrement(QtCore.QSize(800, 600))
        mainWindow.setBaseSize(QtCore.QSize(800, 600))
        mainWindow.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("Images/bot_icon.ico"), QtGui.QIcon.Normal, QtGui.QIcon.On)
        mainWindow.setWindowIcon(icon)
        mainWindow.setWindowOpacity(1.0)
        mainWindow.setToolTipDuration(0)
        mainWindow.setAnimated(True)
        mainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)
        self.centralwidget = QtWidgets.QWidget(mainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.startBotButton = QtWidgets.QPushButton(self.centralwidget)
        self.startBotButton.setGeometry(QtCore.QRect(570, 400, 211, 141))
        font = QtGui.QFont()
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        self.startBotButton.setFont(font)
        self.startBotButton.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.startBotButton.setObjectName("startBotButton")
        self.startBotButton.clicked.connect(self.sendMessage)
        self.contactsField = QtWidgets.QTextEdit(self.centralwidget)
        self.contactsField.setEnabled(True)
        self.contactsField.setGeometry(QtCore.QRect(570, 80, 211, 271))
        self.contactsField.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.contactsField.setReadOnly(True)
        self.contactsField.setObjectName("contactsField")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(640, 50, 71, 20))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setItalic(False)
        font.setUnderline(True)
        font.setWeight(75)
        font.setStrikeOut(False)
        self.label.setFont(font)
        self.label.setLineWidth(1)
        self.label.setObjectName("label")
        self.writeYourMessage = QtWidgets.QTextEdit(self.centralwidget)
        self.writeYourMessage.setGeometry(QtCore.QRect(25, 270, 521, 271))
        self.writeYourMessage.setObjectName("writeYourMessage")
        self.downloadExampleCSVFile = QtWidgets.QPushButton(self.centralwidget)
        self.downloadExampleCSVFile.setGeometry(QtCore.QRect(25, 550, 140, 24))
        self.downloadExampleCSVFile.clicked.connect(lambda: self.dowloadExampleCSV(mainWindow))
        self.selectCSVFileButton = QtWidgets.QPushButton(self.centralwidget)
        self.selectCSVFileButton.setEnabled(False)
        self.selectCSVFileButton.setGeometry(QtCore.QRect(20, 90, 121, 24))
        self.selectCSVFileButton.setMinimumSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.selectCSVFileButton.setFont(font)
        self.selectCSVFileButton.setObjectName("selectCSVFileButton")
        self.selectCSVFileButton.clicked.connect(lambda: self.selectCSVFile(mainWindow))
        self.csvFileDestinationInputField = QtWidgets.QLineEdit(self.centralwidget)
        self.csvFileDestinationInputField.setEnabled(False)
        self.csvFileDestinationInputField.setGeometry(QtCore.QRect(160, 90, 301, 21))
        self.csvFileDestinationInputField.setObjectName("csvFileDestinationInputField")
        self.editTime = QtWidgets.QTimeEdit(self.centralwidget)
        self.editTime.setEnabled(False)
        self.editTime.setGeometry(QtCore.QRect(180, 200, 118, 22))
        self.editTime.setTimeRange(QTime(00, 00, 00), QTime(23, 59, 00))
        self.editTime.setDisplayFormat('hh:mm:ss')
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.editTime.setFont(font)
        self.editTime.setObjectName("editTime")
        self.phoneNumberInputField = QtWidgets.QLineEdit(self.centralwidget)
        self.phoneNumberInputField.setEnabled(False)
        self.phoneNumberInputField.setGeometry(QtCore.QRect(160, 60, 301, 21))
        self.phoneNumberInputField.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
        self.phoneNumberInputField.setEchoMode(QtWidgets.QLineEdit.Normal)
        self.phoneNumberInputField.setObjectName("phoneNumberInputField")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(20, 60, 121, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(260, 120, 71, 20))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setItalic(False)
        font.setUnderline(True)
        font.setWeight(75)
        font.setStrikeOut(False)
        self.label_3.setFont(font)
        self.label_3.setLineWidth(1)
        self.label_3.setObjectName("label_3")
        self.scheduleMessageCheckBox = QtWidgets.QCheckBox(self.centralwidget)
        self.scheduleMessageCheckBox.setGeometry(QtCore.QRect(50, 200, 121, 20))
        self.scheduleMessageCheckBox.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.scheduleMessageCheckBox.setObjectName("scheduleMessageCheckBox")
        self.scheduleMessageCheckBox.toggled.connect(self.controlOptions)
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(40, 240, 181, 20))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setItalic(False)
        font.setUnderline(True)
        font.setWeight(75)
        font.setStrikeOut(False)
        self.label_4.setFont(font)
        self.label_4.setLineWidth(1)
        self.label_4.setObjectName("label_4")
        self.clearButton = QtWidgets.QPushButton(self.centralwidget)
        self.clearButton.setGeometry(QtCore.QRect(470, 60, 31, 51))
        self.clearButton.clicked.connect(self.clearText)
        font = QtGui.QFont()
        font.setKerning(True)
        font.setStyleStrategy(QtGui.QFont.NoAntialias)
        self.clearButton.setFont(font)
        self.clearButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.clearButton.setMouseTracking(False)
        self.clearButton.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.clearButton.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
        self.clearButton.setAcceptDrops(False)
        self.clearButton.setWhatsThis("")
        self.clearButton.setAutoFillBackground(False)
        self.clearButton.setText("")
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("Images/clear_image.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.clearButton.setIcon(icon1)
        self.clearButton.setIconSize(QtCore.QSize(31, 51))
        self.clearButton.setCheckable(False)
        self.clearButton.setAutoExclusive(False)
        self.clearButton.setAutoDefault(False)
        self.clearButton.setDefault(False)
        self.clearButton.setFlat(False)
        self.clearButton.setObjectName("clearButton")
        self.hintButton = QtWidgets.QPushButton(self.centralwidget)
        self.hintButton.setGeometry(QtCore.QRect(730, 10, 41, 41))
        self.hintButton.setText("")
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap("Images/question_icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.hintButton.setIcon(icon2)
        self.hintButton.setIconSize(QtCore.QSize(41, 41))
        self.hintButton.setObjectName("hintButton")
        self.hintButton.clicked.connect(self.showHintText)
        self.hintText = QtWidgets.QTextBrowser(self.centralwidget)
        self.hintText.setEnabled(True)
        self.hintText.setGeometry(QtCore.QRect(480, 40, 256, 221))
        self.hintText.setObjectName("hintText")
        self.hintText.setVisible(False)
        self.label_5 = QtWidgets.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(20, 20, 181, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_5.setFont(font)
        self.label_5.setObjectName("label_5")
        self.whatsappFileDestination = QtWidgets.QLineEdit(self.centralwidget)
        self.whatsappFileDestination.setEnabled(True)
        self.whatsappFileDestination.setGeometry(QtCore.QRect(210, 20, 251, 21))
        self.whatsappFileDestination.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
        self.whatsappFileDestination.setEchoMode(QtWidgets.QLineEdit.Normal)
        self.whatsappFileDestination.setObjectName("whatsappFileDestination")
        self.atTheTimeRadioButton = QtWidgets.QRadioButton(self.centralwidget)
        self.atTheTimeRadioButton.setEnabled(False)
        self.atTheTimeRadioButton.setGeometry(QtCore.QRect(310, 200, 121, 17))
        self.atTheTimeRadioButton.setObjectName("atTheTimeRadioButton")
        self.atTheTimeRadioButton.toggled.connect(self.setTimeOption)
        self.timeLabel = QtWidgets.QLabel(self.centralwidget)
        self.timeLabel.setGeometry(QtCore.QRect(430, 199, 121, 17))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.timeLabel.setFont(font)
        self.timeLabel.setObjectName("timeLabel")
        self.countdownTimeRadioButton = QtWidgets.QRadioButton(self.centralwidget)
        self.countdownTimeRadioButton.setEnabled(False)
        self.countdownTimeRadioButton.setGeometry(QtCore.QRect(310, 220, 131, 31))
        self.countdownTimeRadioButton.setObjectName("countdownTimeRadioButton")
        self.countdownTimeRadioButton.toggled.connect(self.setTimeOption)
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(40, 140, 291, 51))
        self.widget.setObjectName("widget")
        self.singleContactRadioButton = QtWidgets.QRadioButton(self.widget)
        self.singleContactRadioButton.setGeometry(QtCore.QRect(10, 10, 191, 17))
        self.singleContactRadioButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.singleContactRadioButton.setObjectName("singleContactRadioButton")
        self.singleContactRadioButton.toggled.connect(self.controlOptions)
        self.multipleContactsRadioButton = QtWidgets.QRadioButton(self.widget)
        self.multipleContactsRadioButton.setGeometry(QtCore.QRect(10, 30, 201, 17))
        self.multipleContactsRadioButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.multipleContactsRadioButton.setObjectName("multipleContactsRadioButton")
        self.multipleContactsRadioButton.toggled.connect(self.controlOptions)
        mainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(mainWindow)
        self.statusbar.setObjectName("statusbar")
        mainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, mainWindow):
        _translate = QtCore.QCoreApplication.translate
        mainWindow.setWindowTitle(_translate("mainWindow", "WhatsAppBot"))
        self.downloadExampleCSVFile.setText("Download Example CSV File")
        self.startBotButton.setToolTip(
            _translate("mainWindow", "<html><head/><body><p>Let\'s gooo...</p></body></html>"))
        self.startBotButton.setText(_translate("mainWindow", "Start Bot"))
        self.label.setText(_translate("mainWindow", "Contacts"))
        self.timeLabel.setText(_translate("mainWindow", ""))
        self.selectCSVFileButton.setToolTip(
            _translate("mainWindow", "<html><head/><body><p>Select CSV File</p></body></html>"))
        self.selectCSVFileButton.setText(_translate("mainWindow", "Select CSV File"))
        self.editTime.setToolTip(_translate("mainWindow", "Set a specific time to send message..."))
        self.label_2.setText(_translate("mainWindow", "Enter Phone Number:"))
        self.label_3.setText(_translate("mainWindow", "Options"))
        self.scheduleMessageCheckBox.setText(_translate("mainWindow", "Schedule Message"))
        self.label_4.setText(_translate("mainWindow", "Write your message"))
        self.clearButton.setToolTip(_translate("mainWindow", "Clear phone number"))
        self.hintText.setHtml(_translate("mainWindow",
                                         "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
                                         "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
                                         "p, li { white-space: pre-wrap; }\n"
                                         "</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
                                         "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">-To<span style=\" font-weight:600;\"> send message to single contact</span> select it from the options, enter the phone number, write your message and start the bot.</p>\n"
                                         "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">-To<span style=\" font-weight:600;\"> send message to multiple contacts</span> select it from the options, select a csv file with the names, phone numbers of the contacts write your message and start the bot.</p>\n"
                                         "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">-To <span style=\" font-weight:600;\">schedule message</span> at a specific time in a day, select the option &quot;Schedule Message&quot;, select the type, set the time, write your message and start the bot.</p>\n"
                                         "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">-To <span style=\" font-weight:600;\">set WhatsApp file destination</span>:</p>\n"
                                         "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">  1. <a name=\"tw-target-text\"></a><span style=\" font-family:\'inherit\'; font-size:8pt; color:#202124;\">R</span><span style=\" font-family:\'inherit\'; font-size:8pt; color:#202124;\">ight click on whatsapp shortcut on desktop.</span></p>\n"
                                         "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'inherit\'; font-size:8pt; color:#202124;\">  2. Select Properties. </span></p>\n"
                                         "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'inherit\'; font-size:8pt; color:#202124;\">  3. Copy the target path of the shortcut and</span></p>\n"
                                         "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'inherit\'; font-size:8pt; color:#202124;\">  paste it to the input field &quot;Set Whatsapp File</span></p>\n"
                                         "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'inherit\'; font-size:8pt; color:#202124;\">  Destination&quot;.</span></p></body></html>\n"
                                         "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'inherit\'; font-size:8pt; color:#202124;\">-In order to<span style=\" font-weight:600;\"> use the CSV File Format correctly</span>, you can download the sample CSV file by clicking the button in the lower left corner of the screen.</span></p></body></html>\n"
                                         "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'inherit\'; font-size:8pt; color:#202124;\">-To <span style=\" font-weight:600;\"> stop the bot</span>, while running ,drag the mouse cursor to one of the 4 corners of the main screen and wait. If you want to stop the timer while running, you can stop it by clicking the 'Bot Running' Button. </span></p></body></html>"))
        self.label_5.setText(_translate("mainWindow", "Set WhatsApp File Destination:"))
        self.atTheTimeRadioButton.setText(_translate("mainWindow", "At the specified time"))
        self.countdownTimeRadioButton.setText(_translate("mainWindow", "After the specified time\n"
                                                                       "has expired "))
        self.singleContactRadioButton.setText(_translate("mainWindow", "Send Message To Single Contact"))
        self.multipleContactsRadioButton.setText(_translate("mainWindow", "Send Message To Multiple Contacts"))

    def showHintText(self):
        if self.hintText.isVisible():
            self.hintText.setVisible(False)
        else:
            self.hintText.setVisible(True)

    def controlOptions(self):
        if self.singleContactRadioButton.isChecked():
            self.phoneNumberInputField.setEnabled(True)
            self.selectCSVFileButton.setEnabled(False)
            self.csvFileDestinationInputField.setText("")
        elif self.multipleContactsRadioButton.isChecked():
            self.selectCSVFileButton.setEnabled(True)
            self.phoneNumberInputField.setEnabled(False)
            self.phoneNumberInputField.setText("")

        if self.scheduleMessageCheckBox.isChecked():
            self.editTime.setEnabled(True);
            self.atTheTimeRadioButton.setEnabled(True)
            self.countdownTimeRadioButton.setEnabled(True)
        else:
            self.editTime.setEnabled(False);
            self.atTheTimeRadioButton.setEnabled(False)
            self.countdownTimeRadioButton.setEnabled(False)

    def setTimeOption(self):
        if self.atTheTimeRadioButton.isChecked():
            time = QTime().currentTime()
            self.editTime.setTime(time)
        elif self.countdownTimeRadioButton.isChecked():
            self.editTime.setTime(QTime(00, 00, 00))

    def startSingleTimer(self):
        if self.editTime.time() == QTime(0, 0, 0):
            self.editTime.setTime(QTime(0, 0, 1))
        self.timer.start(1000)

    def showSingleTime(self):
        deleteTime = self.editTime.time().addSecs(-1)
        self.editTime.setTime(deleteTime)
        self.stopSingleTimer()

    def stopSingleTimer(self):
        if self.editTime.time() == QTime(0, 0, 0):
            self.timer.stop()
            self.sendSingleMessage()

    def startSingleTimer2(self):
        global sendTime
        sendTime = self.editTime.time()
        time_count = "{}{}".format(":", sendTime.toString())
        self.timeLabel.setText(time_count)
        self.editTime.setTime(QTime.currentTime())
        self.timer3.start(1000)

    def showSingleTime2(self):
        addTime = self.editTime.time().addSecs(1)
        self.editTime.setTime(addTime)
        self.stopSingleTimer2()

    def stopSingleTimer2(self):
        if self.editTime.time() >= sendTime:
            self.timer3.stop()
            self.sendSingleMessage()

    def startMultipleTimer(self):
        if self.editTime.time() == QTime(0, 0, 0):
            self.editTime.setTime(QTime(0, 0, 1))
        self.timer2.start(1000)

    def showMultipleTime(self):
        deleteTime = self.editTime.time().addSecs(-1)
        self.editTime.setTime(deleteTime)
        self.stopMultipleTimer()

    def stopMultipleTimer(self):
        if self.editTime.time() == QTime(0, 0, 0):
            self.timer2.stop()
            self.sendMultipleMessage()

    def startMultipleTimer2(self):
        global sendTime
        sendTime = self.editTime.time()
        time_count = "{}{}".format(":", sendTime.toString())
        self.timeLabel.setText(time_count)
        self.editTime.setTime(QTime.currentTime())
        self.timer4.start(1000)

    def showMultipleTime2(self):
        addTime = self.editTime.time().addSecs(1)
        self.editTime.setTime(addTime)
        self.stopMultipleTimer2()

    def stopMultipleTimer2(self):
        if self.editTime.time() >= sendTime:
            self.timer4.stop()
            self.sendMultipleMessage()

    def sendSingleMessage(self):
        subprocess.call([self.whatsappFileDestination.text()])
        time.sleep(3)
        self.startBot("Single")

    def sendMultipleMessage(self):
        subprocess.call([self.whatsappFileDestination.text()])
        time.sleep(3)
        self.startBot("Multiple")

    def sendMessage(self):
        if self.startBotButton.text() == "Bot Running":
            self.timer.stop()
            self.timer2.stop()
            self.timer3.stop()
            self.timer4.stop()
            self.startBotButton.setText("Start Bot")
            self.timeLabel.setText("")
        else:
            ret = self.controlWPDestination()
            if ret != -1:
                if self.singleContactRadioButton.isChecked():
                    ret = self.controlPhoneNumber()
                    if ret != -1:
                        if self.scheduleMessageCheckBox.isChecked():
                            if self.countdownTimeRadioButton.isChecked():
                                self.startSingleTimer()
                                self.startBotButton.setText("Bot Running")
                            elif self.atTheTimeRadioButton.isChecked():
                                if self.editTime.time() < QTime.currentTime():
                                    gui.alert("Message sending time must be ahead of the current time!", "MESSAGE",
                                              button=pymsgbox.OK_TEXT)
                                else:
                                    self.startSingleTimer2()
                                    self.startBotButton.setText("Bot Running")
                            else:
                                gui.alert("Message scheduling type not selected!", "MESSAGE", button=pymsgbox.OK_TEXT)
                        else:
                            self.sendSingleMessage()
                elif self.multipleContactsRadioButton.isChecked():
                    ret = self.controlCSVFile()
                    if ret != -1:
                        if self.scheduleMessageCheckBox.isChecked():
                            if self.countdownTimeRadioButton.isChecked():
                                self.startMultipleTimer()
                                self.startBotButton.setText("Bot Running")
                            elif self.atTheTimeRadioButton.isChecked():
                                if self.editTime.time() < QTime.currentTime():
                                    gui.alert("Message sending time must be ahead of the current time!", "MESSAGE",
                                              button=pymsgbox.OK_TEXT)
                                else:
                                    self.startMultipleTimer2()
                                    self.startBotButton.setText("Bot Running")
                            else:
                                gui.alert("Message scheduling type not selected!", "MESSAGE", button=pymsgbox.OK_TEXT)
                        else:
                            self.sendMultipleMessage()
                else:
                    self.alert("Message sending type not selected!")

    def controlCSVFile(self):
        if self.csvFileDestinationInputField.text().endswith(".csv"):
            pass
        else:
            self.alert("No CSV File selected!")
            return -1

    def controlWPDestination(self):
        if self.whatsappFileDestination.text().endswith("WhatsApp.exe"):
            pass
        else:
            mainWindow.setEnabled(False)
            self.alert("Whatsapp File Destination Not Found or Not Correct!")
            return -1

    def controlPhoneNumber(self):
        try:
            float(self.phoneNumberInputField.text())
            text = self.contactsField.toPlainText()
            if not self.phoneNumberInputField.text() in text:
                phoneNumber = "{}{}{}".format("Person", 14 * " ", self.phoneNumberInputField.text())
                self.contactsField.append(phoneNumber)
        except:
            self.alert("Phone number cannot contain characters!")
            return -1

    def alert(self, text):
        ret = gui.alert(text, "WARNING", button=pymsgbox.OK_TEXT)
        if ret:
            mainWindow.setEnabled(True)

    def startBot(self, type):
        if type == "Single":
            ret = self.botProcessing(None)
            if ret:
                self.timeLabel.setText("")
                gui.alert("Message sent successfully...", "MESSAGE", button=pymsgbox.OK_TEXT)
        else:
            for i in phoneNumberList:
                ret = self.botProcessing(i)
                if ret == False:
                    break
            if ret:
                self.timeLabel.setText("")
                gui.alert("Messages sent successfully...", "MESSAGE", button=pymsgbox.OK_TEXT)

    def botProcessing(self, value=None):
        time.sleep(1)
        buttonLocation = gui.locateOnScreen("white_image.png", confidence=0.85)
        if buttonLocation is None:
            buttonLocation = gui.locateOnScreen("black_image.png", confidence=0.85)
        if buttonLocation is None:
            self.startBotButton.setText("Start Bot")
            self.alert("Bot stopped. Error occurred!")
            return False
        else:
            point = gui.center(buttonLocation)
            gui.moveTo(point)
            time.sleep(0.25)
            gui.moveRel(60, 0)
            time.sleep(0.25)
            gui.click()
            time.sleep(0.25)
            if value is None:
                gui.write(self.phoneNumberInputField.text())
            else:
                gui.write(str(value))
            time.sleep(1)
            gui.moveRel(0, 125)
            time.sleep(0.25)
            gui.click()
            time.sleep(0.5)
            buttonLocation2 = gui.locateOnScreen("white_image2.png", confidence=0.7)
        if buttonLocation2 is None:
            buttonLocation2 = gui.locateOnScreen("black_image2.png", confidence=0.7)
        if buttonLocation2 is None:
            self.startBotButton.setText("Start Bot")
            self.alert("Bot stopped. Error occurred!")
            return False
        else:
            point = gui.center(buttonLocation2)
            time.sleep(0.25)
            gui.moveTo(point)
            time.sleep(0.25)
            gui.moveRel(50, 0)
            time.sleep(0.25)
            gui.click()
            pyperclip.copy(self.writeYourMessage.toPlainText())
            gui.hotkey('ctrl', 'v')
            time.sleep(0.25)
            gui.press("enter")
            self.startBotButton.setText("Start Bot")
            return True

    def selectCSVFile(self, mainWindow):
        try:
            fileName = QFileDialog.getOpenFileName(mainWindow, "Select File", os.getenv("HOME"),
                                                   filter="CSV file (*.csv)")
            self.csvFileDestinationInputField.setText(fileName[0])
            dataSet = pd.read_csv(fileName[0], encoding="ansi")
            global csvFile
            csvFile = dataSet.values.tolist()
            liste = dataSet.values.tolist()
            phoneNumbers = list()
            for i in liste:
                info = "{}{}{}".format(i[0], (20 - len(i[0])) * " ", i[1])
                text = self.contactsField.toPlainText()
                if info not in text:
                    self.contactsField.append(info)
                phoneNumbers.append(i[1])
            global phoneNumberList
            phoneNumberList = phoneNumbers
        except:
            pass

    def dowloadExampleCSV(self, mainWindow):
        file = open("Example CSV File/contacts.csv", "r", encoding="ansi")
        fileName = QFileDialog.getSaveFileName(mainWindow, "Save File", os.getenv("HOME"), filter="CSV File (*.csv)")
        csvFile = list(file.read())
        if (len(fileName[0]) != 0):
            with open(fileName[0], "w", encoding="ansi") as file:
                for i in csvFile:
                    file.write(i)

    def clearText(self):
        self.phoneNumberInputField.setText("")


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