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

internal class DeskManager
{
	public static void CreateCsvData()
	{
		using (AFileBase afileBase = GameUty.FileSystem.FileOpen("desk_item_category.nei"))
		{
			using (CsvParser csvParser = new CsvParser())
			{
				bool condition = csvParser.Open(afileBase);
				NDebug.Assert(condition, "desk_item_category.nei\nopen failed.");
				for (int i = 1; i < csvParser.max_cell_y; i++)
				{
					if (!csvParser.IsCellToExistData(0, i))
					{
						break;
					}
					int cellAsInteger = csvParser.GetCellAsInteger(0, i);
					string cellAsString = csvParser.GetCellAsString(1, i);
					if (!DeskManager.item_category_data_dic_.ContainsKey(cellAsInteger))
					{
						DeskManager.item_category_data_dic_.Add(cellAsInteger, cellAsString);
					}
				}
			}
		}
		HashSet<int> hashSet = new HashSet<int>();
		CsvCommonIdManager.ReadEnabledIdList(CsvCommonIdManager.FileSystemType.Normal, true, "desk_item_enabled_id", ref hashSet);
		CsvCommonIdManager.ReadEnabledIdList(CsvCommonIdManager.FileSystemType.Old, true, "desk_item_enabled_id", ref hashSet);
		using (AFileBase afileBase2 = GameUty.FileSystem.FileOpen("desk_item_detail.nei"))
		{
			using (CsvParser csvParser2 = new CsvParser())
			{
				bool condition2 = csvParser2.Open(afileBase2);
				NDebug.Assert(condition2, "desk_item_detail.nei\nopen failed.");
				for (int j = 1; j < csvParser2.max_cell_y; j++)
				{
					if (csvParser2.IsCellToExistData(0, j))
					{
						int cellAsInteger2 = csvParser2.GetCellAsInteger(0, j);
						if (hashSet.Contains(cellAsInteger2))
						{
							DeskManager.ItemData itemData = new DeskManager.ItemData(csvParser2, j);
							DeskManager.item_detail_data_dic_.Add(itemData.id, itemData);
						}
					}
				}
			}
		}
		DeskManager.item_inst_data_.Clear();
		DeskManager.init_item_data_.Clear();
		foreach (KeyValuePair<int, DeskManager.ItemData> keyValuePair in DeskManager.item_detail_data_dic_)
		{
			DeskManager.InstansData instansData = new DeskManager.InstansData(keyValuePair.Value);
			DeskManager.init_item_data_.Add(instansData.id, instansData);
			DeskManager.item_inst_data_.Add(new DeskManager.InstansData(instansData));
		}
	}

	public static void OnChangeBG(string bg_name, GameObject bg_object)
	{
		if (bg_object == null)
		{
			for (int i = 0; i < DeskManager.item_inst_data_.Count; i++)
			{
				DeskManager.item_inst_data_[i].item_object = null;
			}
			return;
		}
		string[] array = new string[]
		{
			"ShinShitsumu_ChairRot",
			"ShinShitsumu_ChairRot_Night",
			"ShinShitsumu",
			"ShinShitsumu_Night"
		};
		for (int j = 0; j < array.Length; j++)
		{
			array[j] = array[j].ToLower();
		}
		if (0 <= Array.IndexOf<string>(array, bg_name))
		{
			for (int k = 0; k < DeskManager.item_inst_data_.Count; k++)
			{
				DeskManager.InstansData instansData = DeskManager.item_inst_data_[k];
				if (instansData.IsPossessing())
				{
					GameObject gameObject = null;
					if (!string.IsNullOrEmpty(instansData.item_data.prefab_name))
					{
						gameObject = Utility.CreatePrefab(bg_object, "Prefab/" + instansData.item_data.prefab_name, true);
					}
					else if (!string.IsNullOrEmpty(instansData.item_data.asset_name))
					{
						gameObject = GameMain.Instance.BgMgr.CreateAssetBundle(instansData.item_data.asset_name);
						if (gameObject != null)
						{
							gameObject = UnityEngine.Object.Instantiate<GameObject>(gameObject);
							MeshRenderer[] componentsInChildren = gameObject.GetComponentsInChildren<MeshRenderer>();
							for (int l = 0; l < componentsInChildren.Length; l++)
							{
								if (componentsInChildren[l] != null)
								{
									componentsInChildren[l].shadowCastingMode = ShadowCastingMode.Off;
								}
							}
						}
					}
					if (gameObject != null)
					{
						gameObject.name = gameObject.name.Replace("(Clone)", string.Empty);
						gameObject.transform.SetParent(bg_object.transform, false);
						instansData.Apply(gameObject, true);
						instansData.item_object = gameObject;
						if (instansData.item_object.GetComponentInChildren<BoxCollider>() == null)
						{
							MeshRenderer componentInChildren = instansData.item_object.GetComponentInChildren<MeshRenderer>(true);
							if (componentInChildren != null)
							{
								componentInChildren.gameObject.AddComponent<BoxCollider>();
							}
						}
					}
					else if (!string.IsNullOrEmpty(instansData.item_data.prefab_name))
					{
						Debug.LogError("Prefab[" + instansData.item_data.prefab_name + "]を読み込めませんでした");
					}
					else
					{
						Debug.LogError("Asset[" + instansData.item_data.asset_name + "]を読み込めませんでした");
					}
				}
			}
		}
	}

