from django.http import HttpResponse, JsonResponse

from BaseLib.BaseView import BaseView
from BaseLib.configurator import *
from Platform import settings
from experiments.models import Experiment


class ExperimentsView(BaseView):
    required_login = True
    endpoint = ''
    view_file = 'experiments.html'

    def pre_handle(self):
        if 'stage' not in self.request.GET or self.request.GET['stage'] not in ['development', 'production']:
            return '/experiments/?stage=production'
        self.stage = self.request.GET['stage']
        self.context['stage'] = self.stage

    def get(self):
        # self.context['experiments'] = Experiment.objects.filter(project=self.request.user.selected_project,
        #                                                 stage=self.stage).order_by('name')
        self.context['experiments'] = get_experiments(self.request.user.selected_project.name, self.stage)

    def post_create(self):
        # Experiment.objects.create(project=self.request.user.selected_project, stage=self.stage, name=self.request.POST['name'])
        create_experiment(self.request.user.selected_project.name, self.stage, self.request.POST['name'])
        return '/experiments/?stage=' + self.stage

    def post_delete(self):
        # Experiment.objects.get(id=self.request.POST['experiment_id']).delete()
        delete_experiment(self.request.POST['experiment_id'])
        return '/experiments/?stage=' + self.stage
    
    def post_change(self):
        # exp = Experiment.objects.get(id=self.request.POST['experiment_id'])
        # exp.enabled = 'enabled' in self.request.POST
        # condition = self.request.POST['condition_select']
        # if condition == 'Другое (только для техлидов)':
        #     exp.condition = self.request.POST['condition']
        # elif condition == 'Никому':
        #     exp.condition = 'False'
        # elif condition == 'Только админам':
        #     exp.condition = 'user.is_superuser'
        # elif condition == 'Только сотрудникам':
        #     exp.condition = 'user.platform_staff'
        # else:
        #     exp.condition = 'True'
        # exp.save()
        enabled = 'enabled' in self.request.POST
        condition = self.request.POST['condition_select']
        if condition == 'Другое (только для техлидов)':
            condition = self.request.POST['condition']
        elif condition == 'Никому':
            condition = 'False'
        elif condition == 'Только админам':
            condition = 'user.is_superuser'
        elif condition == 'Только сотрудникам':
            condition = 'user.platform_staff'
        else:
            condition = 'True'
        update_experiment(self.request.POST['experiment_id'], enabled, condition)
        return '/experiments/?stage=' + self.stage


def get_experiment(request):
    if request.headers.get("X-Security-Token") != settings.PLATFORM_SECURITY_TOKEN:
        return HttpResponse('', status=403)
    project = request.GET.get('project')
    stage = request.GET.get('stage')
    name = request.GET.get('name')
    if project is None or stage is None or name is None:
        return HttpResponse('', status=400)
    experiment = Experiment.objects.filter(stage=stage, project__name=project, name=name).first()
    if experiment is None:
        return HttpResponse('', status=404)
    return JsonResponse({
        'enabled': experiment.enabled,
        'condition': experiment.condition
    })
