# -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2020 ITerativ GmbH. All rights reserved.
#
# Created on 25.02.20
# @author: chrigu <christian.cueni@iterativ.ch>
import json

from django.http import JsonResponse
from django.views import View

from core.hep_client import HepClient, HepClientException


class RegistrationProxyView(View):

    def post(self, request, *args, **kwargs):
        hep_client = HepClient()
        data = json.loads(request.body)

        if not self.terms_accepted(data):
            return JsonResponse(
                {
                    'message': 'Sie müssen hier zustimmen, damit Sie sich registrieren können.'
                },
                status=400)

        data['customer']['group_id'] = 5

        try:
            hep_data = hep_client.customer_create(data)
        except HepClientException as e:
            return JsonResponse(e.args[1], status=e.args[0])

        response_data = hep_data.copy()
        del response_data['confirmation']

        return JsonResponse(response_data)

    def terms_accepted(self, data):
        if 'accepted_terms' in data and data['accepted_terms']:
            del data['accepted_terms']
            return True

        return False
