#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import View

from Model.models import User_Brand, Device_User
from Object.ResponseObject import ResponseObject
from Object.TokenObject import TokenObject


class UserBrandV2(View):
    @method_decorator(csrf_exempt)
    def dispatch(self, *args, **kwargs):
        return super(UserBrandV2, self).dispatch(*args, **kwargs)

    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation')
        request_dict = request.GET
        return self.validate(request_dict, operation)

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

    def validate(self, request_dict, operation):
        token = request_dict.get('token', None)

        response = ResponseObject()
        token = TokenObject(token)

        if token.code != 0:
            return response.json(token.code)

        if operation == 'add':
            return self.do_add(token.userID, request_dict, response)
        elif operation == 'query':
            return self.do_query(token.userID, request_dict, response)
        else:
            return response.json(404)

    def do_add(self, userID, request_dict, response):
        deviceSupplier = request_dict.get('deviceSupplier', None)
        deviceModel = request_dict.get('deviceModel', None)
        osType = request_dict.get('osType', None)
        osVersion = request_dict.get('osVersion', None)

        if userID and deviceSupplier and deviceModel and osType and osVersion:
            ub_qs = User_Brand.objects.filter(userID=userID)
            if ub_qs.exists():
                update = {
                    'deviceSupplier': deviceSupplier,
                    'deviceModel': deviceModel,
                    'osType': osType,
                    'osVersion': osVersion
                }
                ub_qs.update(**update)
                return response.json(0)
            else:
                user = Device_User.objects.filter(userID=userID)
                if not user.exists():
                    return response.json(104)
                data = {
                    'userID': user[0],
                    'deviceSupplier': deviceSupplier,
                    'deviceModel': deviceModel,
                    'osType': osType,
                    'osVersion': osVersion
                }
                User_Brand.objects.create(**data)
                return response.json(0)
        else:
            return response.json(444)

    def do_query(self, userID, request_dict, response):

        if not userID:
            return response.json(444)
        else:
            ub_qs = User_Brand.objects.filter(userID=userID)
            data = None
            if ub_qs.exists():
                data = ub_qs.values('id', 'deviceSupplier', 'deviceModel', 'osType', 'osVersion')[0]

            return response.json(0, {'data': data})