from traits.api import HasTraits, List, Str, Dict, Bool
from traitsui.api import HGroup, Item, VGroup

from pychron.core.helpers.traitsui_shortcuts import okcancel_view
from pychron.core.pychron_traits import EmailStr
from pychron.core.ui.combobox_editor import ComboboxEditor


class GitCommitAuthorView(HasTraits):
    author = Str
    authors = List
    authornames = List
    email = EmailStr
    remember_choice = Bool

    def __init__(self, *args, **kw):
        super(GitCommitAuthorView, self).__init__(*args, **kw)
        self.authornames = [a.name for a in self.authors]

    def _author_changed(self, new):
        if new and new in self.authornames:
            a = next((aa for aa in self.authors if aa.name == new), None)
            self.email = a.email

    def traits_view(self):
        v = okcancel_view(
            VGroup(
                Item("author", editor=ComboboxEditor(name="authornames")), Item("email")
            ),
            Item("remember_choice", label="Remember this choice?"),
            width=400,
            title="Git Commit Author",
        )
        return v


# ============= EOF =============================================
