# Create your views here.
import os
import time
import cx_Oracle
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from workflow.models import Workflow
from workflow_transaccion.models import Workflow_Transaccion
from transacciones.models import Transaccion
from actividades.models import Actividad
from rel_actividad.models import Rel_Actividad
from modulos.models import Modulo
from workflow.serializers import *
class JSONResponse(HttpResponse):
    """
    An HttpResponse that renders it's content into JSON.
    """
    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)

#---Metodo para Modelo
@csrf_exempt
def workflow_list(request):
    if request.method == 'GET':
        actividades = Workflow.objects.all().order_by('nombre')
        serializer = WorkflowSerializer(actividades, many=True)
        response={}
        total={}
        response['status']=0
        response['totalRows']=len(actividades)
        response['startRow']=0
        response['endRow']=len(actividades)-1
        response['data']=serializer.data
        total['response']=response
        return JSONResponse(total)
    elif request.method == 'POST':
        actividades = Workflow.objects.all().order_by('nombre')
        serializer = WorkflowSerializer(actividades, many=True)
        response={}
        total={}
        response['status']=0
        response['totalRows']=len(actividades)
        response['startRow']=0
        response['endRow']=len(actividades)-1
        response['data']=serializer.data
        total['response']=response
        return JSONResponse(total)

@csrf_exempt
def workflow_ingresa(request):
    if request.method == 'GET':
        entrada = request.GET.get('batch')
        batch = ""
        if(entrada == "true"):
            batch = "1"
        else:
            batch = "0"
        nombre = request.GET.get('nombre')
        descripcion = request.GET.get('descripcion')
        if descripcion is None:
            descripcion=""
        json = request.GET.get('json')
        if json is None:
            json=""
        a=Workflow(batch=batch,nombre=nombre,descripcion=descripcion,json=json) 
        a.save() 
        actividades = Workflow.objects.get(id_workflow=a.id_workflow)
        serializer = WorkflowSerializer(actividades)
        id_transaccion = request.GET.get('id_transaccion')
        transaccion=Transaccion.objects.get(id_transaccion=id_transaccion)
        puente=Workflow_Transaccion(id_workflow=actividades,id_transaccion=transaccion)
        puente.save()
        try:
            actividadbase = Actividad.objects.get(id_actividad=0)
        except Exception, e:
            modulo = Modulo.objects.all()
            rtamodulo=list(modulo[:1])
            moduloausar = rtamodulo[0]
            actividadbase = Actividad(id_actividad=0,id_modulo=moduloausar,nombre="Actividad Base", descripcion="Actividad Base",programa=None)
            actividadbase.save()
        entrada=Rel_Actividad(id_actividad_padre=None, id_actividad_hijo=actividadbase, id_workflow=actividades)
        entrada.save()
        response={}
        total={}
        response['status']=0
        response['totalRows']=1
        response['startRow']=0
        response['endRow']=0
        response['data']=serializer.data
        total['response']=response
        return JSONResponse(total)
    if request.method == 'POST':
        entrada = request.POST.get('batch')
        batch = ""
        if(entrada == "true"):
            batch = "1"
        else:
            batch = "0"
        nombre = request.POST.get('nombre')
        descripcion = request.POST.get('descripcion')
        if descripcion is None:
            descripcion=""
        json = request.POST.get('json')
        if json is None:
            json=""
        a=Workflow(batch=batch,nombre=nombre,descripcion=descripcion,json=json) 
        a.save() 
        actividades = Workflow.objects.get(id_workflow=a.id_workflow)
        serializer = WorkflowSerializer(actividades)
        id_transaccion = request.POST.get('id_transaccion')
        transaccion=Transaccion.objects.get(id_transaccion=id_transaccion)
        puente=Workflow_Transaccion(id_workflow=actividades,id_transaccion=transaccion)
        puente.save()
        try:
            actividadbase = Actividad.objects.get(id_actividad=0)
        except Exception, e:
            modulo = Modulo.objects.all()
            rtamodulo=list(modulo[:1])
            moduloausar = rtamodulo[0]
            actividadbase = Actividad(id_actividad=0,id_modulo=moduloausar,nombre="Actividad Base", descripcion="Actividad Base",programa=None)
            actividadbase.save()
        entrada=Rel_Actividad(id_actividad_padre=None, id_actividad_hijo=actividadbase, id_workflow=actividades)
        entrada.save()
        response={}
        total={}
        response['status']=0
        response['totalRows']=1
        response['startRow']=0
        response['endRow']=0
        response['data']=serializer.data
        total['response']=response
        return JSONResponse(total)

@csrf_exempt
def workflow_actualiza(request):
    if request.method == 'GET':
        id_workflow = request.GET.get('id_workflow')
        entrada = request.GET.get('batch')
        batch = ""
        if(entrada == "true"):
            batch = "1"
        else:
            batch = "0"
        nombre = request.GET.get('nombre')
        descripcion = request.GET.get('descripcion')
        if descripcion is None:
            descripcion=""
        json = request.GET.get('json')
        if json is None:
            json=""
        actividades = Workflow.objects.get(id_workflow=id_workflow)
        actividades.batch=batch
        actividades.nombre = nombre
        actividades.descripcion = descripcion
        actividades.json = json
        actividades.save()
        serializer = WorkflowSerializer(actividades)
        response={}
        total={}
        response['status']=0
        response['totalRows']=1
        response['startRow']=0
        response['endRow']=0
        response['data']=serializer.data
        total['response']=response
        return JSONResponse(total)
    if request.method == 'POST':
        id_workflow = request.POST.get('id_workflow')
        entrada = request.POST.get('batch')
        batch = ""
        if(entrada == "true"):
            batch = "1"
        else:
            batch = "0"
        nombre = request.POST.get('nombre')
        descripcion = request.POST.get('descripcion')
        if descripcion is None:
            descripcion=""
        json = request.POST.get('json')
        if json is None:
            json=""
        actividades = Workflow.objects.get(id_workflow=id_workflow)
        actividades.batch=batch
        actividades.nombre = nombre
        actividades.descripcion = descripcion
        actividades.json = json
        actividades.save()
        serializer = WorkflowSerializer(actividades)
        response={}
        total={}
        response['status']=0
        response['totalRows']=1
        response['startRow']=0
        response['endRow']=0
        response['data']=serializer.data
        total['response']=response
        return JSONResponse(total)

@csrf_exempt
def workflow_elimina(request):
    if request.method == 'GET':
        id_workflow = request.POST.get('id_workflow')
        work = Workflow.objects.get(id_workflow=id_workflow)
        work.delete()
        puente = Workflow_Transaccion.objects.filter(id_workflow=id_workflow)
        puente.delete()
        relactividad = Rel_Actividad.objects.filter(id_workflow=id_workflow)
        relactividad.delete()
        borro={}
        devuelve={}
        borro['id_workflow']=id_workflow
        devuelve['response']=borro
        return JSONResponse(devuelve)
    if request.method == 'POST':
        id_workflow = request.POST.get('id_workflow')
        work = Workflow.objects.get(id_workflow=id_workflow)
        work.delete()
        puente = Workflow_Transaccion.objects.filter(id_workflow=id_workflow)
        puente.delete()
        relactividad = Rel_Actividad.objects.filter(id_workflow=id_workflow)
        relactividad.delete()
        borro={}
        devuelve={}
        borro['id_workflow']=id_workflow
        devuelve['response']=borro
        return JSONResponse(devuelve)
