using System;
using System.Collections.Generic;
using Nibriboard.RippleSpace;
using Nibriboard.Utilities;
using SBRL.Utilities;
namespace Nibriboard.Client
{
///
/// Manages the construction of lines that the clients are drawing bit by bit.
///
public class LineIncubator
{
///
/// The current lines that haven't been completed yet.
///
private Dictionary currentLines = new Dictionary();
///
/// The number of lines that this line incubator has completed.
///
public int LinesCompleted { get; private set; } = 0;
///
/// The number of lines that are still incubating and haven't been completed yet.
///
public int IncompletedLines {
get {
return currentLines.Count;
}
}
public LineIncubator()
{
}
///
/// Figures out whether an incomplete line with the given id exists or not.
///
/// The line id to check for.
public bool LineExists(string lineId)
{
if(currentLines.ContainsKey(lineId))
return true;
return false;
}
///
/// Creates a new line in the incubator.
///
/// The line id to attach to the new line in the incubator.
public void CreateLine(string lineId, string newColour, int newWidth)
{
if(currentLines.ContainsKey(lineId))
throw new InvalidOperationException($"Error: A line with the id {lineId} already exists, so you can't recreate it.");
currentLines.Add(lineId, new DrawnLine(lineId) {
Colour = newColour,
Width = newWidth
});
}
///
/// Adds a series of points to the incomplete line with the specified id.
///
/// The line id to add the points to.
/// The points to add to the lines.
public void AddBit(string lineId, List points)
{
// Create a new line if one doesn't exist already
if(!currentLines.ContainsKey(lineId))
throw new InvalidOperationException($"Error: A line with the id {lineId} doesn't exist, so you can't add to it.");
// Add these points to the line
currentLines[lineId].Points.AddRange(points);
}
///
/// Mark a line as complete and remove it from the incubator.
///
/// The id of the line to complete.
/// The completed line.
public DrawnLine CompleteLine(string lineId)
{
if(!currentLines.ContainsKey(lineId))
throw new KeyNotFoundException("Error: A line with that id wasn't found in this LineIncubator.");
DrawnLine completedLine = currentLines[lineId];
currentLines.Remove(lineId);
int originalPointCount = completedLine.Points.Count;
completedLine.Points = LineSimplifier.SimplifyLine(completedLine.Points, 6);
Log.WriteLine(
"[LineIncubator] [LineComplete] #{0}: {1} -> {2} points ({3:0.00}% reduction)",
lineId,
originalPointCount,
completedLine.Points.Count,
((float)completedLine.Points.Count / (float)originalPointCount) * 100f
);
return completedLine;
}
}
}