import json
import requests

from django.contrib.auth.decorators import login_required
from django.core import serializers

from django.http import HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

from machine.models import Warp_Machine, Knit_Machine

# Create your views here.

@login_required(login_url='/')
def showMachine(request):

    return render(request, 'machine.html', {'title': 'Machine'})

def setMachine(request):
    jsonData = requests.get("http://tnswebserver.iptime.org:1978/values/trs").json()

    for trsObj in jsonData["trs"]:
        print(trsObj["trs_code"])

        if Knit_Machine.objects.filter(trs_code=trsObj["trs_code"]).count() == 0:
            trsMachineObj = Knit_Machine()
            trsMachineObj.trs_code = trsObj["trs_code"] if trsObj["trs_code"] != "" else None
            trsMachineObj.user_id = trsObj["user_id"] if trsObj["user_id"] != "" else None
            trsMachineObj.company_code = trsObj["company_code"] if trsObj["company_code"] != "" else None
            trsMachineObj.trs_installation_time = trsObj["trs_installation_time"] if trsObj["trs_installation_time"] != "" else None
            trsMachineObj.trs_model_name = trsObj["trs_model_name"] if trsObj["trs_model_name"] != "" else None
            trsMachineObj.trs_beam_cnt = trsObj["trs_beam_cnt"] if trsObj["trs_beam_cnt"] != "" else None
            trsMachineObj.trs_bar_cnt = trsObj["trs_bar_cnt"] if trsObj["trs_bar_cnt"] != "" else None
            trsMachineObj.trs_eac_enable = trsObj["trs_eac_enable"] if trsObj["trs_eac_enable"] != "" else None
            trsMachineObj.trs_tempo = trsObj["trs_tempo"] if trsObj["trs_tempo"] != "" else None
            trsMachineObj.trs_lowmotor_enable = trsObj["trs_lowmotor_enable"] if trsObj["trs_lowmotor_enable"] != "" else None
            trsMachineObj.trs_gauge = trsObj["trs_gauge"] if trsObj["trs_gauge"] != "" else None
            trsMachineObj.trs_ip = trsObj["trs_ip"] if trsObj["trs_ip"] != "" else None

            trsMachineObj.save()

    print("")

    jsonData = requests.get("http://tnswebserver.iptime.org:1978/values/tws").json()

    for tswObj in jsonData["tws"]:
        print(tswObj["tws_code"])

        if Warp_Machine.objects.filter(tws_code=tswObj["tws_code"]).count() == 0:
            trsMachineObj = Warp_Machine()
            trsMachineObj.tws_code = tswObj["tws_code"] if tswObj["tws_code"] != "" else None
            trsMachineObj.user_id = tswObj["user_id"] if tswObj["user_id"] != "" else None
            trsMachineObj.company_code = tswObj["company_code"] if tswObj["company_code"] != "" else None
            trsMachineObj.tws_name = tswObj["tws_name"] if tswObj["tws_name"] != "" else None
            trsMachineObj.tws_installation_time = tswObj["tws_installation_time"] if tswObj["tws_installation_time"] != "" else None
            trsMachineObj.tws_ip = tswObj["tws_ip"] if tswObj["tws_ip"] != "" else None

            trsMachineObj.save()

    return HttpResponse(json.dumps('{success: true}'), content_type='application/json')

@csrf_exempt
def listByType(request):

    machineType = int(request.POST['machineType'])

    if machineType == 1:
        machineList = Warp_Machine.objects.all()
    else:
        machineList = Knit_Machine.objects.all()

    jsonList = serializers.serialize('json', machineList)

    return HttpResponse(jsonList, content_type="text/json-comment-filtered")