from __future__ import annotations

import uuid

import file_widgets
from PyQt6.QtWidgets import QLineEdit, QMessageBox, QPushButton, QVBoxLayout, QWidget
from request_client import RequestClient


class CreateFolderWidget(QWidget):
    def __init__(self, folder_id: uuid.UUID, file_list: file_widgets.FileListWidget):
        super().__init__()
        self.folder_id = folder_id
        self.file_list = file_list
        self.setWindowTitle("Folder creation")
        layout = QVBoxLayout()
        self.edit = QLineEdit()
        layout.addWidget(self.edit)
        button = QPushButton("Submit")
        button.clicked.connect(self.submit)
        layout.addWidget(button)
        self.setLayout(layout)

    def submit(self):
        response = RequestClient().client.post(
            "/folders",
            json={
                "folder_name": self.edit.text(),
                "parent_folder_id": str(self.folder_id),
            },
        )
        if not response.is_success:
            QMessageBox.warning(None, "Error creating folder", response.text)
        else:
            QMessageBox.information(None, "Folder created", "Folder created")
            self.file_list.update_response()
        self.close()
