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

public class ObjectCreateWindow : BaseMaidPhotoWindow
{
	public static string folder_path
	{
		get
		{
			string text = PhotoWindowManager.path_photo_folder + "MyObject";
			if (!Directory.Exists(text))
			{
				Directory.CreateDirectory(text);
			}
			return text;
		}
	}

	public bool visibleTextureSelectWindow
	{
		get
		{
			return this.textureSelectSubtWindow.visible;
		}
		set
		{
			this.textureSelectSubtWindow.visible = value;
		}
	}

	public override void Awake()
	{
		base.Awake();
		EventDelegate.Add(this.selectTextureButton.onClick, new EventDelegate.Callback(this.OnClickSelectTextureButton));
		if (this.typeSelectPopupList != null)
		{
			EventDelegate.Add(this.typeSelectPopupList.onChange, delegate()
			{
				if (this.selectData == this.target_list_[0])
				{
					GameObject parent = GameObject.Find("CustomObjectPrev");
					GameObject gameObject3 = null;
					if (UIPopupList.current.value.IndexOf("プレーン") != -1 && this.selectData.customObject.type != BasePhotoCustomObject.Type.Plane)
					{
						gameObject3 = Utility.CreatePrefab(parent, "ScenePhotoMode/CustomObject/Plane", false);
					}
					else if (UIPopupList.current.value.IndexOf("スフィア") != -1 && this.selectData.customObject.type != BasePhotoCustomObject.Type.Sphere)
					{
						gameObject3 = Utility.CreatePrefab(parent, "ScenePhotoMode/CustomObject/Sphere", false);
					}
					if (gameObject3 != null)
					{
						int key = -1;
						foreach (KeyValuePair<int, BasePhotoCustomObject> keyValuePair in this.buttonDic)
						{
							if (keyValuePair.Value == this.selectData.customObject)
							{
								key = keyValuePair.Key;
								break;
							}
						}
						BasePhotoCustomObject component3 = gameObject3.GetComponent<BasePhotoCustomObject>();
						ObjectCreateWindow.Data value = new ObjectCreateWindow.Data(component3);
						this.target_list_[0] = value;
						this.buttonDic[key] = component3;
						UnityEngine.Object.DestroyImmediate(this.selectData.customObject.gameObject);
						this.selectData = null;
						this.selectData = value;
						this.UpdateObjectEditUI(this.selectData);
					}
				}
			});
		}
		this.editScaleSlider.ResetValue();
		this.editScaleSlider.onChangeValue.Add(delegate(float val)
		{
			if (this.selectData != null)
			{
				this.selectData.customObject.scale = val;
			}
		});
		this.texColorPalette.onChangeValue.Add(delegate(WindowPartsInputColorrPalette colorInputs, Color setColor)
		{
			if (this.selectData != null)
			{
				this.selectData.customObject.textureColor = setColor;
			}
		});
		EventDelegate.Add(this.planeBackDrawPopupList.onChange, delegate()
		{
			if (this.selectData != null && this.selectData.customObject.type == BasePhotoCustomObject.Type.Plane)
			{
				PhotoCustomObjectPlane.BackRenderingType backRenderingType = PhotoCustomObjectPlane.BackRenderingType.Mirror;
				if (UIPopupList.current.value.IndexOf("反転") == 0)
				{
					backRenderingType = PhotoCustomObjectPlane.BackRenderingType.Flip;
				}
				else if (UIPopupList.current.value.IndexOf("透明") == 0)
				{
					backRenderingType = PhotoCustomObjectPlane.BackRenderingType.None;
				}
				(this.selectData.customObject as PhotoCustomObjectPlane).backRenderingType = backRenderingType;
			}
		});
		EventDelegate.Add(this.sphereBackDrawPopupList.onChange, delegate()
		{
			if (this.selectData != null && this.selectData.customObject.type == BasePhotoCustomObject.Type.Sphere)
			{
				PhotoCustomObjectSphere.RenderingType renderingType = PhotoCustomObjectSphere.RenderingType.TwoSides;
				if (UIPopupList.current.value.IndexOf("表面表示") == 0)
				{
					renderingType = PhotoCustomObjectSphere.RenderingType.Front;
				}
				else if (UIPopupList.current.value.IndexOf("裏面表示") == 0)
				{
					renderingType = PhotoCustomObjectSphere.RenderingType.Back;
				}
				(this.selectData.customObject as PhotoCustomObjectSphere).renderingType = renderingType;
			}
		});
		this.camera_move_support_ = base.gameObject.AddComponent<WfCameraMoveSupport>();
		this.scroll_view_ = NGUITools.FindInParents<UIScrollView>(this.objectListGrid.transform);
		EventDelegate.Add(this.saveButton.onClick, new EventDelegate.Callback(this.OnClickSave));
		EventDelegate.Add(this.textureRestoreButton.onClick, new EventDelegate.Callback(this.OnClickTextureRestore));
		this.CheckPrevVisible.check = false;
		this.CheckPrevVisible.onClick.Add(delegate(WFCheckBox check_box)
		{
			if (this.selectData != null)
			{
				this.selectData.customObject.visible = check_box.check;
			}
		});
		this.textureSelectSubtWindow.GetButtonOnClickEventList(BasePhotoSubWindow.BtnType.End).Add(delegate
		{
			this.visibleTextureSelectWindow = false;
		});
		this.textureSelectSubtWindow.SetButtonEnabled(BasePhotoSubWindow.BtnType.End, true, false);
		GameObject gameObject = GameObject.Find("CustomObjectPrev");
		if (gameObject == null)
		{
			gameObject = new GameObject("CustomObjectPrev");
		}
		gameObject.transform.localPosition = new Vector3(0f, 1.5f, -0.8f);
		GameObject gameObject2 = Utility.CreatePrefab(gameObject, "ScenePhotoMode/CustomObject/Plane", false);
		BasePhotoCustomObject component = gameObject2.GetComponent<BasePhotoCustomObject>();
		this.target_list_.Add(new ObjectCreateWindow.Data(component));
		this.CreateTabButton(this.objectListGrid.gameObject, component, "新規オブジェクト");
		Action<string, List<string>> action = delegate(string path, List<string> result_list)
		{
			string[] files = Directory.GetFiles(path);
			for (int i = 0; i < files.Length; i++)
			{
				if (Path.GetExtension(files[i]) == ".poj")
				{
					result_list.Add(files[i]);
				}
			}
		};
		List<string> list = new List<string>();
		action(ObjectCreateWindow.folder_path, list);
		foreach (string text in list)
		{
			BasePhotoCustomObject basePhotoCustomObject = this.CreateLoadCustomObject(gameObject, text);
			if (!(basePhotoCustomObject == null))
			{
				this.target_list_.Add(new ObjectCreateWindow.Data(basePhotoCustomObject, text));
				this.CreateTabButton(this.objectListGrid.gameObject, basePhotoCustomObject, text);
			}
		}
		foreach (ObjectCreateWindow.Data data in this.target_list_)
		{
			data.customObject.visible = false;
		}
		UIWFTabPanel component2 = this.objectListGrid.GetComponent<UIWFTabPanel>();
		component2.UpdateChildren();
		component2.Select(this.objectListGrid.GetChild(0).gameObject.GetComponent<UIWFTabButton>());
		Utility.ResetNGUI(this.objectListGrid);
		Utility.ResetNGUI(this.scroll_view_);
		this.UpdateChildren();
	}

