from django.shortcuts import render
from django.contrib import messages
from django.shortcuts import redirect
import requests
from django.http import HttpResponse
from articles.views import get_skill
from django.http import HttpResponse
import json
from django.http import JsonResponse




# Create your views here.
def get_machine_data():  # sourcery skip: inline-immediately-returned-variable
   response = requests.get('https://thingsaccess.com/rfid/index.php/Api/getMachine')
   data_send = response.json()
   data = data_send['response']
   if data == 11:
         data = [] 
   return data

def get_machine_state():  # sourcery skip: inline-immediately-returned-variable
   response = requests.get('https://thingsaccess.com/rfid/index.php/Api/getMachineStates')
   data_send = response.json()
   data = data_send['response']
   if data == 11:
         data = [] 
   return data



def machine_page(request):
   data = get_machine_data()
   return render(request, 'machine/machine.html',context={'data': data})

def Add_Machine(request):
   skills = get_skill() 
   if request.method == 'POST':
      data_json = json.loads(request.body)    
      data = {
          'title': data_json .get('title'),
          'device_ID': data_json .get('device_ID'),
          'skill': data_json.get('skill')
          
      }
      response = requests.post('https://thingsaccess.com/rfid/index.php/Api/addMachine', json=data)
      data_send = response.json()
      data_machine = data_send['response']
      return JsonResponse({'data_machine': data_machine})
      # if response.status_code != 200:
      #    # Insertion failed, show error message to user
      #    return HttpResponse("Machine not added successfully")
      # return redirect('machine_page')
   return render(request, 'machine/addMachine.html',context={'skills':skills})


def Edit_Machine(request,id):
   skills = get_skill() 
   state = get_machine_state()    
   response = requests.get('https://thingsaccess.com/rfid/index.php/Api/getMachine')
   data_send = response.json()
   data = data_send['response']
   edit_item = None
   for item in data:
        if item['ID'] == id:
            edit_item = item
            break
   if edit_item:
      if request.method == 'POST':
         data = {
               'id':id,
               'title': request.POST.get('title'),
               'device_ID': request.POST.get('device_ID'),
               'state': request.POST.get('state'),
               'skill': request.POST.get('skill'),
         }
         response = requests.post('https://thingsaccess.com/rfid/index.php/Api/editMachine', json=data)
         if response.status_code == 200:
               return redirect('machine_page')
               # messages.success(request, 'Data added successfully')
         else:
               # Insertion failed, show error message to user
               messages.error(request, 'Failed to edit data')

   return render(request,'machine/editMachine.html',context={'item': edit_item,'skills':skills,'state':state})
  
def delete_Machine(request,id):
   data_delete = {
        'id': id
   }
   print(data_delete)
   response = requests.post('https://thingsaccess.com/rfid/index.php/Api/deleteMachine', json=data_delete)
   if response.status_code != 200:
      messages.error(request, 'Failed to delete data')
   return redirect('machine_page')


   
