using System;
using System.Collections.Generic;
using System.IO;
using MaidStatus.Old;

namespace MaidStatus
{
	public class OldDataConverter
	{
		public string saveTime { get; private set; }

		public int gameDay { get; private set; }

		public string playerName { get; private set; }

		public int maidCount { get; private set; }

		public string userComment { get; private set; }

		public int days { get; private set; }

		public List<OldDataConverter.MaidData> OpenFile(string filePath)
		{
			NDebug.Assert(File.Exists(filePath), "ファイルを開けません。" + filePath);
			List<OldDataConverter.MaidData> list = new List<OldDataConverter.MaidData>();
			using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
			{
				using (BinaryReader binaryReader = new BinaryReader(fileStream))
				{
					string a = binaryReader.ReadString();
					NDebug.Assert(a == "CM3D2_SAVE", "セーブデータファイルのヘッダーが不正です。_SAVE 3");
					binaryReader.ReadInt32();
					this.saveTime = binaryReader.ReadString();
					this.gameDay = binaryReader.ReadInt32();
					this.playerName = binaryReader.ReadString();
					this.maidCount = binaryReader.ReadInt32();
					this.userComment = binaryReader.ReadString();
					a = binaryReader.ReadString();
					NDebug.Assert(a == "CM3D2_CHR_MGR", "セーブデータファイルのヘッダーが不正です。_CHR_MGR 2");
					binaryReader.ReadInt32();
					this.days = this.ReadOnPlayerParam(binaryReader);
					int num = binaryReader.ReadInt32();
					for (int i = 0; i < num; i++)
					{
						this.ReadOnMaidProp(binaryReader);
						this.ReadOnMaidMisc(binaryReader);
					}
					int num2 = binaryReader.ReadInt32();
					for (int j = 0; j < num2; j++)
					{
						long num3 = fileStream.Seek(0L, SeekOrigin.Current);
						this.ReadOnMaidProp(binaryReader);
						this.ReadOnMultiColor(binaryReader);
						int appearanceByteSize = (int)(fileStream.Seek(0L, SeekOrigin.Current) - num3);
						StatusReader statusReader = new StatusReader(binaryReader, this.days);
						KeyValuePair<long, int> keyValuePair = this.ReadOnMaidMisc(binaryReader);
						OldDataConverter.MaidData item = new OldDataConverter.MaidData(filePath, statusReader, num3, appearanceByteSize, keyValuePair.Key, keyValuePair.Value);
						list.Add(item);
					}
				}
			}
			return list;
		}

		private int ReadOnPlayerParam(BinaryReader reader)
		{
			string a = reader.ReadString();
			NDebug.Assert(a == "CM3D2_PPARAM", "主人公パラメータのヘッダーが不正です。");
			int num = reader.ReadInt32();
			reader.ReadString();
			reader.ReadInt32();
			reader.ReadInt32();
			int result = reader.ReadInt32();
			reader.ReadInt64();
			reader.ReadInt64();
			if (num > 8)
			{
				reader.ReadInt64();
			}
			reader.ReadInt64();
			reader.ReadInt32();
			reader.ReadInt32();
			reader.ReadInt32();
			if (num > 5)
			{
				reader.ReadBoolean();
			}
			reader.ReadInt32();
			reader.ReadInt32();
			for (int i = 0; i < 6; i++)
			{
				reader.ReadString();
				reader.ReadInt32();
				reader.ReadInt32();
				reader.ReadBoolean();
				this.ReadOnDirectoryStringAndInt32(reader);
			}
			this.ReadOnDirectoryStringAndInt32(reader);
			int num2 = reader.ReadInt32();
			for (int j = 0; j < num2; j++)
			{
				reader.ReadInt32();
				reader.ReadInt32();
				reader.ReadString();
				reader.ReadBoolean();
			}
			this.ReadOnDirectoryInt32AndInt32(reader);
			this.ReadOnDirectoryStringAndBool(reader);
			this.ReadOnListInt32(reader);
			if (num > 9)
			{
				this.ReadOnListInt32(reader);
				this.ReadOnListInt32(reader);
			}
			if (117 <= num)
			{
				num2 = reader.ReadInt32();
				for (int k = 0; k < num2; k++)
				{
					reader.ReadString();
					reader.ReadString();
					reader.ReadInt32();
					this.ReadOnDirectoryStringAndInt32(reader);
				}
			}
			int num3 = reader.ReadInt32();
			NDebug.Assert(348195810 == num3, "主人公パラメータのロードに失敗しました");
			return result;
		}

		private void ReadOnMaidProp(BinaryReader reader)
		{
			string a = reader.ReadString();
			NDebug.Assert(a == "CM3D2_MPROP_LIST", "メイドプロパティリストのヘッダーが不正です。");
			int num = reader.ReadInt32();
			int num2 = reader.ReadInt32();
			for (int i = 0; i < num2; i++)
			{
				reader.ReadString();
				reader.ReadString();
				int num3 = reader.ReadInt32();
				reader.ReadInt32();
				reader.ReadString();
				reader.ReadInt32();
				reader.ReadInt32();
				reader.ReadInt32();
				if (101 <= num3)
				{
					reader.ReadInt32();
				}
				reader.ReadInt32();
				reader.ReadString();
				reader.ReadInt32();
				reader.ReadBoolean();
				reader.ReadInt32();
				reader.ReadInt32();
			}
		}

