using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace TeslaConsole;

internal class Site
{
	private static Site mActiveConfig;

	private List<Squad> mSquads = new List<Squad>();

	public static Site Active
	{
		get
		{
			if (mActiveConfig == null)
			{
				LoadFromStore();
			}
			return mActiveConfig;
		}
	}

	public List<Squad> Squads => mSquads;

	public static Site LoadFromStore()
	{
		mActiveConfig = new Site();
		mActiveConfig.Load();
		return mActiveConfig;
	}

	public static Site LoadFromFile(string filename)
	{
		mActiveConfig = new Site();
		mActiveConfig.LoadFrom(filename);
		return mActiveConfig;
	}

	protected void Load()
	{
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		string path = Path.Combine(Program.GetCommonAppDataDirectory(), "local.siteconfig");
		if (!File.Exists(path))
		{
			return;
		}
		using FileStream input = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
		BinaryReader binaryReader = new BinaryReader(input);
		int num = binaryReader.ReadInt32();
		for (int i = 0; i < num; i++)
		{
			Squad squad = (Squad)binaryFormatter.Deserialize(binaryReader.BaseStream);
			mSquads.Add(squad);
			int num2 = binaryReader.ReadInt32();
			for (int j = 0; j < num2; j++)
			{
				squad.Pods.Add((Pod)binaryFormatter.Deserialize(binaryReader.BaseStream));
			}
		}
		binaryReader.Close();
	}

	protected void LoadFrom(string filename)
	{
		BinaryReader binaryReader = new BinaryReader(File.OpenRead(filename));
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		int num = binaryReader.ReadInt32();
		for (int i = 0; i < num; i++)
		{
			Squad squad = (Squad)binaryFormatter.Deserialize(binaryReader.BaseStream);
			mSquads.Add(squad);
			int num2 = binaryReader.ReadInt32();
			for (int j = 0; j < num2; j++)
			{
				squad.Pods.Add((Pod)binaryFormatter.Deserialize(binaryReader.BaseStream));
			}
		}
		binaryReader.Close();
	}

	public void Save()
	{
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		string path = Path.Combine(Program.GetCommonAppDataDirectory(), "local.siteconfig");
		using (FileStream output = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None))
		{
			BinaryWriter binaryWriter = new BinaryWriter(output);
			binaryWriter.Write(mSquads.Count);
			foreach (Squad mSquad in mSquads)
			{
				binaryFormatter.Serialize(binaryWriter.BaseStream, mSquad);
				binaryWriter.Write(mSquad.Pods.Count);
				foreach (Pod pod in mSquad.Pods)
				{
					binaryFormatter.Serialize(binaryWriter.BaseStream, pod);
				}
			}
			binaryWriter.Close();
		}
		mActiveConfig = this;
	}

	public void SaveAs(string filename)
	{
		BinaryWriter binaryWriter = new BinaryWriter(File.OpenWrite(filename));
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		binaryWriter.Write(mSquads.Count);
		foreach (Squad mSquad in mSquads)
		{
			binaryFormatter.Serialize(binaryWriter.BaseStream, mSquad);
			binaryWriter.Write(mSquad.Pods.Count);
			foreach (Pod pod in mSquad.Pods)
			{
				binaryFormatter.Serialize(binaryWriter.BaseStream, pod);
			}
		}
		binaryWriter.Close();
	}

	public Pod FindPod(byte[] macAddress)
	{
		foreach (Squad mSquad in mSquads)
		{
			foreach (Pod pod in mSquad.Pods)
			{
				if (pod.MacAddressEquals(macAddress))
				{
					return pod;
				}
			}
		}
		return null;
	}

	public Squad FindSquad(Guid id)
	{
		foreach (Squad mSquad in mSquads)
		{
			if (mSquad.Guid == id)
			{
				return mSquad;
			}
		}
		return null;
	}
}
