using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace FacilityFlag
{
	public class DataArray<TKey, TValue>
	{
		public DataArray()
		{
			this.m_TempDataArray = new Dictionary<TKey, TValue>();
		}

		public DataArray(Dictionary<TKey, TValue> dataArray)
		{
			this.m_TempDataArray = dataArray;
		}

		public void Serialize(BinaryWriter brWrite, Converter<TKey, byte[]> converter_key, Converter<TValue, byte[]> converter_value)
		{
			brWrite.Write("CM3D21_TEMP_FLAG");
			int count = this.m_TempDataArray.Count;
			List<TKey> list = new List<TKey>(this.m_TempDataArray.Keys);
			List<TValue> list2 = new List<TValue>(this.m_TempDataArray.Values);
			brWrite.Write(count);
			for (int i = 0; i < count; i++)
			{
				byte[] array = converter_key(list[i]);
				brWrite.Write(array, 0, array.Length);
				array = converter_value(list2[i]);
				brWrite.Write(array, 0, array.Length);
			}
		}

		public void Deserialize(BinaryReader brRead, Func<BinaryReader, TKey> func_read_key, Func<BinaryReader, TValue> func_read_value)
		{
			string text = brRead.ReadString();
			NDebug.Assert(text == "CM3D21_TEMP_FLAG", string.Format("セーブデータのヘッダーが不正な値です\n{0}", text));
			this.Clear();
			int num = brRead.ReadInt32();
			for (int i = 0; i < num; i++)
			{
				TKey key = func_read_key(brRead);
				TValue value = func_read_value(brRead);
				this.Add(key, value, true);
			}
		}

		public static bool Skip(BinaryReader brRead, Func<BinaryReader, TKey> func_read_key, Func<BinaryReader, TValue> func_read_value)
		{
			long position = 0L;
			bool result;
			try
			{
				position = brRead.BaseStream.Position;
				string text = brRead.ReadString();
				if (text != "CM3D21_TEMP_FLAG")
				{
					throw new Exception(string.Format("セーブデータのヘッダーが不正な値です\n{0}", text));
				}
				int num = brRead.ReadInt32();
				for (int i = 0; i < num; i++)
				{
					func_read_key(brRead);
					func_read_value(brRead);
				}
				result = true;
			}
			catch (Exception ex)
			{
				Debug.LogWarning(ex.Message + "\n関数呼び出し前の位置へ戻ります。");
				brRead.BaseStream.Position = position;
				result = false;
			}
			return result;
		}

		public int Count()
		{
			return this.m_TempDataArray.Count;
		}

		public bool Contains(TKey key)
		{
			return this.m_TempDataArray.ContainsKey(key);
		}

		public bool Add(TKey key, TValue value, bool enableOverWrite = true)
		{
			if (this.Contains(key))
			{
				if (!enableOverWrite)
				{
					return false;
				}
				this.m_TempDataArray[key] = value;
			}
			else
			{
				this.m_TempDataArray.Add(key, value);
			}
			return true;
		}

		public TValue Get(TKey key, bool enableCheckWarning = false)
		{
			if (this.Contains(key))
			{
				return this.m_TempDataArray[key];
			}
			NDebug.Warning(!enableCheckWarning, string.Format("フラグ「{0}」は存在しません。", key));
			return default(TValue);
		}

		public bool Remove(TKey key, bool enableCheckWarning = false)
		{
			if (this.Contains(key))
			{
				this.m_TempDataArray.Remove(key);
				return true;
			}
			NDebug.Warning(!enableCheckWarning, string.Format("フラグ「{0}」は存在しません。", key));
			return false;
		}

		public void Clear()
		{
			this.m_TempDataArray.Clear();
		}

		public void Loop(Action<int, TKey, TValue> callback)
		{
			int count = this.m_TempDataArray.Count;
			List<TKey> list = new List<TKey>(this.m_TempDataArray.Keys);
			List<TValue> list2 = new List<TValue>(this.m_TempDataArray.Values);
			for (int i = 0; i < count; i++)
			{
				callback(i, list[i], list2[i]);
			}
		}

		public Dictionary<TKey, TValue> Copy()
		{
			return new Dictionary<TKey, TValue>(this.m_TempDataArray);
		}

		public void DebugLogConsole()
		{
		}

		protected readonly Dictionary<TKey, TValue> m_TempDataArray;
	}
}
