import base64
from io import BytesIO
from pathlib import Path
from shutil import which

import pytest

from hawc.apps.common.svg import SVGConverter

DATA_PATH = Path(__file__).parent.absolute() / "data"


@pytest.fixture
def svg_data():
    svg = """<svg width="100" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg">
            <rect x="10" y="10" width="80" height="80" style="fill:red;stroke-width:3;stroke:blue" />
        </svg>"""
    return (base64.encodebytes(svg.encode()), "/test/", 240, 240)


@pytest.mark.skipif(which("phantomjs") is None, reason="requires phantomjs on path to run")
class TestSvgConverter:
    def test_svg(self, svg_data):
        conv = SVGConverter(*svg_data)
        resp = conv.to_svg()
        # assert html is returned
        assert '<style type="text/css">' in resp
        assert (DATA_PATH / "svg-converter.svg").read_text() == resp

    def test_png(self, svg_data):
        conv = SVGConverter(*svg_data)
        resp = conv.to_png()
        assert isinstance(resp, bytes)
        assert (DATA_PATH / "svg-converter.png").read_bytes() == resp

    def test_pdf(self, svg_data):
        conv = SVGConverter(*svg_data)
        resp = conv.to_pdf()
        assert isinstance(resp, bytes)
        # can't compare file; timestamp metadata information pdf

    def test_pptx(self, svg_data):
        conv = SVGConverter(*svg_data)
        resp = conv.to_pptx()
        assert isinstance(resp, BytesIO)
        # can't compare file; timestamp metadata information pptx