		private void ReadOnMultiColor(BinaryReader reader)
		{
			string a = reader.ReadString();
			NDebug.Assert(a == "CM3D2_MULTI_COL", "無限色のヘッダーが不正です。");
			reader.ReadInt32();
			int num = reader.ReadInt32();
			for (int i = 0; i < num; i++)
			{
				reader.ReadBoolean();
				reader.ReadInt32();
				reader.ReadInt32();
				reader.ReadInt32();
				reader.ReadInt32();
				reader.ReadInt32();
				reader.ReadInt32();
				reader.ReadInt32();
				reader.ReadInt32();
				reader.ReadInt32();
			}
		}

		private KeyValuePair<long, int> ReadOnMaidMisc(BinaryReader reader)
		{
			string a = reader.ReadString();
			NDebug.Assert(a == "CM3D2_MAID_MISC", "メイドMiscのヘッダーが不正です。");
			int num = reader.ReadInt32();
			reader.ReadInt32();
			int num2 = reader.ReadInt32();
			long key = reader.BaseStream.Seek(0L, SeekOrigin.Current);
			if (num2 != 0)
			{
				reader.ReadBytes(num2);
			}
			if (8 <= num)
			{
				reader.ReadString();
			}
			reader.ReadSingle();
			reader.ReadSingle();
			reader.ReadSingle();
			reader.ReadSingle();
			return new KeyValuePair<long, int>(key, num2);
		}

		private void ReadOnListInt32(BinaryReader reader)
		{
			int num = reader.ReadInt32();
			for (int i = 0; i < num; i++)
			{
				reader.ReadInt32();
			}
		}

		private void ReadOnDirectoryStringAndInt32(BinaryReader reader)
		{
			int num = reader.ReadInt32();
			for (int i = 0; i < num; i++)
			{
				reader.ReadString();
				reader.ReadInt32();
			}
		}

		private void ReadOnDirectoryStringAndString(BinaryReader reader)
		{
			int num = reader.ReadInt32();
			for (int i = 0; i < num; i++)
			{
				reader.ReadString();
				reader.ReadInt32();
			}
		}

		private void ReadOnDirectoryInt32AndInt32(BinaryReader reader)
		{
			int num = reader.ReadInt32();
			for (int i = 0; i < num; i++)
			{
				reader.ReadInt32();
				reader.ReadInt32();
			}
		}

		private void ReadOnDirectoryStringAndBool(BinaryReader reader)
		{
			int num = reader.ReadInt32();
			for (int i = 0; i < num; i++)
			{
				reader.ReadString();
				reader.ReadBoolean();
			}
		}

		public class MaidData
		{
			public MaidData(string saveFilePath, StatusReader statusReader, long appearanceBytePos, int appearanceByteSize, long thumbnailBytePos, int thumbnailByteSize)
			{
				this.saveFilePath = saveFilePath;
				this.statusReader = statusReader;
				this.appearanceBytePos = appearanceBytePos;
				this.appearanceByteSize = appearanceByteSize;
				this.thumbnailBytePos = thumbnailBytePos;
				this.thumbnailByteSize = thumbnailByteSize;
			}

			public byte[] ReadAppearanceByte()
			{
				byte[] result = null;
				using (FileStream fileStream = new FileStream(this.saveFilePath, FileMode.Open))
				{
					using (BinaryReader binaryReader = new BinaryReader(fileStream))
					{
						long offset = binaryReader.BaseStream.Seek(0L, SeekOrigin.Current);
						binaryReader.BaseStream.Seek(this.appearanceBytePos, SeekOrigin.Begin);
						result = binaryReader.ReadBytes(this.appearanceByteSize);
						binaryReader.BaseStream.Seek(offset, SeekOrigin.Begin);
					}
				}
				return result;
			}

			public byte[] ReadThumbnailByte()
			{
				byte[] result = null;
				using (FileStream fileStream = new FileStream(this.saveFilePath, FileMode.Open))
				{
					using (BinaryReader binaryReader = new BinaryReader(fileStream))
					{
						long offset = binaryReader.BaseStream.Seek(0L, SeekOrigin.Current);
						binaryReader.BaseStream.Seek(this.thumbnailBytePos, SeekOrigin.Begin);
						result = binaryReader.ReadBytes(this.thumbnailByteSize);
						binaryReader.BaseStream.Seek(offset, SeekOrigin.Begin);
					}
				}
				return result;
			}

			public readonly StatusReader statusReader;

			public readonly string saveFilePath;

			private long appearanceBytePos;

			private int appearanceByteSize;

			private long thumbnailBytePos;

			private int thumbnailByteSize;
		}
	}
}