	public static void SerializeSingleSaveData(BinaryWriter binary)
	{
		binary.Write("CM3D2_DeskCustomize");
		binary.Write(1170);
		binary.Write(DeskManager.item_inst_data_.Count);
		for (int i = 0; i < DeskManager.item_inst_data_.Count; i++)
		{
			binary.Write(DeskManager.item_inst_data_[i].id);
			DeskManager.item_inst_data_[i].Serialize(binary);
		}
	}

	public static void Deserialize()
	{
		string fullPath = Path.GetFullPath(".\\");
		string text = fullPath + "SaveData";
		if (!Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		DeskManager.old_item_data_from_system_.Clear();
		string path = text + "/DeskCustomize.save";
		if (!File.Exists(path))
		{
			return;
		}
		using (FileStream fileStream = new FileStream(path, FileMode.Open))
		{
			if (fileStream != null)
			{
				byte[] buffer = new byte[fileStream.Length];
				fileStream.Read(buffer, 0, (int)fileStream.Length);
				using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer)))
				{
					if (binaryReader.ReadString() != "CM3D2_DeskCustomize")
					{
						NDebug.Assert("DeskCustomizeファイルのヘッダーが不正です。", false);
					}
					else
					{
						int game_ver = binaryReader.ReadInt32();
						int num = binaryReader.ReadInt32();
						for (int i = 0; i < num; i++)
						{
							int num2 = binaryReader.ReadInt32();
							DeskManager.InstansData instansData = new DeskManager.InstansData(binaryReader, game_ver);
							if (instansData.item_data != null)
							{
								if (!DeskManager.old_item_data_from_system_.ContainsKey(instansData.item_data.id))
								{
									DeskManager.old_item_data_from_system_.Add(instansData.item_data.id, instansData);
								}
							}
						}
					}
				}
			}
		}
	}

	public static void DeserializeSingleSaveData(BinaryReader binary, int version)
	{
		DeskManager.item_inst_data_.Clear();
		Dictionary<int, DeskManager.InstansData> dictionary = new Dictionary<int, DeskManager.InstansData>();
		foreach (KeyValuePair<int, DeskManager.InstansData> keyValuePair in DeskManager.init_item_data_)
		{
			dictionary.Add(keyValuePair.Key, new DeskManager.InstansData(keyValuePair.Value));
		}
		if (version <= 145)
		{
			foreach (KeyValuePair<int, DeskManager.InstansData> keyValuePair2 in DeskManager.old_item_data_from_system_)
			{
				if (!dictionary.ContainsKey(keyValuePair2.Key))
				{
					dictionary.Add(keyValuePair2.Key, null);
				}
				dictionary[keyValuePair2.Key] = new DeskManager.InstansData(keyValuePair2.Value);
			}
			GameMain.Instance.CharacterMgr.status.SetFlag("desk_customize_savedata_warning", 1);
		}
		else
		{
			string a = binary.ReadString();
			NDebug.Assert(a == "CM3D2_DeskCustomize", "セーブデータ\nデスクカスタマイズのヘッダーが正しくありません");
			int game_ver = binary.ReadInt32();
			int num = binary.ReadInt32();
			for (int i = 0; i < num; i++)
			{
				int key = binary.ReadInt32();
				DeskManager.InstansData value = new DeskManager.InstansData(binary, game_ver);
				if (dictionary.ContainsKey(key))
				{
					dictionary[key] = value;
				}
			}
		}
		foreach (KeyValuePair<int, DeskManager.InstansData> keyValuePair3 in dictionary)
		{
			DeskManager.item_inst_data_.Add(new DeskManager.InstansData(keyValuePair3.Value));
		}
	}

	public static List<DeskManager.InstansData> item_inst_data
	{
		get
		{
			return DeskManager.item_inst_data_;
		}
	}

	public static Dictionary<int, string> item_category_data_dic
	{
		get
		{
			return DeskManager.item_category_data_dic_;
		}
	}

	public static Dictionary<int, DeskManager.ItemData> item_detail_data_dic
	{
		get
		{
			return DeskManager.item_detail_data_dic_;
		}
	}

	private static List<DeskManager.InstansData> item_inst_data_ = new List<DeskManager.InstansData>();

	private static Dictionary<int, DeskManager.InstansData> init_item_data_ = new Dictionary<int, DeskManager.InstansData>();

	private static Dictionary<int, DeskManager.InstansData> old_item_data_from_system_ = new Dictionary<int, DeskManager.InstansData>();

	private static Dictionary<int, string> item_category_data_dic_ = new Dictionary<int, string>();

	private static Dictionary<int, DeskManager.ItemData> item_detail_data_dic_ = new Dictionary<int, DeskManager.ItemData>();

	public class ItemData
	{
		public ItemData(CsvParser csv, int csv_y)
		{
			int num = 0;
			this.id = csv.GetCellAsInteger(num++, csv_y);
			this.name = csv.GetCellAsString(num++, csv_y);
			this.category_id = csv.GetCellAsInteger(num++, csv_y);
			this.prefab_name = csv.GetCellAsString(num++, csv_y);
			this.asset_name = csv.GetCellAsString(num++, csv_y);
			this.possession_flag = csv.GetCellAsString(num++, csv_y);
			string cellAsString = csv.GetCellAsString(num++, csv_y);
			this.seasonal = !string.IsNullOrEmpty(cellAsString);
			if (this.seasonal)
			{
				string[] array = cellAsString.Split(new char[]
				{
					','
				});
				this.seasonal_month = new int[array.Length];
				for (int i = 0; i < array.Length; i++)
				{
					this.seasonal_month[i] = int.Parse(array[i]);
				}
			}
			else
			{
				this.seasonal_month = new int[0];
			}
			this.init_visible = (csv.GetCellAsString(num++, csv_y) == "○");
			this.init_position = csv.GetCellAsVector3(num++, csv_y, ',');
			this.init_rotation = csv.GetCellAsVector3(num++, csv_y, ',');
			this.init_scale = csv.GetCellAsVector3(num++, csv_y, ',');
			this.init_ui_position_scale = csv.GetCellAsReal(num++, csv_y);
			this.init_ui_rotation_scale = csv.GetCellAsReal(num++, csv_y);
		}

		public readonly int id;

		public readonly string name;

		public readonly int category_id;

		public readonly string prefab_name;

		public readonly string asset_name;

		public readonly bool seasonal;

		public readonly string possession_flag;

		public readonly int[] seasonal_month;

		public readonly bool init_visible;

		public readonly Vector3 init_position;

		public readonly Vector3 init_rotation;

		public readonly Vector3 init_scale;

		public readonly float init_ui_position_scale;

		public readonly float init_ui_rotation_scale;
	}

	public class InstansData
	{
		public InstansData(DeskManager.ItemData data)
		{
			this.id = data.id;
			this.visible = true;
			this.only_season_visible = data.seasonal;
			this.visible = data.init_visible;
			this.position = data.init_position;
			this.rotation = data.init_rotation;
			this.scale = data.init_scale;
			this.item_data = data;
		}

		public InstansData(BinaryReader binary, int game_ver)
		{
			this.Deserialize(binary, game_ver);
		}

		public InstansData(DeskManager.InstansData instans_data)
		{
			this.id = instans_data.id;
			this.visible = instans_data.visible;
			this.only_season_visible = instans_data.only_season_visible;
			this.position = instans_data.position;
			this.rotation = instans_data.rotation;
			this.scale = instans_data.scale;
			this.item_data = instans_data.item_data;
			this.item_object = instans_data.item_object;
		}

		public void Apply(GameObject apply_object, bool season_visible_setting)
		{
			if (apply_object == null)
			{
				return;
			}
			Transform transform = apply_object.transform;
			transform.position = this.position;
			transform.rotation = Quaternion.Euler(this.rotation);
			transform.localScale = this.scale;
			apply_object.SetActive(this.visible);
			if (this.visible && season_visible_setting && this.only_season_visible && !this.IsNowMonthSeason())
			{
				apply_object.SetActive(false);
			}
		}

		public bool IsNowMonthSeason()
		{
			if (!this.item_data.seasonal)
			{
				return false;
			}
			DateTime now = DateTime.Now;
			return 0 <= Array.IndexOf<int>(this.item_data.seasonal_month, now.Month);
		}

		public bool IsPossessing()
		{
			return string.IsNullOrEmpty(this.item_data.possession_flag) || GameMain.Instance.CharacterMgr.status.GetFlag(this.item_data.possession_flag) == 1;
		}

		public void Serialize(BinaryWriter binary)
		{
			Action<Vector3> action = delegate(Vector3 data)
			{
				binary.Write(data.x);
				binary.Write(data.y);
				binary.Write(data.z);
			};
			binary.Write(this.id);
			binary.Write(this.visible);
			binary.Write(this.only_season_visible);
			action(this.position);
			action(this.rotation);
			action(this.scale);
		}

		public void Deserialize(BinaryReader binary, int game_ver)
		{
			Func<Vector3> func = delegate()
			{
				Vector3 zero = Vector3.zero;
				zero.x = binary.ReadSingle();
				zero.y = binary.ReadSingle();
				zero.z = binary.ReadSingle();
				return zero;
			};
			this.id = binary.ReadInt32();
			this.visible = binary.ReadBoolean();
			this.only_season_visible = binary.ReadBoolean();
			this.position = func();
			this.rotation = func();
			this.scale = func();
			if (DeskManager.item_detail_data_dic.ContainsKey(this.id))
			{
				this.item_data = DeskManager.item_detail_data_dic[this.id];
			}
		}

		public int id;

		public bool visible;

		public bool only_season_visible;

		public Vector3 position;

		public Vector3 rotation;

		public Vector3 scale;

		public DeskManager.ItemData item_data;

		public GameObject item_object;
	}
}
