﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Lab4.Renderers
{
  class SVGRenderer : IRenderer
  {
    private List<string> lines;
    private Stream stream;

    public SVGRenderer(Stream stream)
    {
      this.stream = stream;
      lines = new List<string> { "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">" };
    }

    public void close()
    {
      lines.Add("</svg>");
      var writer = new StreamWriter(stream);
      lines.ForEach(line => writer.WriteLine(line));
      writer.Flush();
      writer.Close();
    }

    public void drawLine(Point s, Point e)
    {
      // <line x1="0"  y1="10" x2="0"   y2="100" style="stroke:#006600;"/>
      lines.Add("<line x1=\"" + s.getX() + "\" y1=\"" + s.getY() + "\" x2\"" + e.getX() + "\" y2=\"" + e.getY() + "\" style=\"stroke:#006600;\" />");
    }

    public void fillPolygon(Point[] points)
    {
      // <polyline points="0,0  30,0  15,30" style = "stroke:#006600;" />
      string ret = "<polyline points=\"";
      points.ToList().ForEach(p => ret += p.getX() + "," + p.getY() + " ");
      ret += "\" style = \"stroke:#006600;\" />";

      lines.Add(ret);
    }
  }
}
