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


class ProductListView(APIView):

    def get_brand_infos(self, product_ids):
        brand_infos = self.rpc['neptune/commodity/brand/products_brand_info'](product_ids=product_ids).unwrap()

        brand_product_dict = {str(obj.get('product_id')): obj for obj in brand_infos} if brand_infos else {}
        return brand_product_dict

    def get_category_infos(self, product_ids):
        category_infos = self.rpc['neptune/commodity/category/products_category_info'](product_ids=product_ids).unwrap()
        ret = {}
        if not category_infos:
            return ret
        for obj in category_infos:
            product_id = obj.get('project_id')
            if ret.get(str(product_id)):
                ret[str(product_id)] = ret.get(str(product_id)).append(obj)
            else:
                ret[str(product_id)] = [obj]
        return ret

    def get_effect_infos(self, product_ids):
        effect_infos = self.rpc['neptune/commodity/effect/products_effect_info'](product_ids=product_ids).unwrap()
        ret = {}
        if not effect_infos:
            return ret
        for obj in effect_infos:
            product_id = obj.get('project_id')
            if ret.get(str(product_id)):
                ret[str(product_id)] = ret.get(str(product_id)).append(obj)
            else:
                ret[str(product_id)] = [obj]
        return ret

    def get(self, request):
        offset, count = get_offset_count(request)
        id_ = request.GET.get('id', None)
        cn_name = request.GET.get('cn_name', None)
        en_name = request.GET.get('en_name', None)
        alias = request.GET.get('alias', None)
        is_online = request.GET.get('is_online', None)
        has_area = request.GET.get('has_area', None)
        has_brand = request.GET.get('has_brand', None)
        has_image = request.GET.get('has_image', None)
        brand_id = request.GET.get('brand_id', None)
        category_id = request.GET.get('category_id', None)
        price_low = request.GET.get('price_low', None)
        price_high = request.GET.get('price_high', None)
        grade_low = request.GET.get('grade_low', None)
        grade_high = request.GET.get('grade_high', None)
        comment_nums_low = request.GET.get('comment_nums_low', None)
        comment_nums_high = request.GET.get('comment_nums_high', None)

        data = self.rpc['neptune/commodity/product/list'](
            offset=offset, count=count, id_=id_, cn_name=cn_name, en_name=en_name, alias=alias, is_online=is_online, has_area=has_area, 
            has_brand=has_brand, has_image=has_image, brand_id=brand_id, category_id=category_id, price_low=price_low, price_high=price_high,
            grade_low=grade_low, grade_high=grade_high, comment_nums_low=comment_nums_low, comment_nums_high=comment_nums_high).unwrap()

        product_ids = [obj.get('id') for obj in data]
        brand_product_dict = self.get_brand_infos(product_ids)
        category_product_dict = self.get_category_infos(product_ids)
        effect_product_dict = self.get_effect_infos(product_ids)

        for obj in data:
            product_id = obj.get('id')
            obj['brand_name'] = brand_product_dict.get(str(product_id)).get('cn_name') if brand_product_dict.get(str(product_id)) else ''
            obj['category_names'] = [obj.get('cn_name') 
                                     for obj in category_product_dict.get(str(product_id))] if category_product_dict.get(str(product_id)) else []
            obj['effect_names'] = [obj.get('cn_name') 
                                   for obj in effect_product_dict.get(str(product_id))] if effect_product_dict.get(str(product_id)) else []

            obj.pop('norms')
            obj.pop('country')
            obj.pop('platform')

        count = self.rpc['neptune/commodity/product/count'](
            id_=id_, cn_name=cn_name, en_name=en_name, alias=alias, is_online=is_online, has_area=has_area, has_brand=has_brand, 
            has_image=has_image, brand_id=brand_id, category_id=category_id, price_low=price_low, price_high=price_high,
            grade_low=grade_low, grade_high=grade_high, comment_nums_low=comment_nums_low, comment_nums_high=comment_nums_high).unwrap()
        result = {
            'data': data,
            'total': count,
        }
        return result


class ProductInfoView(APIView):

    def get_category_names(self, product_id):
        category_infos = self.rpc['neptune/commodity/category/infos'](product_id=product_id).unwrap()
        if not category_infos:
            return []
        return [obj.get('cn_name') for obj in category_infos]

    def get_effect_names(self, product_id):
        effect_infos = self.rpc['neptune/commodity/effect/infos'](product_id=product_id).unwrap()
        if not effect_infos:
            return []
        return [obj.get('cn_name') for obj in effect_infos]

    def get(self, request):
        id_ = request.GET.get('id')

        data = self.rpc['neptune/commodity/product/info'](id_=id_).unwrap()
        data.pop('norms')
        data.pop('country')
        data.pop('platform')

        brand_info = self.rpc['neptune/commodity/brand/infos'](product_id=id_).unwrap()
        data['brand_name'] = brand_info[0].get('cn_name') if brand_info else ''
        data['category_names'] = self.get_category_names(id_)
        data['effect_names'] = self.get_effect_names(id_)

        return data


class ProductCreateView(APIView):

    def post(self, request):
        cn_name = request.POST.get('cn_name', None)
        en_name = request.POST.get('en_name', None)
        alias = request.POST.get('alias', None)
        image = request.POST.get('image', None)
        norms = request.POST.get('norms', None)
        grade = request.POST.get('grade', None)
        price = request.POST.get('price', None)
        country = request.POST.get('country', None)
        description = request.POST.get('description', None)
        comment_nums = request.POST.get('comment_nums', None)
        classify_id = request.POST.get('classify_id', None)
        

        if not cn_name or not image:
            return r'缺少参数'

        data = self.rpc['neptune/commodity/product/create'](
            cn_name=cn_name, en_name=en_name, alias=alias, image=image, norms=norms, grade=grade, price=price,
            country=country, description=description, comment_nums=comment_nums, classify_id=classify_id).unwrap()

        return data


class ProductUpdateView(APIView):
    
    def post(self, request):
        id_ = request.POST.get('id')
        cn_name = request.POST.get('cn_name', None)
        en_name = request.POST.get('en_name', None)
        alias = request.POST.get('alias', None)
        image = request.POST.get('image', None)
        norms = request.POST.get('norms', None)
        grade = request.POST.get('grade', None)
        price = request.POST.get('price', None)
        country = request.POST.get('country', None)
        description = request.POST.get('description', None)
        comment_nums = request.POST.get('comment_nums', None)
        classify_id = request.POST.get('classify_id', None)
        

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

        data = self.rpc['neptune/commodity/product/update'](
            id_=id_, cn_name=cn_name, en_name=en_name, alias=alias, image=image, norms=norms, grade=grade, price=price,
            country=country, description=description, comment_nums=comment_nums, classify_id=classify_id).unwrap()

        return data