	public override void Start()
	{
		base.Start();
		this.visibleTextureSelectWindow = false;
	}

	private void OnDestroy()
	{
	}

	public void OnClickSelectTextureButton()
	{
		this.visibleTextureSelectWindow = !this.visibleTextureSelectWindow;
	}

	public void OnChangeSelectTexture(string pngPath)
	{
		this.textureExpTextObject.SetActive(false);
		if (!string.IsNullOrEmpty(this.selectData.fullPath) && this.selectData.backupPng == null)
		{
			byte[] imageBinarys = this.selectData.customObject.imageBinarys;
			this.selectData.backupPng = new byte[imageBinarys.Length];
			Array.Copy(imageBinarys, this.selectData.backupPng, imageBinarys.Length);
			this.textureRestoreButton.isEnabled = true;
		}
		this.selectData.customObject.SetTexture(pngPath);
		UI2DSprite componentInChildren = this.selectTextureButton.GetComponentInChildren<UI2DSprite>(true);
		Sprite sprite2D = Sprite.Create(this.selectData.customObject.mainTexture, new Rect(0f, 0f, (float)this.selectData.customObject.mainTexture.width, (float)this.selectData.customObject.mainTexture.height), default(Vector2));
		componentInChildren.sprite2D = sprite2D;
	}

