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

from utils.base import APIView
from alpha_types.venus import PICK_TYPE


class PickListView(APIView):
    def get(self, request):
        offset = int(request.GET.get('page', 1))
        limit = int(request.GET.get('limit', 10))
        filter = self.handle_filter(request.GET.get('filter', ""))
        try:
            data = self.rpc['venus/sun/pick/list'](offset=(offset - 1) * limit, limit=limit, filters=filter).unwrap()
        except Exception as e:
            raise e
        return data

    def post(self, request):
        ids = request.POST.get('ids', '').split()
        type = request.POST.get('type', '')

        updates = {}

        if type == 'offline':
            updates['is_online'] = 0
        else:
            updates['is_online'] = 1
        try:
            self.rpc['venus/sun/pick/batch/update'](updates=updates, ids=ids).unwrap()
        except Exception as e:
            raise e
        return {
            "message": "更新成功"
        }


class UpdateOrCreateView(APIView):
    def get(self, request):
        id = request.GET.get('id')
        try:
            data = self.rpc['venus/sun/pick/get'](id=id).unwrap()
        except Exception as e:
            raise e
        return {'data': data}

    def post(self, request):
        id = request.POST.get('id', '')
        data = {
            'name': request.POST.get('name'),
            'desc': request.POST.get('desc'),
            'gender': request.POST.get('gender'),
            'region': list(set(request.POST.get('region', '').split(','))),
            'pick_type': request.POST.get('pick_type'),
            'pick_group': list(set(request.POST.get('pick_group', '').split(','))),
            'position': request.POST.get('position'),
            'is_online': int(request.POST.get('is_online')),
        }
        try:
            self.rpc['venus/sun/pick/edit'](id=id, data=data).unwrap()
        except Exception as e:
            raise e
        return {
            'message': '操作成功'
        }


class PickUserListView(APIView):
    def get(self, request):
        pick_id = request.GET.get('id', 0)
        pick_type = int(request.GET.get('pick_type_origin', 0))
        offset = int(request.GET.get('page', 0))
        limit = int(request.GET.get('limit', 10))
        filters = {
            'pick_id': pick_id
        }
        dispatch_rpc_endpoint = {
            PICK_TYPE.CELEBRITY: 'venus/sun/pick/celebrity/list',
            PICK_TYPE.TOPIC: 'venus/sun/pick/topic/list'
        }

        try:
            data = self.rpc[dispatch_rpc_endpoint[pick_type]](offset=(offset - 1) * limit, limit=limit, filters=filters).unwrap()
        except Exception as e:
            raise e
        return data

    def post(self, request):
        celebrity_id = request.POST.get('celebrity_id', '')
        fake_pick_nums = request.POST.get('fake_pick_nums', 0)
        topic_id = request.POST.get('topic_id', '')
        try:
            self.rpc['venus/sun/pick/add_fake_pick_nums'](celebrity_id=celebrity_id, topic_id=topic_id, pick_nums=fake_pick_nums).unwrap()
        except Exception as e:
            raise e
        return {
            'message': '操作失败'
        }


class PickTopicView(APIView):
    def get(self, request):
        topic_id = request.GET.get('id')
        try:
            data = self.rpc['venus/sun/topic/pick_topic'](id=topic_id).unwrap()
        except Exception as e:
            raise e
        return [data, ]


class PickCelebrityListView(APIView):
    def get(self, request):
        celebrity_id = request.GET.get('id')
        try:
            data = self.rpc['venus/sun/pick/pick_celebrity'](id=celebrity_id).unwrap()
        except Exception as e:
            raise e
        return [data, ]