# Copyright 2021 Vincent Texier <vit@free.fr>
#
# This software is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import sys
from typing import Optional

from PyQt5.QtWidgets import QApplication, QMenu, QMessageBox, QWidget

from tikka.domains.application import Application
from tikka.domains.entities.constants import DATA_PATH
from tikka.domains.entities.datapod import DataPod
from tikka.domains.entities.events import DataPodsEvent


class DataPodPopupMenu(QMenu):
    """
    DataPodPopupMenu class
    """

    def __init__(
        self,
        application: Application,
        datapod: DataPod,
        parent: Optional[QWidget] = None,
    ):
        """
        Init DataPodPopupMenu instance

        :param application: Application instance
        :param datapod: DataPod instance
        :param parent: QWidget instance
        """
        super().__init__(parent=parent)

        self.application = application
        self.datapod = datapod
        self._ = self.application.translator.gettext

        # menu actions
        copy_url_to_clipboard_action = self.addAction(self._("Copy URL to clipboard"))
        copy_url_to_clipboard_action.triggered.connect(self.copy_url_to_clipboard)

        # Separator
        self.addSeparator()

        forget_datapod_action = self.addAction(self._("Forget server"))
        forget_datapod_action.triggered.connect(self.delete_datapod)

        # Disable "Forget server" if conditions are not met
        if (
            self.datapod.url == self.application.datapods.get_current_url()
            or self.datapod.url
            in self.application.currencies.get_entry_point_urls()[
                self.application.datapods.CONFIG_DATAPOD_ENDPOINTS_KEYWORD
            ]
        ):
            forget_datapod_action.setEnabled(False)

    def copy_url_to_clipboard(self):
        """
        Copy URL to clipboard

        :return:
        """
        clipboard = QApplication.clipboard()
        clipboard.setText(self.datapod.url)

    def delete_datapod(self):
        """
        Forget selected datapod and its entry point

        :return:
        """
        response_button = self.confirm_delete_datapod(self.datapod)
        if response_button == QMessageBox.Yes:
            if (
                self.application.datapods.get_current_url() == self.datapod.url
                and self.application.connections.datapod.is_connected()
            ):
                self.application.connections.datapod.disconnect()
            self.application.datapods.delete(self.datapod.url)
            self.application.event_dispatcher.dispatch_event(
                DataPodsEvent(DataPodsEvent.EVENT_TYPE_LIST_CHANGED)
            )

    def confirm_delete_datapod(self, datapod: DataPod) -> QMessageBox.StandardButton:
        """
        Display confirm dialog and return response

        :param datapod: DataPod instance
        :return:
        """
        # display confirm dialog and get response
        custom_question = self._("Forget server {}?")
        return QMessageBox.question(
            self,
            self._("Forget server"),
            custom_question.format(datapod.url),
        )


if __name__ == "__main__":
    qapp = QApplication(sys.argv)

    application_ = Application(DATA_PATH)
    datapod_ = DataPod(
        "ws://datapod.url.com",
        999,
    )

    menu = DataPodPopupMenu(application_, datapod_)
    menu.exec_()

    sys.exit(qapp.exec_())
