using System;
using System.IO;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.Collections;
namespace Nibriboard.RippleSpace
{
///
/// Represents a single chunk of an infinite .
///
public class Chunk : IEnumerable
{
///
/// The lines that this chunk currently contains.
///
private List lines = new List();
///
/// The plane that this chunk is located on.
///
public readonly Plane Plane;
///
/// The size of this chunk.
///
public readonly int Size;
///
/// The time at which this chunk was loaded.
///
public readonly DateTime TimeLoaded = DateTime.Now;
///
/// The time at which this chunk was last accessed.
///
public DateTime TimeLastAccessed { get; private set; } = DateTime.Now;
public Chunk(Plane inPlane, int inSize)
{
Plane = inPlane;
Size = inSize;
}
public void UpdateAccessTime()
{
TimeLastAccessed = DateTime.Now;
}
public DrawnLine this[int i]
{
get {
UpdateAccessTime();
return lines[i];
}
set {
UpdateAccessTime();
lines[i] = value;
}
}
public IEnumerator GetEnumerator()
{
UpdateAccessTime();
return lines.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
UpdateAccessTime();
return GetEnumerator();
}
public static async Task FromFile(Plane plane, string filename)
{
StreamReader chunkSource = new StreamReader(filename);
return await FromStream(plane, chunkSource);
}
public static async Task FromStream(Plane plane, StreamReader chunkSource)
{
Chunk result = new Chunk(
plane,
int.Parse(chunkSource.ReadLine())
);
string nextLine = string.Empty;
while((nextLine = await chunkSource.ReadLineAsync()) != null)
{
throw new NotImplementedException();
}
return result;
}
}
}