import json
from utils.base import APIView, get_offset_count
from utils.logger import error_logger


class ActivityListView(APIView):

    def get(self, request):
        offset, count = get_offset_count(request)
        name = request.GET.get('name', None)
        is_online = request.GET.get('is_online', None)

        data = self.rpc['venus/sun/activity/list'](offset=offset, count=count, name=name, is_online=is_online).unwrap()
        return data


class ActivityCreateView(APIView):

    def post(self, request):
        name = request.POST.get('name', None)
        un_join_url = request.POST.get('un_join_url', None)
        in_join_url = request.POST.get('in_join_url', None)
        agreement_url = request.POST.get('agreement_url', None)

        if not name:
            return r'缺少参数'

        data = self.rpc['venus/sun/activity/add'](name=name, un_join_url=un_join_url, in_join_url=in_join_url, agreement_url=agreement_url).unwrap()

        return data


class ActivityUpdateView(APIView):
    
    def post(self, request):
        id_ = request.POST.get('id', None)
        name = request.POST.get('name', None)
        un_join_url = request.POST.get('un_join_url', None)
        in_join_url = request.POST.get('in_join_url', None)
        agreement_url = request.POST.get('agreement_url', None)
        rank = request.POST.get('rank', None)

        if not id_:
            return r'缺少参数'

        data = self.rpc['venus/sun/activity/edit'](id_=id_, name=name, un_join_url=un_join_url, in_join_url=in_join_url, agreement_url=agreement_url, rank=rank).unwrap()
        if not data:
            return r'更新失败'

        return data


class ActivityDeleteView(APIView):
    
    def post(self, request):
        id_ = request.POST.get('id', None)

        if not id_:
            return r'缺少参数'

        data = self.rpc['venus/sun/activity/delete'](id_=id_).unwrap()
        if not data:
            return r'操作失败'

        return data
