# -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2013, 2014 Sourcefabric z.u. and contributors.
#
# For the full copyright and license information, please see the
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

from superdesk.resource import Resource
from superdesk.services import BaseService
from superdesk.utils import ListCursor
from superdesk import get_resource_service
from superdesk.io.subjectcodes import get_subjectcodeitems


class FilterConditionParametersResource(Resource):
    url = "filter_conditions/parameters"
    resource_methods = ['GET']
    item_methods = []


class FilterConditionParametersService(BaseService):
    def get(self, req, lookup):
        values = self._get_field_values()
        return ListCursor([{'field': 'anpa_category',
                            'operators': ['in', 'nin'],
                            'values': values['anpa_category'],
                            'value_field': 'qcode'
                            },
                           {'field': 'urgency',
                            'operators': ['in', 'nin'],
                            'values': values['urgency'],
                            'value_field': 'qcode'
                            },
                           {'field': 'genre',
                            'operators': ['in', 'nin'],
                            'values': values['genre'],
                            'value_field': 'qcode'
                            },
                           {'field': 'subject',
                            'operators': ['in', 'nin'],
                            'values': values['subject'],
                            'value_field': 'qcode'
                            },
                           {'field': 'priority',
                            'operators': ['in', 'nin'],
                            'values': values['priority'],
                            'value_field': 'qcode'
                            },
                           {'field': 'keywords',
                            'operators': ['in', 'nin', 'like', 'notlike', 'startswith', 'endswith']
                            },
                           {'field': 'slugline',
                            'operators': ['in', 'nin', 'like', 'notlike', 'startswith', 'endswith']
                            },
                           {'field': 'type',
                            'operators': ['in', 'nin'],
                            'values': values['type'],
                            'value_field': 'qcode'
                            },
                           {'field': 'source',
                            'operators': ['in', 'nin', 'like', 'notlike', 'startswith', 'endswith']
                            },
                           {'field': 'headline',
                            'operators': ['in', 'nin', 'like', 'notlike', 'startswith', 'endswith']
                            },
                           {'field': 'ednote',
                            'operators': ['in', 'nin', 'like', 'notlike', 'startswith', 'endswith']
                            },
                           {'field': 'body_html',
                            'operators': ['in', 'nin', 'like', 'notlike', 'startswith', 'endswith']
                            },
                           {'field': 'desk',
                            'operators': ['in', 'nin'],
                            'values': values['desk'],
                            'value_field': '_id'
                            },
                           {'field': 'stage',
                            'operators': ['in', 'nin'],
                            'values': values['stage'],
                            'value_field': '_id'
                            },
                           {'field': 'sms',
                            'operators': ['in', 'nin'],
                            'values': values['sms'],
                            'value_field': 'name'
                            }])

    def _get_field_values(self):
        values = {}
        vocabularies_resource = get_resource_service('vocabularies')
        values['anpa_category'] = vocabularies_resource.find_one(req=None, _id='categories')['items']
        values['genre'] = vocabularies_resource.find_one(req=None, _id='genre')['items']
        values['urgency'] = vocabularies_resource.find_one(req=None, _id='urgency')['items']
        values['priority'] = vocabularies_resource.find_one(req=None, _id='priority')['items']
        values['type'] = vocabularies_resource.find_one(req=None, _id='type')['items']
        values['subject'] = get_subjectcodeitems()
        values['desk'] = list(get_resource_service('desks').get(None, {}))
        values['stage'] = self._get_stage_field_values(values['desk'])
        values['sms'] = [{'qcode': 0, 'name': 'False'}, {'qcode': 1, 'name': 'True'}]
        return values

    def _get_stage_field_values(self, desks):
        stages = list(get_resource_service('stages').get(None, {}))
        for stage in stages:
            desk = next(filter(lambda d: d['_id'] == stage['desk'], desks))
            stage['name'] = '{}: {}'.format(desk['name'], stage['name'])
        return stages

@@ -525,16 +525,52 @@
 deitems%0A
+from eve.utils import ParsedRequest%0A
 %0A%0Aclass 
@@ -4300,32 +4300,137 @@
 ries')%5B'items'%5D%0A
+        req = ParsedRequest()%0A        req.where = %7B'$or': %5B%7B%22schema_field%22: %22genre%22%7D, %7B%22_id%22: %22genre%22%7D%5D%7D%0A
         values%5B'
@@ -4478,25 +4478,11 @@
 req=
-None, _id='gen
 re
-'
+q
 )%5B'i
@@ -4755,32 +4755,202 @@
 type')%5B'items'%5D%0A
+        subject = vocabularies_resource.find_one(req=None, schema_field='subject')%0A        if subject:%0A            values%5B'subject'%5D = subject%5B'items'%5D%0A        else:%0A    
         values%5B'

Fix the generation on field list for filters (SDNTB-258)