from graph import Graph
from math import sin, cos
from time import sleep
import numpy as np
from argparse import ArgumentParser

def function(args, func):
  x = 0.0
  while True:
    with Graph(series=args.series, chart=args.chart, address=args.address, port=args.port) as g:
        try:
            x += 0.01
            g += (x, func(x))
            sleep(0.01)
        except KeyboardInterrupt:
            break

def sine_array(args):
  start = 0
  with Graph(series=args.series, chart=args.chart, address=args.address, port=args.port) as g:
    while True:
      try:
        x = np.linspace(start, start+0.5, args.num)
        y = np.sin(x)

        start += 0.5

        g += (x.tolist(), y.tolist())

        sleep(args.delay)
      except KeyboardInterrupt:
        break

def star(args):
  with Graph(series=args.series, chart=args.chart, address=args.address, port=args.port) as g:
    points = [
      (2/4,2/2),
      (3/4,0),
      (0,1/2),
      (4/4,1/2),
      (1/4,0),
      (2/4,2/2)
    ]
    g += points

if __name__ == "__main__":
  parser = ArgumentParser()
  parser.add_argument("-t", action="store", dest="test",    help="test type", type=str, choices=["sin", "cos", "array", "star"], required=False, default="sin")
  parser.add_argument("-n", action="store", dest="num",     help="sin_array size/iteration", type=int, required=False, default=100)
  parser.add_argument("-d", action="store", dest="delay",   help="delay between iteration", type=float, required=False, default=0.25)
  parser.add_argument("-c", action="store", dest="chart",   help="chart id", type=int, required=True)
  parser.add_argument("-s", action="store", dest="series",  help="series name", type=str, required=True)
  parser.add_argument("-p", action="store", dest="port",    help="server port", type=int, required=False, default=8080)
  parser.add_argument("-a", action="store", dest="address", help="server address", type=str, required=False, default="127.0.0.1")

  args = parser.parse_args()
  if args.test == "sin":
    function(args, sin)
  if args.test == "cos":
    function(args, cos)
  elif args.test == "array":
    sine_array(args)
  elif args.test == "star":
    star(args)