	private void UpdateObjectEditUI(ObjectCreateWindow.Data targetData)
	{
		if (targetData == null)
		{
			return;
		}
		if (this.typeSelectPopupList != null)
		{
			this.typeSelectPopupList.value = ((targetData.customObject.type != BasePhotoCustomObject.Type.Plane) ? "スフィア" : "プレーン");
			this.typeSelectPopupList.GetComponent<UIButton>().isEnabled = (targetData == this.target_list_[0]);
		}
		if (!string.IsNullOrEmpty(targetData.fullPath))
		{
			this.saveButton.GetComponentInChildren<UILabel>().text = "更新";
			this.textureExpTextObject.SetActive(false);
		}
		else
		{
			this.saveButton.GetComponentInChildren<UILabel>().text = "保存";
			if (targetData.customObject.mainTexture != null)
			{
				this.textureExpTextObject.SetActive(false);
			}
			else
			{
				this.textureExpTextObject.SetActive(true);
			}
		}
		this.textureRestoreButton.gameObject.SetActive(!string.IsNullOrEmpty(targetData.fullPath));
		this.textureRestoreButton.isEnabled = (targetData.backupPng != null);
		foreach (ObjectCreateWindow.Data data in this.target_list_)
		{
			data.customObject.visible = (data == targetData && this.CheckPrevVisible.check);
		}
		BasePhotoCustomObject customObject = targetData.customObject;
		UI2DSprite componentInChildren = this.selectTextureButton.GetComponentInChildren<UI2DSprite>(true);
		if (customObject.mainTexture != null)
		{
			Sprite sprite2D = Sprite.Create(customObject.mainTexture, new Rect(0f, 0f, (float)customObject.mainTexture.width, (float)customObject.mainTexture.height), default(Vector2));
			componentInChildren.sprite2D = sprite2D;
		}
		else
		{
			componentInChildren.sprite2D = null;
		}
		this.editScaleSlider.value = customObject.scale;
		this.texColorPalette.ColorPalette.SetColor(customObject.textureColor);
		this.texColorPalette.ColorPalette.UIUpdate();
		this.planeUIParent.SetActive(false);
		this.sphereUIParent.SetActive(false);
		if (this.selectData.customObject.type == BasePhotoCustomObject.Type.Plane)
		{
			this.planeUIParent.SetActive(true);
			PhotoCustomObjectPlane.BackRenderingType backRenderingType = (this.selectData.customObject as PhotoCustomObjectPlane).backRenderingType;
			this.planeBackDrawPopupList.value = ((backRenderingType != PhotoCustomObjectPlane.BackRenderingType.Flip) ? ((backRenderingType != PhotoCustomObjectPlane.BackRenderingType.None) ? "鏡合わせ" : "透明") : "反転");
		}
		else if (this.selectData.customObject.type == BasePhotoCustomObject.Type.Sphere)
		{
			this.sphereUIParent.SetActive(true);
			PhotoCustomObjectSphere.RenderingType renderingType = (this.selectData.customObject as PhotoCustomObjectSphere).renderingType;
			this.sphereBackDrawPopupList.value = ((renderingType != PhotoCustomObjectSphere.RenderingType.TwoSides) ? ((renderingType != PhotoCustomObjectSphere.RenderingType.Front) ? "裏面表示" : "表面表示") : "両面表示");
		}
	}

