from PyQt5.QtWidgets import *


class GraphRequester(QWidget):
    def __init__(self, nomer_grafika=1):
        QWidget.__init__(self)
        layout = QVBoxLayout(self)
        layout_x = QHBoxLayout()
        layout_y = QHBoxLayout()
        layout_close_and_name = QHBoxLayout()
        self.LineEditGraf = QLineEdit(f"график {nomer_grafika}")
        NameX = QLabel("X")
        NameY = QLabel("Y")
        Name_Close = QPushButton("Закрыть")

        self.LineEditX = QLineEdit()
        self.LineEditY = QLineEdit()

        layout_x.addWidget(NameX)
        layout_x.addWidget(self.LineEditX)

        layout_y.addWidget(NameY)
        layout_y.addWidget(self.LineEditY)

        Name_Close.clicked.connect(lambda: self.setParent(None))

        layout_close_and_name.addWidget(self.LineEditGraf)
        layout_close_and_name.addWidget(Name_Close)

        layout.addLayout(layout_close_and_name)  # Вложения названия и закрыть
        layout.addLayout(layout_x)  # Вложение
        layout.addLayout(layout_y)  # Вложение


class ButtonGroup(QWidget):
    def __init__(self, category: str, full_names={}, buttons_actions={}, parent=None):
        super().__init__()
        self.layout = QVBoxLayout()  # Создание основного лаяутв
        Doplayout = QHBoxLayout()
        label = QLabel(category)

        self.layout.addWidget(label)
        for button_name in full_names:
            button = QPushButton(button_name, self)

            button.setFixedWidth(80)
            button.setToolTip(
                full_names[button_name]
            )  # Создание подскачоки при наведении

            button.clicked.connect(
                buttons_actions[button_name]
            )  # Назначение кнопочке действия

            Doplayout.addWidget(button)  # отрисовывание кнопок

        self.layout.addLayout(Doplayout)
        self.setLayout(self.layout)


# ButtonGroup(
#     name="Function",
#     full_names={"a": "A", "b": "B", "c": "C"},
#     buttons_actions={
#         "a": lambda: print("A"),
#         "b": lambda: print("B"),
#         "c": lambda: None,
#     },
# )

# GraphRequester(close=lambda x: x)


class PlotDialog(QDialog):
    def __init__(
        self,
        variable_full_names: dict[str, str],
        function_full_names: dict[str, str],
        # variable_values: dict[str, np.ndarray],
    ):
        super().__init__()
        layout_boss = QVBoxLayout()  # главный лояут

        scroll = QScrollArea()
        scrollWidget = QWidget()

        self.inputs_layout = QVBoxLayout()  # лаяут первой трети
        self.num_of_input = 0  # инициализация первого графика
        self.add_input()
        # first_layout = add_input(num_of_input) # Установка начального окна ввода

        scrollWidget.setLayout(self.inputs_layout)

        scroll.setWidgetResizable(True)
        scroll.setWidget(scrollWidget)

        layout_boss.addWidget(scroll)

        Button_make_fun_button = QPushButton("+")
        Button_make_fun_button.clicked.connect(self.add_input)
        Button_make_fun_button.setFixedWidth(80)
        layout_boss.addWidget(Button_make_fun_button)

        layout_boss.addWidget(
            ButtonGroup(
                "Переменные",
                full_names=variable_full_names,
                buttons_actions={
                    "a": lambda: print("A"),
                    "b": lambda: print("B"),
                    "c": lambda: None,
                },
            )
        )
        layout_boss.addWidget(
            ButtonGroup(
                "Функции",
                full_names=function_full_names,
                buttons_actions={
                    "exp": lambda: print("exp"),
                    "ln": lambda: print("ln"),
                    "mod": lambda: None,
                },
            )
        )

        self.setLayout(layout_boss)

    def add_input(self):
        self.num_of_input += 1
        self.inputs_layout.addWidget(GraphRequester(self.num_of_input))


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    w = PlotDialog(
        variable_full_names={"a": "A", "b": "B", "c": "C"},
        function_full_names={"exp": "экспонента", "ln": "Логарифм", "mod": "модуль"},
    )
    w.show()
    sys.exit(app.exec_())
