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

public class PhotoBGObjectData
{
	private PhotoBGObjectData()
	{
	}

	public static void Create()
	{
		if (PhotoBGObjectData.bg_data_ != null)
		{
			return;
		}
		PhotoBGObjectData.bg_data_ = new List<PhotoBGObjectData>();
		HashSet<int> hashSet = new HashSet<int>();
		CsvCommonIdManager.ReadEnabledIdList(CsvCommonIdManager.FileSystemType.Normal, true, "phot_bg_object_enabled_list", ref hashSet);
		using (AFileBase afileBase = GameUty.FileSystem.FileOpen("phot_bg_object_list.nei"))
		{
			using (CsvParser csvParser = new CsvParser())
			{
				bool condition = csvParser.Open(afileBase);
				NDebug.Assert(condition, "phot_bg_object_list.nei\nopen failed.");
				for (int i = 1; i < csvParser.max_cell_y; i++)
				{
					if (csvParser.IsCellToExistData(0, i) && hashSet.Contains(csvParser.GetCellAsInteger(0, i)))
					{
						int num = 0;
						PhotoBGObjectData photoBGObjectData = new PhotoBGObjectData();
						photoBGObjectData.id = (long)csvParser.GetCellAsInteger(num++, i);
						photoBGObjectData.category = csvParser.GetCellAsString(num++, i);
						photoBGObjectData.name = csvParser.GetCellAsString(num++, i);
						photoBGObjectData.create_prefab_name = csvParser.GetCellAsString(num++, i);
						photoBGObjectData.create_asset_bundle_name = csvParser.GetCellAsString(num++, i);
						string cellAsString = csvParser.GetCellAsString(num++, i);
						if (string.IsNullOrEmpty(cellAsString) || PluginData.IsEnabled(cellAsString))
						{
							PhotoBGObjectData.bg_data_.Add(photoBGObjectData);
						}
					}
				}
			}
		}
		Action<string, List<string>> action = delegate(string path, List<string> result_list)
		{
			string[] files = Directory.GetFiles(path);
			for (int k = 0; k < files.Length; k++)
			{
				if (Path.GetExtension(files[k]) == ".poj")
				{
					result_list.Add(files[k]);
				}
			}
		};
		List<string> list = new List<string>();
		action(ObjectCreateWindow.folder_path, list);
		foreach (string fullPath in list)
		{
			PhotoBGObjectData.AddMyObject(fullPath);
		}
		PhotoBGObjectData.category_list_ = new Dictionary<string, List<PhotoBGObjectData>>();
		PhotoBGObjectData.popup_menu_list_ = new List<KeyValuePair<string, UnityEngine.Object>>();
		HashSet<string> hashSet2 = new HashSet<string>();
		for (int j = 0; j < PhotoBGObjectData.bg_data_.Count; j++)
		{
			if (!PhotoBGObjectData.category_list_.ContainsKey(PhotoBGObjectData.bg_data_[j].category))
			{
				PhotoBGObjectData.category_list_.Add(PhotoBGObjectData.bg_data_[j].category, new List<PhotoBGObjectData>());
			}
			PhotoBGObjectData.category_list_[PhotoBGObjectData.bg_data_[j].category].Add(PhotoBGObjectData.bg_data_[j]);
			if (!hashSet2.Contains(PhotoBGObjectData.bg_data_[j].category))
			{
				PhotoBGObjectData.popup_menu_list_.Add(new KeyValuePair<string, UnityEngine.Object>(PhotoBGObjectData.bg_data_[j].category, null));
				hashSet2.Add(PhotoBGObjectData.bg_data_[j].category);
			}
		}
		if (!hashSet2.Contains("マイオブジェクト"))
		{
			PhotoBGObjectData.popup_menu_list_.Add(new KeyValuePair<string, UnityEngine.Object>("マイオブジェクト", null));
			PhotoBGObjectData.category_list_.Add("マイオブジェクト", new List<PhotoBGObjectData>());
		}
	}

