# -*- coding: utf-8 -*-

#  Copyright © 2011-2013  B. Clausius <barcc@gmx.de>
#
#  This program 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 program 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/>.

from __future__ import print_function, division

import os

from gi.repository import GObject
from gi.repository import Gtk

from bzr_panel.dialog import error_dialog


class InitDialog(GObject.Object):
    """Dialog to initialize branch and repository"""
    _ui_file = os.path.splitext(__file__)[0] + '.ui'
    
    __gsignals__ = {
        'init-repository':  (GObject.SignalFlags.RUN_FIRST, None, (str,)),
        'init-branch':    (GObject.SignalFlags.RUN_FIRST, None, (str,)),
    }
    
    def __init__(self, path, parent=None):
        GObject.Object.__init__(self)
        builder = Gtk.Builder()
        builder.add_from_file(self._ui_file)
        
        self.dialog_initialize = builder.get_object("dialog_initialize")
        self.dialog_initialize.set_transient_for(parent)
        
        self.entry_repository = builder.get_object("entry_repository")
        self.entry_branch = builder.get_object("entry_branch")
        self.entry_repository.set_text(os.path.dirname(path))
        self.entry_branch.set_text(path)
        
        button_init_repo = builder.get_object('button_init_repo')
        button_init_branch = builder.get_object('button_init_branch')
        button_init_repo.connect('clicked', self.on_button_init_repo_clicked)
        button_init_branch.connect('clicked', self.on_button_init_branch_clicked)
        
    def run(self):
        return self.dialog_initialize.run()
        
    def destroy(self):
        return self.dialog_initialize.destroy()
        
    def on_button_init_repo_clicked(self, button):
        location = self.entry_repository.get_text()
        if not os.path.isabs(location):
            error_dialog('Initialize repository',
                         'Repository path is not absolut', self.dialog_initialize)
        elif not os.path.isdir(location):
            error_dialog('Initialize repository',
                         'Repository path does not exist', self.dialog_initialize)
        else:
            self.emit('init-repository', location)
            
    def on_button_init_branch_clicked(self, button):
        location = self.entry_branch.get_text()
        if not os.path.isabs(location):
            error_dialog('Initialize branch',
                         'Branch path is not absolut', self.dialog_initialize)
        elif not os.path.isdir(location):
            error_dialog('Initialize branch',
                         'Branch path does not exist', self.dialog_initialize)
        else:
            self.emit('init-branch', location)
            
            