	public void OnSelectItem(BasePhotoCustomObject select)
	{
		foreach (ObjectCreateWindow.Data data in this.target_list_)
		{
			if (data.customObject == select)
			{
				this.selectData = data;
				this.UpdateObjectEditUI(this.selectData);
				break;
			}
		}
	}

	public void OnClickTextureRestore()
	{
		if (this.selectData == null || this.selectData.backupPng == null)
		{
			return;
		}
		this.selectData.customObject.SetTexture(this.selectData.backupPng);
		UI2DSprite componentInChildren = this.selectTextureButton.GetComponentInChildren<UI2DSprite>(true);
		Sprite sprite2D = Sprite.Create(this.selectData.customObject.mainTexture, new Rect(0f, 0f, (float)this.selectData.customObject.mainTexture.width, (float)this.selectData.customObject.mainTexture.height), default(Vector2));
		componentInChildren.sprite2D = sprite2D;
		this.textureRestoreButton.isEnabled = false;
		this.selectData.backupPng = null;
	}

	public void OnClickSave()
	{
		if (this.selectData == null)
		{
			return;
		}
		if (this.selectData.customObject.mainTexture == null)
		{
			GameMain.Instance.SysDlg.Show("テクスチャが未設定です", SystemDialog.TYPE.OK, null, null);
			return;
		}
		if (string.IsNullOrEmpty(this.selectData.fullPath))
		{
			this.savePanel.Open(this.selectData.customObject, delegate(string filePath)
			{
				GameObject parent = GameObject.Find("CustomObjectPrev");
				BasePhotoCustomObject basePhotoCustomObject = this.CreateLoadCustomObject(parent, filePath);
				if (basePhotoCustomObject == null)
				{
					return;
				}
				this.target_list_.Add(new ObjectCreateWindow.Data(basePhotoCustomObject, filePath));
				this.CreateTabButton(this.objectListGrid.gameObject, basePhotoCustomObject, filePath);
				this.objectListGrid.GetComponent<UIWFTabPanel>().UpdateChildren();
				Utility.ResetNGUI(this.objectListGrid);
				Utility.ResetNGUI(this.scroll_view_);
				this.UpdateChildren();
				ObjectManagerWindow objectManagerWindow2 = base.mgr.GetWindow(PhotoWindowManager.WindowType.ObjectManager) as ObjectManagerWindow;
				objectManagerWindow2.createBgObjectWindow.AddMyObject(filePath);
			});
		}
		else
		{
			if (File.Exists(this.selectData.fullPath))
			{
				File.Delete(this.selectData.fullPath);
			}
			try
			{
				File.WriteAllBytes(this.selectData.fullPath, this.selectData.customObject.Serialize());
				ObjectManagerWindow objectManagerWindow = base.mgr.GetWindow(PhotoWindowManager.WindowType.ObjectManager) as ObjectManagerWindow;
				objectManagerWindow.createBgObjectWindow.UpdateMyObject(this.selectData.fullPath);
			}
			catch
			{
				GameMain.Instance.SysDlg.Show("オブジェクトの保存に失敗しました", SystemDialog.TYPE.OK, null, null);
			}
		}
	}

	public override void OnMaidAddEvent(Maid maid, bool is_deserialize_load)
	{
	}

	public override void OnMaidRemoveEventPrev(Maid maid)
	{
	}

	public void OnClickCameraFocus()
	{
		if (this.select_trans_target_ == null || this.select_trans_target_.obj == null)
		{
			return;
		}
		Vector3 position = this.select_trans_target_.obj.transform.position;
		float distance = 4f;
		Vector2 aroundAngle = new Vector2(-180f, 4.7f);
		this.camera_move_support_.StartCameraPosition(position, distance, aroundAngle);
	}