	public static PhotoBGObjectData AddMyObject(string fullPath)
	{
		if (!File.Exists(fullPath))
		{
			return null;
		}
		using (FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read))
		{
			using (BinaryReader binaryReader = new BinaryReader(fileStream))
			{
				string a = binaryReader.ReadString();
				if (a != "COM3D2_PHOTO_CUSTOM_OBJECT")
				{
					return null;
				}
			}
		}
		string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(Path.GetFileName(fullPath));
		PhotoBGObjectData photoBGObjectData = new PhotoBGObjectData();
		photoBGObjectData.id = (long)fileNameWithoutExtension.GetHashCode();
		photoBGObjectData.category = "マイオブジェクト";
		photoBGObjectData.name = fileNameWithoutExtension;
		photoBGObjectData.direct_file = fullPath;
		PhotoBGObjectData.bg_data_.Add(photoBGObjectData);
		return photoBGObjectData;
	}

	public static List<PhotoBGObjectData> data
	{
		get
		{
			return PhotoBGObjectData.bg_data_;
		}
	}

	public static Dictionary<string, List<PhotoBGObjectData>> category_list
	{
		get
		{
			return PhotoBGObjectData.category_list_;
		}
	}

	public static List<KeyValuePair<string, UnityEngine.Object>> popup_category_list
	{
		get
		{
			return PhotoBGObjectData.popup_menu_list_;
		}
	}

	public static PhotoBGObjectData Get(long id)
	{
		for (int i = 0; i < PhotoBGObjectData.bg_data_.Count; i++)
		{
			if (PhotoBGObjectData.bg_data_[i].id == id)
			{
				return PhotoBGObjectData.bg_data_[i];
			}
		}
		return null;
	}

	public GameObject Instantiate(string name)
	{
		Transform transform = GameMain.Instance.BgMgr.bg_parent_object.transform.Find("PhotoPrefab");
		if (transform == null)
		{
			GameObject gameObject = new GameObject("PhotoPrefab");
			gameObject.transform.SetParent(GameMain.Instance.BgMgr.bg_parent_object.transform, false);
			transform = gameObject.transform;
		}
		GameObject gameObject2 = null;
		if (!string.IsNullOrEmpty(this.create_prefab_name))
		{
			UnityEngine.Object @object = Resources.Load("Prefab/" + this.create_prefab_name);
			if (@object == null)
			{
				return null;
			}
			gameObject2 = (UnityEngine.Object.Instantiate(@object) as GameObject);
		}
		else if (!string.IsNullOrEmpty(this.create_asset_bundle_name))
		{
			GameObject gameObject3 = GameMain.Instance.BgMgr.CreateAssetBundle(this.create_asset_bundle_name);
			if (gameObject3 == null)
			{
				return null;
			}
			gameObject2 = UnityEngine.Object.Instantiate<GameObject>(gameObject3);
		}
		else if (!string.IsNullOrEmpty(this.direct_file))
		{
			BasePhotoCustomObject basePhotoCustomObject = BasePhotoCustomObject.InstantiateFromFile(transform.gameObject, this.direct_file);
			gameObject2 = basePhotoCustomObject.gameObject;
		}
		if (gameObject2 == null)
		{
			return null;
		}
		if (gameObject2.GetComponentInChildren<BoxCollider>() == null)
		{
			MeshRenderer componentInChildren = gameObject2.GetComponentInChildren<MeshRenderer>(true);
			if (componentInChildren != null)
			{
				componentInChildren.gameObject.AddComponent<BoxCollider>();
			}
		}
		if (!string.IsNullOrEmpty(name))
		{
			gameObject2.name = name;
		}
		gameObject2.transform.SetParent(transform);
		return gameObject2;
	}

	public long id;

	public string category;

	public string name;

	public string create_prefab_name;

	public string create_asset_bundle_name;

	public string direct_file;

	private static List<PhotoBGObjectData> bg_data_;

	private static Dictionary<string, List<PhotoBGObjectData>> category_list_;

	private static List<KeyValuePair<string, UnityEngine.Object>> popup_menu_list_;
}
