from PyQt5.QtWidgets import *
from Moodipy.UserSummary import Person
from Moodipy.DiscoverPgGUI import DiscoverPG
from Moodipy.SpotifyAuthorization import Authorization
from screeninfo import get_monitors
from os import path

class UserLoginPG(QMainWindow):
    def __init__(self):
        max_screen_width = 1536
        min_screen_width = 1000

        max_screen_height = 864
        min_screen_height = 610

        super().__init__()
        self.title = "Login"
        self.desktop = QApplication.desktop()
        self.left = 0
        self.top = 0
        temp_width = get_monitors()[0].width * .5
        self.width = max(min(temp_width, max_screen_width), min_screen_width)
        temp_height = get_monitors()[0].height * .5
        self.height = max(min(temp_height, max_screen_height), min_screen_height)
        self.initUI()

    def initUI(self):
        self.sw = (self.width / 1000)
        self.sh = (self.height / 610)
        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)
        self.setStyleSheet("background-color: #ccccff")
        self.mood_window()
        self.show()

    def mood_window(self):
        Person.setLabel(self, "", True, self.sw*290, self.sh*100, self.sw*460, self.sh*400, 0, "#f2ccff", False, 'Consolas')
        Person.setLabel(self, "Log In", False, self.sw*470, self.sh*130, self.sw*100, self.sh*39, self.sw*20, "#f2ccff", True, 'Consolas')
        Person.setLabel(self, "Login here using your Spotify username", False, self.sw*400, self.sh*170, self.sw*300, self.sh*39, self.sw*8, "#f2ccff", False, 'Consolas')
        Person.setLabel(self, "Username", False, self.sw*380, self.sh*230, self.sw*100, self.sh*30, self.sw*13, "#f2ccff", False, 'Consolas')
        self.username = QLineEdit(self)
        self.username.setGeometry(self.sw*380, self.sh*260, self.sw*270, self.sh*29)
        self.username.setStyleSheet("background-color: white")

        self.loginbtn = QPushButton("LOGIN", self)
        self.loginbtn.setGeometry(self.sw*450, self.sh*380, self.sw*150, self.sh*40)
        self.loginbtn.setStyleSheet("color: rgb(255, 255, 255); background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 #ccccff, stop:1 rgb(240, 53, 218)); border-style: solid; border-radius:20px;")
        self.loginbtn.clicked.connect(self.on_click)

    def on_click(self):
        self.loginbtn.setEnabled(False)
        Person.userID = (self.username.text())
        if Authorization() == None:
            self.pop_up()
        else:
            userInfoPath = path.join(path.dirname(__file__), "UserInfo.txt")
            f = open(userInfoPath, "w+")
            f.write(self.username.text())
            f.close()
            self.nextPg = DiscoverPG()
            self.nextPg.show()
            self.hide()

    def pop_up(self):
        msg = QMessageBox.question(self, 'Invalid Username', 'Please enter a valid username', QMessageBox.Ok)
        self.loginbtn.setEnabled(True)


