"""Subclass of OpenGitRepo, which is generated by wxFormBuilder."""


import wx
import wxx
from activity.git.ui import ui as ui
import app.resources as rc


# Implementing OpenGitRepo
class OpenGitRepoDialog(ui.OpenGitRepo):
    """
    This dialogs allows to select an existing
    repository and integrate it into the workspace

    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, container):
        """Initialize dialog"""
        super(OpenGitRepoDialog, self).__init__(parent)
        self.container = container
        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(rc.GetBitmap("git"))
        self.SetIcon(icon)

    def get_kwargs(self):
        """Returns kwargs dictionary suitable for object creation"""
        kwargs = {}
        kwargs['parent'] = self.container
        kwargs['uri'] = self._path
        kwargs['local'] = True
        return kwargs

    def Validate(self):
        """Validate the dialog"""
        self._path = self.m_dirPicker2.GetPath().strip()
        if len(self._path) == 0:
            wx.MessageBox("Git directory must be specified", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        #really check git repo
        try:
            import git
            repo = git.Repo(self._path)
            if repo.bare:
                wx.MessageBox("The specified dir is a bare Git repository\nOnly working repositories are allowed.", "Error",
                    wx.OK | wx.CENTER | wx.ICON_ERROR, self)
                return False
        except git.exc.InvalidGitRepositoryError:
            wx.MessageBox("The specified dir is not a Git repository", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        except git.exc.NoSuchPathError:
            wx.MessageBox("The specified path is invalid", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        return True

    def OnCancel(self, event):
        """cancel event handler"""
        self.EndModal(wx.ID_CANCEL)

    def OnOK(self, event):
        """Handle OnOk"""
        if self.Validate():
            self.EndModal(wx.ID_OK)
