using System;
using System.Collections.Generic;

namespace wf
{
	public abstract class CsvCommonDatabaseIDType<Data>
	{
		public abstract string csvTopCommonName { get; }

		public abstract string typeNameForErrorLog { get; }

		public int Count
		{
			get
			{
				this.Initialize();
				return this.commonIdManager.idMap.Count;
			}
		}

		public bool Contains(int id)
		{
			return this.commonIdManager.idMap.ContainsKey(id);
		}

		public Data GetData(int id)
		{
			this.Initialize();
			NDebug.Assert(this.basicDatas.ContainsKey(id), string.Concat(new object[]
			{
				this.typeNameForErrorLog,
				"\nID[",
				id,
				"]のデータは存在しません"
			}));
			return this.basicDatas[id];
		}

		public List<Data> GetAllDatas(bool onlyEnabled)
		{
			this.Initialize();
			List<Data> list = new List<Data>();
			foreach (KeyValuePair<int, KeyValuePair<string, string>> keyValuePair in this.commonIdManager.idMap)
			{
				if (!onlyEnabled || this.commonIdManager.enabledIdList.Contains(keyValuePair.Key))
				{
					list.Add(this.basicDatas[keyValuePair.Key]);
				}
			}
			return list;
		}

		public bool IsEnabled(int id)
		{
			this.Initialize();
			return this.commonIdManager.enabledIdList.Contains(id);
		}

		public virtual bool Initialize()
		{
			if (this.commonIdManager != null)
			{
				return false;
			}
			this.commonIdManager = new CsvCommonIdManager(this.csvTopCommonName, this.typeNameForErrorLog, CsvCommonIdManager.Type.IdOnly, null);
			this.basicDatas = this.CreateData(this.commonIdManager);
			return true;
		}

		protected abstract Dictionary<int, Data> CreateData(CsvCommonIdManager createIds);

		public CsvCommonDatabaseIDType<Data>.CsvPair OpenCsvFromNameSuffix(string nameSuffix)
		{
			return this.OpenCsv(this.csvTopCommonName + "_" + nameSuffix + ".csv");
		}

		public CsvCommonDatabaseIDType<Data>.CsvPair OpenCsv(string fileName)
		{
			bool flag;
			return new CsvCommonDatabaseIDType<Data>.CsvPair(fileName, ref flag);
		}

		protected CsvCommonIdManager commonIdManager;

		protected Dictionary<int, Data> basicDatas;

		public class CsvPair : IDisposable
		{
			public CsvPair(string fileName, out bool openResult)
			{
				this.file = GameUty.FileSystem.FileOpen(fileName);
				this.csv = new CsvParser();
				openResult = (this.file != null && this.csv.Open(this.file));
			}

			public CsvPair(string fileName)
			{
				this.file = GameUty.FileSystem.FileOpen(fileName);
				this.csv = new CsvParser();
				this.csv.Open(this.file);
			}

			public CsvPair()
			{
			}

			~CsvPair()
			{
				this.Dispose();
			}

			public void Dispose()
			{
				if (this.isDisposed)
				{
					return;
				}
				this.isDisposed = true;
				if (this.file != null)
				{
					this.file.Dispose();
				}
				if (this.csv != null)
				{
					this.csv.Dispose();
				}
				this.file = null;
				this.csv = null;
				GC.SuppressFinalize(this);
			}

			public AFileBase file;

			public CsvParser csv;

			private bool isDisposed;
		}
	}
}
