""" revert dialog

This file is part of Turtle.

Turtle 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.

Turtle 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 Turtle. If not, see <https://www.gnu.org/licenses/>. 
"""
from turtlevcs.dialogs import Notification, Question
from turtlevcs.dialogs.base import DialogBase



class RevertFileDialog(Question, DialogBase):
    """ revert file dialog """
    def __init__(self, path):
        Question.__init__(self, "Revert", self._on_response)
        DialogBase.__init__(self, path)

        file = self.turtle.get_relative_file_path(path)

        _, wt_new = self.turtle.can_be_reverted(file, raise_ex=True)
        self.set_body(f"Do you want to {'delete' if wt_new else 'revert'} '{file}'?")

    def _on_response(self, _widget, response):
        if response == "yes":
            try:
                file = self.turtle.get_relative_file_path(self.path)
                self.turtle.revert(file)

                title = "Reverted"
                message = f"File '{file}' reverted"
            except Exception as ex:
                title = "Revert Failed"
                message = f"Revert of file '{file}' has failed: {str(ex)}"

            notification = Notification(message, title)
            self.show_new_window(notification)
