# copyright 2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr -- mailto:contact@logilab.fr
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 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 Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""cubicweb-prov views/forms/actions/components for web ui"""

from cubicweb import tags
from cubicweb.predicates import partial_relation_possible
from cubicweb.view import EntityView, AnyRsetView
from cubicweb.web.views import uicfg

_ = unicode

afs = uicfg.autoform_section
pvs = uicfg.primaryview_section
abaa = uicfg.actionbox_appearsin_addmenu

pvs.tag_object_of(('*', 'used', '*'), 'hidden')
afs.tag_object_of(('*', 'used', '*'), 'main', 'hidden')
abaa.tag_object_of(('*', 'used', '*'), False)

pvs.tag_object_of(('*', 'generated', '*'), 'hidden')
afs.tag_object_of(('*', 'generated', '*'), 'main', 'hidden')
abaa.tag_object_of(('*', 'generated', '*'), False)

pvs.tag_object_of(('*', 'associated_with', 'Agent'), 'hidden')
afs.tag_object_of(('*', 'associated_with', 'Agent'), 'main', 'hidden')
abaa.tag_object_of(('*', 'associated_with', 'Agent'), False)

afs.tag_subject_of(('*', 'derived_from', '*'), 'main', 'hidden')
afs.tag_object_of(('*', 'derived_from', '*'), 'main', 'hidden')


class I18NView(AnyRsetView):
    __regid__ = 'i18n'

    def cell_call(self, row, col, **kwargs):
        value = self.cw_rset.rows[row][col]
        if value is not None:
            self.w(self._cw._(value))


class RelatedEntitiesView(EntityView):
    """Abstract view to display related entities in the context of an other"""
    __abstract__ = True
    __select__ = partial_relation_possible()
    rtype = None
    role = None # 'subject'/'object'
    rql = None  # RQL used to build the table.
    empty_msg = None  # Message to be displayed in case there's no target to relation.

    @property
    def title(self):
        return self._cw.__(self.rtype + ('_object' if self.role == 'object' else ''))

    def entity_call(self, entity, **kwargs):
        rset = self._cw.execute(self.rql + ', X eid %(x)s', {'x': entity.eid})
        if rset or self.empty_msg:
            self.w(tags.h2(self.title))
        if rset:
            self._cw.view('table', rset, w=self.w, cellvids={0: 'i18n'})
        elif self.empty_msg:
            self.w(tags.div(self._cw._(self.empty_msg), klass="alert alert-info"))


class ActivityGeneratedView(RelatedEntitiesView):
    """View for Activity, to be displayed in the context of the entity generated by the activity.
    """
    __regid__ = 'prov.activity-generated'
    rtype = 'generated'
    role = 'object'
    empty_msg = _('no activity')
    rql = ('Any T,DE,D,A ORDERBY D DESC '
           'WHERE E generated X, E associated_with A?, E type T, E start D,'
           '      E description DE')


class ActivityAssociatedWithView(RelatedEntitiesView):
    """View for Activity, to be displayed in the context of the Agent associated with the Activity.
    """
    __regid__ = 'prov.activity-associated-with'
    rtype = 'associated_with'
    role = 'object'
    rql = ('Any T,DE,D,A ORDERBY D DESC WHERE '
           'E associated_with X, E generated A, E type T, E start D,'
           'E description DE')
