using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace TeslaConsole;

[Serializable]
internal class Squad
{
	[NonSerialized]
	private EventHandler mOnOnlineChanged;

	private Guid mGuid = Guid.NewGuid();

	private string mName = "";

	private bool mOnline;

	[NonSerialized]
	private List<Pod> mPods = new List<Pod>();

	public Guid Guid => mGuid;

	public string Name
	{
		get
		{
			return mName;
		}
		set
		{
			mName = value;
		}
	}

	public bool IsLocal
	{
		get
		{
			int num = 0;
			foreach (Pod pod in Pods)
			{
				if (pod.IsLocal)
				{
					num++;
				}
			}
			return num > 0;
		}
	}

	public bool Online
	{
		get
		{
			return mOnline;
		}
		set
		{
			if (value == mOnline)
			{
				return;
			}
			mOnline = value;
			if (mOnOnlineChanged != null)
			{
				mOnOnlineChanged(this, EventArgs.Empty);
			}
			foreach (Pod mPod in mPods)
			{
				mPod.Online = value;
			}
		}
	}

	public List<Pod> Pods
	{
		get
		{
			if (mPods == null)
			{
				mPods = new List<Pod>();
			}
			return mPods;
		}
	}

	public List<Pod> OnlinePods
	{
		get
		{
			List<Pod> list = new List<Pod>();
			foreach (Pod mPod in mPods)
			{
				if (mPod.Online)
				{
					list.Add(mPod);
				}
			}
			return list;
		}
	}

	public event EventHandler OnOnlineChanged
	{
		[MethodImpl(MethodImplOptions.Synchronized)]
		add
		{
			mOnOnlineChanged = (EventHandler)Delegate.Combine(mOnOnlineChanged, value);
		}
		[MethodImpl(MethodImplOptions.Synchronized)]
		remove
		{
			mOnOnlineChanged = (EventHandler)Delegate.Remove(mOnOnlineChanged, value);
		}
	}

	public Squad()
		: this(Guid.NewGuid(), "")
	{
	}

	public Squad(string name)
		: this(Guid.NewGuid(), name)
	{
	}

	public Squad(Guid guid, string name)
	{
		mGuid = guid;
		mName = name;
	}

	public Squad Clone()
	{
		Squad squad = new Squad(mGuid, mName);
		squad.mOnline = mOnline;
		squad.mPods.AddRange(mPods);
		return squad;
	}
}