	private BasePhotoCustomObject CreateLoadCustomObject(GameObject parent, string filePath)
	{
		BasePhotoCustomObject basePhotoCustomObject = BasePhotoCustomObject.InstantiateFromFile(parent, filePath);
		if (basePhotoCustomObject == null)
		{
			return null;
		}
		string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(Path.GetFileName(filePath));
		basePhotoCustomObject.gameObject.name = fileNameWithoutExtension.GetHashCode().ToString();
		basePhotoCustomObject.visible = false;
		return basePhotoCustomObject;
	}

	private UIWFTabButton CreateTabButton(GameObject parent_obj, BasePhotoCustomObject target, string filePath)
	{
		GameObject gameObject = Utility.CreatePrefab(parent_obj, "ScenePhotoMode/WindowListItem", true);
		gameObject.name = Path.GetFileNameWithoutExtension(Path.GetFileName(filePath));
		gameObject.GetComponentInChildren<UILabel>().text = gameObject.name;
		UIWFTabButton component = gameObject.GetComponent<UIWFTabButton>();
		this.buttonDic.Add(component.GetHashCode(), target);
		EventDelegate.Add(component.onSelect, delegate()
		{
			if (!UIWFSelectButton.current.isSelected)
			{
				return;
			}
			if (this.buttonDic.ContainsKey(UIWFSelectButton.current.GetHashCode()))
			{
				this.OnSelectItem(this.buttonDic[UIWFSelectButton.current.GetHashCode()]);
			}
		});
		return component;
	}

	public override void OnSerializeEvent()
	{
	}

	public override void OnDeserializeEvent()
	{
	}

	public override KeyValuePair<string, Dictionary<string, string>> Serialize()
	{
		KeyValuePair<string, Dictionary<string, string>> result = base.Serialize();
		Dictionary<string, string> value = result.Value;
		result = new KeyValuePair<string, Dictionary<string, string>>(result.Key, value);
		return result;
	}

	public override void Deserialize(Dictionary<string, Dictionary<string, string>> data)
	{
		base.Deserialize(data);
		string key = base.gameObject.name + "title";
		if (!data.ContainsKey(key))
		{
			return;
		}
		this.visibleTextureSelectWindow = false;
	}

	public PhotoModeObjectSave savePanel;

	public UIButton selectTextureButton;

	public GameObject textureExpTextObject;

	public UIButton textureRestoreButton;

	public TextureSelectSubWindow textureSelectSubtWindow;

	public UIGrid objectListGrid;

	public UIPopupList typeSelectPopupList;

	public WFCheckBox CheckPrevVisible;

	public UIButton saveButton;

	public PhotoSliderAndInput editScaleSlider;

	public WindowPartsInputColorrPalette texColorPalette;

	public GameObject planeUIParent;

	public UIPopupList planeBackDrawPopupList;

	public GameObject sphereUIParent;

	public UIPopupList sphereBackDrawPopupList;

	private Dictionary<int, BasePhotoCustomObject> buttonDic = new Dictionary<int, BasePhotoCustomObject>();

	private List<ObjectCreateWindow.Data> target_list_ = new List<ObjectCreateWindow.Data>();

	private ObjectCreateWindow.Data selectData;

	private bool change_bg_;

	private PhotoTransTargetObject select_trans_target_;

	private WfCameraMoveSupport camera_move_support_;

	private UIScrollView scroll_view_;

	private class Data
	{
		public Data(BasePhotoCustomObject obj)
		{
			this.customObject = obj;
		}

		public Data(BasePhotoCustomObject obj, string fullPath)
		{
			this.customObject = obj;
			this.fullPath = fullPath;
		}

		public BasePhotoCustomObject customObject;

		public byte[] backupPng;

		public string fullPath;
	}
}
