# -*- encoding: utf-8 -*-
"""
@File    : AlgorithmShopManageController.py
@Time    : 2023/7/25 9:48
@Author  : stephen
@Email   : zhangdongming@asj6.wecom.work
@Software: PyCharm
"""
from django.views import View

from Model.models import DeviceAlgorithmExplain
from Object.ResponseObject import ResponseObject
from Object.TokenObject import TokenObject


class AlgorithmShopManageView(View):
    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation')
        api_version = kwargs.get('apiVersion')
        return self.validation(request.GET, request, operation, api_version)

    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation')
        api_version = kwargs.get('apiVersion')
        return self.validation(request.POST, request, operation, api_version)

    def validation(self, request_dict, request, operation, api_version='v1'):
        token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
        response = ResponseObject()
        if token.code != 0:
            return response.json(token.code)

        ''' 后台管理'''
        response = ResponseObject(returntype='pc')
        if operation == 'save':
            pass
        if operation == 'update':
            return self.algorithm_update(request_dict, response, api_version)
        else:
            return response.json(404)

    @classmethod
    def algorithm_add(cls, request_dict, response):
        pass
        return response.json(0)

    @classmethod
    def algorithm_update(cls, request_dict, response, api_version):
        try:
            a_id = request_dict.get('aId', None)
            lang = request_dict.get('lang', None)
            title = request_dict.get('title', None)
            subtitle = request_dict.get('subtitle', None)
            introduction = request_dict.get('introduction', None)
            install_explain = request_dict.get('installExplain', None)
            concerning = request_dict.get('concerning', None)
            risk_warning = request_dict.get('riskWarning', None)
            if not all([a_id, lang]):
                return response.json()
            a_explain_qs = DeviceAlgorithmExplain.objects.filter(algorithm_type_id=int(a_id), lang=lang)
            if not a_explain_qs.exists():
                return response.json(173)
            data = {}
            if title:
                data['title'] = title
            if subtitle:
                data['subtitle'] = subtitle
            if introduction:
                data['introduction'] = introduction
            if install_explain:
                data['install_explain'] = install_explain
            if concerning:
                data['concerning'] = concerning
            if risk_warning:
                data['risk_warning'] = risk_warning
            a_explain_qs.update(**data)
            return response.json(0)
        except Exception as e:
            print(repr(e))
            return response.json(500)
