"""svgen/svgen/elements.py
"""
# Standard Library
import abc

# Third-Party
import numpy as np


class Element(metaclass=abc.ABCMeta):
    def __init__(self, kw: dict):
        self.kw = self.kwargs(kw)

    @abc.abstractmethod
    def render(self, array: np.ndarray) -> str:
        pass

    @staticmethod
    def kwargs(kw: dict) -> str:
        if kw is None:
            return ""
        elif type(kw) is str:
            return kw
        elif type(kw) is dict:
            return " ".join(f'{k}="{v}"' for k, v in kw.items())
        else:
            raise TypeError(f"Can't interpret '{type(kw)}' as key-word arg list.")


class Line(Element):
    def __init__(self, p: int, q: int, kw: dict):
        Element.__init__(self, kw)
        self.p = p
        self.q = q

    def render(self, array) -> str:
        return f'<line x1="{array[self.p, 0]:.0f}" y1="{array[self.p,1]:.0f}" x2="{array[self.q,0]:.0f}" y2="{array[self.q,0]:.0f}" {self.kw} />'


class Poly(Element):
    def __init__(self, *P: int, kw: dict):
        Element.__init__(self, kw)
        self.P = P

    def render(self, array) -> str:
        return f'<polygon points="{",".join(f"{array[p,0]:.0f} {array[p,1]:.0f}" for p in self.P)}" {self.kw}/>'


class PolyLine(Element):
    def __init__(self, *P: int, kw: dict):
        Element.__init__(self, kw)
        self.P = P

    def render(self, array) -> str:
        return f'<path d="M {" L ".join(f"{array[p,0]:.0f} {array[p,1]:.0f}" for p in self.P)}" {self.kw}/>'


class Surf(Element):
    def __init__(self, kw: dict):
        Element.__init__(self, kw)