# Python 2 and 3
from __future__ import unicode_literals

from gapipy.models import AdvertisedDeparture
from gapipy.query import Query
from gapipy.resources.base import Resource
from gapipy.resources.booking_company import BookingCompany
from gapipy.utils import get_resource_class_from_class_name


MAP_IMAGE_TYPE = 'MAP'
BANNER_IMAGE_TYPE = 'BANNER'


class TourDossier(Resource):

    _resource_name = 'tour_dossiers'

    _as_is_fields = [
        'id',
        'href',
        'categories',
        'description',
        'details',
        'product_line',
        'geography',
        'images',
        'name',
        'site_links',
        'slug',
    ]
    _date_fields = [
        'departures_start_date',
        'departures_end_date',
    ]
    _resource_fields = [
        ('tour', 'Tour'),
    ]
    _resource_collection_fields = [
        ('departures', 'Departure'),
    ]
    _model_collection_fields = [
        ('advertised_departures', AdvertisedDeparture),
        ('booking_companies', BookingCompany),
        ('structured_itineraries', 'Itinerary'),
    ]

    def _set_resource_collection_field(self, field, value):
        """
        Overridden to ensure that the `departures` query has the right
        parent resource (i.e. the tour and not the tour dossier).
        """
        if field == 'departures':
            resource_cls = get_resource_class_from_class_name('Departure')
            # Tour dossiers always have the same id as the corresponding tour
            parent = ('tours', self.id, None)
            query = Query(self._client, resource_cls, parent=parent, raw_data=value)
            setattr(self, field, query)
        else:
            return super(TourDossier, self)._set_resource_collection_field(field, value)

    def _get_image_url(self, image_type):
        for image in self.images:
            if image['type'] == image_type:
                return image['image_href']

    def get_map_url(self):
        return self._get_image_url(MAP_IMAGE_TYPE)

    def get_banner_url(self):
        return self._get_image_url(BANNER_IMAGE_TYPE)

    def get_visited_countries(self):
        return [country['name'] for country in self.geography['visited_countries']]

    def get_trip_detail(self, label):
        for detail in self.details:
            if detail['detail_type']['label'] == label:
                return detail['body']

    def get_category_name(self, label):
        for category in self.categories:
            if category['category_type']['label'] == label:
                return category['name']
