# Path: cognitive/app/models.py
# class Experiment(models.Model):
#     EXPERIMENT_STATUS = (
#         ('Stop', ' Saved as stop'),
#         ('InComplete', 'More information required'),
#         ('InExecution', 'Execution started'),
#         ('Error', ' Error when executed'),
#         ('Completed', 'Executed Successfully'),
#     )
#     user = models.ForeignKey(User)
#     name = models.CharField(max_length=50)
#     status = models.CharField(max_length=50, choices=EXPERIMENT_STATUS, default='Stop')
#     created_time = models.DateTimeField(auto_now=True)
#     modified_time = models.DateTimeField(auto_now=True)
#     execution_start_time = models.DateTimeField(blank=True, null=True)
#     execution_end_time = models.DateTimeField(blank=True, null=True)
#     component_start_id = models.IntegerField(blank=True, null=True)
# 
#     class Meta:
#         ordering = ['-modified_time']
# 
# Path: cognitive/app/serializers.py
# class ExperimentSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Experiment
#         # fields = ('created_time', 'modified_time',
#         #        'execution_start_time', 'execution_end_time', 'component_start_id')
#         # read_only_fields = ('created_time', 'modified_time',
#         #        'execution_start_time', 'execution_end_time', 'component_start_id')
#         # write_only_fields = ('created_time', 'modified_time',
#         #        'execution_start_time', 'execution_end_time', 'component_start_id')
# 
# Path: cognitive/app/views.py
# def send_response(method, serializer):
#     if method == 'GET':
#         return Response(serializer.data)
#     elif method == 'POST':
#         if serializer.is_valid():
#             return Response(serializer.data, status=status.HTTP_201_CREATED)
#         else:
#             return Response(
#                 serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#     elif method == 'PUT':
#         if serializer.is_valid():
#             return Response(serializer.data)
#         else:
#             return Response(
#                 serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#     elif method == 'DELETE':
#         return Response(status=status.HTTP_204_NO_CONTENT)


from ..models import Experiment
from ..serializers import ExperimentSerializer
from ..views import send_response
from rest_framework import viewsets

class ExperimentViewSet(viewsets.ViewSet):
    def list(self, request):
        """
        Lists all experiments for a particular user
        ---
        """
        exp = Experiment.objects.all()
        serializer = ExperimentSerializer(exp, many=True)
        return send_response(request.method, serializer)

    def retrieve(self, request, pk=None):
        """
        Retrieve an experiment for a particular user
        ---
        """
        exp = Experiment.objects.get(pk=pk)
        serializer = ExperimentSerializer(exp)
        return send_response(request.method, serializer)

    def create(self, request):
        """
        Create an experiment for a particular user
        ---
        request_serializer: ExperimentSerializer
        """
        serializer = ExperimentSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()


# Path: cognitive/app/models.py
# class Experiment(models.Model):
#     EXPERIMENT_STATUS = (
#         ('Stop', ' Saved as stop'),
#         ('InComplete', 'More information required'),
#         ('InExecution', 'Execution started'),
#         ('Error', ' Error when executed'),
#         ('Completed', 'Executed Successfully'),
#     )
#     user = models.ForeignKey(User)
#     name = models.CharField(max_length=50)
#     status = models.CharField(max_length=50, choices=EXPERIMENT_STATUS, default='Stop')
#     created_time = models.DateTimeField(auto_now=True)
#     modified_time = models.DateTimeField(auto_now=True)
#     execution_start_time = models.DateTimeField(blank=True, null=True)
#     execution_end_time = models.DateTimeField(blank=True, null=True)
#     component_start_id = models.IntegerField(blank=True, null=True)
# 
#     class Meta:
#         ordering = ['-modified_time']
# 
# Path: cognitive/app/serializers.py
# class ExperimentSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Experiment
#         # fields = ('created_time', 'modified_time',
#         #        'execution_start_time', 'execution_end_time', 'component_start_id')
#         # read_only_fields = ('created_time', 'modified_time',
#         #        'execution_start_time', 'execution_end_time', 'component_start_id')
#         # write_only_fields = ('created_time', 'modified_time',
#         #        'execution_start_time', 'execution_end_time', 'component_start_id')
# 
# Path: cognitive/app/views.py
# def send_response(method, serializer):
#     if method == 'GET':
#         return Response(serializer.data)
#     elif method == 'POST':
#         if serializer.is_valid():
#             return Response(serializer.data, status=status.HTTP_201_CREATED)
#         else:
#             return Response(
#                 serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#     elif method == 'PUT':
#         if serializer.is_valid():
#             return Response(serializer.data)
#         else:
#             return Response(
#                 serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#     elif method == 'DELETE':
#         return Response(status=status.HTTP_204_NO_CONTENT)


# Path: cognitive/app/api/experiments.py
from ..models import Experiment
from ..serializers import ExperimentSerializer
from ..views import send_response
from rest_framework import viewsets


class ExperimentViewSet(viewsets.ViewSet):
    def list(self, request):
        """
        Lists all experiments for a particular user
        ---
        """
        exp = Experiment.objects.all()
        serializer = ExperimentSerializer(exp, many=True)
        return send_response(request.method, serializer)

    def retrieve(self, request, pk=None):
        """
        Retrieve an experiment for a particular user
        ---
        """
        exp = Experiment.objects.get(pk=pk)
        serializer = ExperimentSerializer(exp)
        return send_response(request.method, serializer)

    def create(self, request):
        """
        Create an experiment for a particular user
        ---
        request_serializer: ExperimentSerializer
        """
        serializer = ExperimentSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()


        return send_response(request.method, serializer)