# -*- coding: utf-8 -*-
from django.shortcuts import render
from rest_framework.views import APIView
from django.views.generic import View
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from rest_framework import status
import json
import logging
from neuron.task import add
import random
import neuron.SaveClosedPossition as SaveClosedPossition
from restfactory.makeDict import makeDict
from restfactory.buildListForCloseNeuronFromDataBase import buildListForCloseNeuronFromDataBase
import string
from neuron.models import DataSet, OpenNeuronTasks, CloseNeuronTasks
from neuron.task import Teach, TeachCloseNet, testtask

__author__ = 'Chugunov Yaroslav <ChugunovYar@gmail.com>'

loggermsg = logging.getLogger('django')

class TestTask(APIView):

	def post(self, request, format=json):
		x = request.data['x']
		y = request.data['y']
		loggermsg.info("test task")
		task = testtask.delay(x,y)
		return Response('testtask request',status=status.HTTP_201_CREATED)

class FromBegin(APIView):

	def post(self, request, format=json):
		loggermsg.info(request.data)
		data = str(request.data)
		d = data.replace('<QueryDict: {u\'','{')
		dd = d.replace('\\x00\':', '')
		data = dd.replace(' [u\'\']}>','}')
		jsonparsed = json.loads(data)
		SaveClosedPossition.saveClosedPossition(jsonparsed)
		#add.delay(4,5)
		loggermsg.info(jsonparsed)

		return Response('post request',status=status.HTTP_201_CREATED)

	def get(self, request, format=json):

		loggermsg.info('get request')
		#add.delay(4,5)
		return Response('get request',status=status.HTTP_201_CREATED)


class RestFactory(APIView):

	def post(self, request, format=json):

		loggermsg.info('post request')
		loggermsg.info(request.data)



		return Response('post request',status=status.HTTP_201_CREATED)

	def get(self, request, format=json):

		loggermsg.info('get request')

		return Response('get request',status=status.HTTP_201_CREATED)


class RunOpenTeach(APIView):

	def get(self, request, format=json):
		loggermsg.info("run open teach")

		filename = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10))
		dictonary = makeDict()
		task = Teach.delay(filename, dictonary)
		openneuron = OpenNeuronTasks(task=task, filename=filename, expired=False)
		openneuron.save()

		# close net

		# filename = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10))
		# existDoneDeals = DataSet.objects.exclude(close_volume_24__isnull=True)
		# dictDataSetForCloseNeuron = buildListForCloseNeuronFromDataBase(existDoneDeals)
		# task = TeachCloseNet.delay(filename, dictDataSetForCloseNeuron)
		# closeneuron = CloseNeuronTasks(task=task, filename=filename, expired=False)
		# closeneuron.save()

		return Response('run open teach', status=status.HTTP_200_OK)

class RunCloseTeach(APIView):

	def get(self, request, format=json):
		loggermsg.info("run close teach")
		filename = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10))
		existDoneDeals = DataSet.objects.exclude(close_volume_24__isnull=True)
		dictDataSetForCloseNeuron = buildListForCloseNeuronFromDataBase(existDoneDeals)
		#task = TeachCloseNet.delay(filename, dictDataSetForCloseNeuron)		
		task = TeachCloseNet.apply_async(args=[filename, dictDataSetForCloseNeuron], time_limit=86400 * 2, soft_time_limit=86400)

		return Response('run close teach', status=status.HTTP_200_OK)
