import requests
import json
LOCAL_MODE = False

url = 'https://zubatomic.com/projects/erudite/server.php'

state = None
if LOCAL_MODE:
  with open('../client/localMode/state.json', 'r') as myfile:
    data = myfile.read()
    state = json.loads(data)

def setKey(key, val):
  print(key)
  if LOCAL_MODE:
    state[key] = val
    with open('../client/localMode/state.json', 'w') as myfile:
      json_str = json.dumps(state)
      myfile.write(json_str)
      with open('../client/localMode/state.js', 'w') as myfile2:
        myfile2.write('var LOCALSERVER_STATE = ' + json_str + ';')
        print("Success (local)")
    return

  r = requests.post(url + '?key=' + key, json = val)
  if r.status_code == 200:
    print("Success")
  else:
    print(r.status_code)

def getKey(key, default = None):
  if LOCAL_MODE:
    if key in state:
      return state[key]
    else:
      return default

  r = requests.get(url + '?key=' + key)
  if r.status_code == 200:
    return r.json()
  else:
    return default
