#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "chenwei"
# Date: 2018/11/17

from utils.base import APIView, get_offset_count


class CountrySearchView(APIView):
    def get(self, request):
        name = request.GET.get('name')
        try:
            data = self.rpc['venus/sun/country/search'](name=name).unwrap()
        except Exception as e:
            raise e
        return {
            'data': ['{id}:{name}'.format(id=search_data['id'], name=search_data['name']) for search_data in data]
        }


class UserSearchView(APIView):
    def get(self, request):
        name = request.GET.get('name')
        offset, count = get_offset_count(request)
        try:
            data = self.rpc['venus/sun/user/search'](name=name, offset=offset, count=count).unwrap()
        except Exception as e:
            raise e
        return {
            'data': ['{id}:{name}'.format(id=search_data['id'], name=search_data['name']) for search_data in data]
        }


class TagSearchView(APIView):
    def get(self, request):
        offset, count = get_offset_count(request)
        name = request.GET.get('name')
        platform = request.GET.get('platform')
        collection = request.GET.get('collection')
        try:
            data = self.rpc['venus/sun/tag/search'](offset=offset, count=count, name=name, platform=platform, collection=collection).unwrap()
        except Exception as e:
            raise e
        return {
            'data': ['{id}:{name}'.format(id=search_data['id'], name=search_data['name']) for search_data in data]
        }


class CelebritySearchView(APIView):
    def get(self, request):
        name = request.GET.get('name')
        try:
            data = self.rpc['venus/sun/celebrity/search'](name=name).unwrap()
        except Exception as e:
            raise e
        return {
            'data': ['{id}:{name}'.format(id=search_data['id'], name=search_data['name']) for search_data in data]
        }


class CitySearchView(APIView):
    def get(self, request):
        name = request.GET.get('name')
        try:
            data = self.rpc['venus/sun/city/search'](name=name).unwrap()
        except Exception as e:
            raise e
        return {
            'data': ['{id}:{name}'.format(id=search_data['id'], name=search_data['name']) for search_data in data]
        }


class TopicSearchView(APIView):
    def get(self, request):
        name = request.GET.get('name')
        try:
            data = self.rpc['venus/sun/topic/search'](name=name).unwrap()
        except Exception as e:
            raise e
        return {
            'data': ['{id}:{name}'.format(id=search_data['id'], name=search_data['name']) for search_data in data]
        }


class TagTypeSearchView(APIView):
    def get(self, request):
        name = request.GET.get('name')
        try:
            data = self.rpc['venus/sun/tag/tagtype_search'](name=name).unwrap()
        except Exception as e:
            raise e
        return {
            'data': ['{id}:{name}'.format(id=search_data['id'], name=search_data['name']) for search_data in data]
        }

    
class PictorialSearchView(APIView):
    def get(self, request):
        name = request.GET.get('name')
        try:
            data = self.rpc['venus/sun/pictorial/search'](name=name).unwrap()
        except Exception as e:
            raise e

        return {
            'data': ['{id}:{name}'.format(id=search_data['id'], name=search_data['name']) for search_data in data]
        }
