from __future__ import annotations

import httpx
import state
from PyQt6.QtWidgets import (
    QLabel,
    QLineEdit,
    QMessageBox,
    QPushButton,
    QVBoxLayout,
    QWidget,
)
from request_client import RequestClient


class RegisterWidget(QWidget):
    def __init__(self, switcher: state.State):
        super().__init__()

        self.switcher = switcher

        self.username_label = QLabel("Username:")
        self.username_input = QLineEdit()

        self.email_label = QLabel("Email:")
        self.email_input = QLineEdit()

        self.password_label = QLabel("Password:")
        self.password_input = QLineEdit()
        self.password_input.setEchoMode(QLineEdit.EchoMode.Password)

        self.register_button = QPushButton("Register")
        self.register_button.clicked.connect(self.register)

        self.login_button = QPushButton("Switch to Login")
        self.login_button.clicked.connect(self.switcher.switch_to_login)

        layout = QVBoxLayout()
        layout.addWidget(self.username_label)
        layout.addWidget(self.username_input)
        layout.addWidget(self.email_label)
        layout.addWidget(self.email_input)
        layout.addWidget(self.password_label)
        layout.addWidget(self.password_input)
        layout.addWidget(self.register_button)
        layout.addWidget(self.login_button)

        self.setLayout(layout)

    def register(self):
        username = self.username_input.text()
        email = self.email_input.text()
        password = self.password_input.text()

        if not username or not email or not password:
            QMessageBox.warning(self, "Input Error", "All fields are required")
            return

        try:
            response = RequestClient().client.post(
                "/users/register",
                data={
                    "username": username,
                    "email": email,
                    "password": password,
                },
            )
            if response.is_success:
                token = response.json().get("access_token")
                if not token:
                    QMessageBox.critical(
                        self,
                        "Error getting the token",
                        "Error getting the token",
                    )
                self.switcher.login(token)
            else:
                QMessageBox.warning(
                    self, "Error", f"Registration failed: {response.text}"
                )
        except httpx.HTTPError as e:
            print(e)
            QMessageBox.critical(self, "HTTP Error", str(e))


class LoginWidget(QWidget):
    def __init__(self, switcher: state.State):
        super().__init__()

        self.switcher = switcher

        self.username_label = QLabel("Username:")
        self.username_input = QLineEdit()

        self.password_label = QLabel("Password:")
        self.password_input = QLineEdit()
        self.password_input.setEchoMode(QLineEdit.EchoMode.Password)

        self.login_button = QPushButton("Login")
        self.login_button.clicked.connect(self.login)

        self.register_button = QPushButton("Switch to Register")
        self.register_button.clicked.connect(self.switcher.switch_to_register)

        layout = QVBoxLayout()
        layout.addWidget(self.username_label)
        layout.addWidget(self.username_input)
        layout.addWidget(self.password_label)
        layout.addWidget(self.password_input)
        layout.addWidget(self.login_button)
        layout.addWidget(self.register_button)

        self.setLayout(layout)

    def login(self):
        username = self.username_input.text()
        password = self.password_input.text()

        if not username or not password:
            QMessageBox.warning(
                self, "Input Error", "Email and Password are required"
            )
            return

        try:
            response = RequestClient().client.post(
                "/users/authorize",
                data={"username": username, "password": password},
            )
            if response.is_success:
                access_token = response.json().get("access_token")
                if access_token:
                    self.switcher.login(access_token)
                else:
                    QMessageBox.warning(
                        self, "Error", "No access token received"
                    )
            else:
                QMessageBox.warning(
                    self, "Error", f"Login failed: {response.text}"
                )
        except httpx.HTTPError as e:
            QMessageBox.critical(self, "HTTP Error", str(e))
