from requests import session
from numpy import array

class Graph:
    def __init__(self, series: str, chart: int = 0, address: str = "127.0.0.1", port: int = 8080):
        self.base = f"http://{address}:{port}/add"
        self.chart = chart
        self.series = series

    def __enter__(self):
        self.session = session()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.session.close()

    def list(self, points: list):
        """
        Send a list of points to the graph.

        Parameters:
        points: eg. [(0.1, 13.5), (0.2, 14.9)] 
        """
        self.session.post(
            self.base,
            json={
                "chart": self.chart,
                "series": self.series,
                "points": [ {"x": i[0], "y": i[1]} for i in points ] 
            }
        )

    def lists(self, x: list, y: list):
        self.list(zip(x,y))

    def point(self, point: tuple[float, float]):
        self.session.get(f"{self.base}?chart={self.chart}&series={self.series}&x={point[0]}&y={point[1]}")

    def __iadd__(self, data):
        if (isinstance(data, list)):
            self.list(data)
            return self
        elif (isinstance(data, tuple)):
            if (isinstance(data[0], list)):
                self.lists(data[0], data[1])
                return self
            elif (isinstance(data[0], float)):
                self.point(data)
                return self
            
        raise Exception("unknown data format")