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

public class LightWindow : BaseMaidPhotoWindow
{
	public override void Awake()
	{
		base.Awake();
		this.tabPanel = this.contentGrid.GetComponent<UIWFTabPanel>();
		EventDelegate.Add(this.addBtn.onClick, new EventDelegate.Callback(this.OnClickAddButton));
		EventDelegate.Add(this.removeBtn.onClick, new EventDelegate.Callback(this.OnClickRemoveButton));
		this.checkBoxVisible.onClick.Add(new Action<WFCheckBox>(this.OnClickCheckBoxVisible));
		this.checkBoxPoint.onClick.Add(new Action<WFCheckBox>(this.OnClickCheckBoxSwitching));
		this.checkBoxSpot.onClick.Add(new Action<WFCheckBox>(this.OnClickCheckBoxSwitching));
		this.cameraMoveSupport = base.gameObject.AddComponent<WfCameraMoveSupport>();
		EventDelegate.Add(this.cameraFocusBtn.onClick, new EventDelegate.Callback(this.OnClickCameraFocus));
		this.scrollView = NGUITools.FindInParents<UIScrollView>(this.contentGrid.transform);
		this.targetList.Add(new PhotoTransTargetObject(null, "メインライト", PhotoTransTargetObject.Type.Maid, Vector2.one));
		this.CreateTabButton(this.contentGrid.gameObject, this.targetList[0]);
		this.tabPanel.UpdateChildren();
		this.tabPanel.Select(this.contentGrid.GetChild(0).gameObject.GetComponentInChildren<UIWFTabButton>());
		Utility.ResetNGUI(this.contentGrid);
		Utility.ResetNGUI(this.scrollView);
		this.UpdateChildren();
	}

	public override void Start()
	{
		base.Start();
		this.AddTransTargetObject(this.InstantiateLight(), "1 : ポイントライト", PhotoTransTargetObject.Type.Light);
		this.AddTransTargetObject(this.InstantiateLight(), "2 : ポイントライト", PhotoTransTargetObject.Type.Light);
		this.AddTransTargetObject(this.InstantiateLight(), "3 : ポイントライト", PhotoTransTargetObject.Type.Light);
		this.AddTransTargetObject(this.InstantiateLight(), "4 : ポイントライト", PhotoTransTargetObject.Type.Light);
		this.UpdateChildren();
		this.DirectionalLightWindow.SetLightWindow(this);
		this.DirectionalLightWindow.Init(new Action(this.UpdateButtonInfo));
	}

	public void OnDestroy()
	{
		if (GameMain.Instance == null || GameMain.Instance.BgMgr == null || GameMain.Instance.BgMgr.bg_parent_object == null)
		{
			return;
		}
		Transform transform = GameMain.Instance.BgMgr.bg_parent_object.transform.Find("LightObject");
		if (transform != null)
		{
			UnityEngine.Object.DestroyImmediate(transform.gameObject);
		}
	}

	public void OnSelectItem(PhotoTransTargetObject select)
	{
		if (this.selectObject != null && this.selectObject.obj != null && this.selectObject.axis_obj != null && this.selectObject.rotate_obj != null)
		{
			this.selectObject.axis_obj.Visible = (this.selectObject.rotate_obj.Visible = false);
		}
		this.selectObject = select;
		if (select == null)
		{
			return;
		}
		if (select.obj == null)
		{
			this.directionalLightProp.SetActive(true);
			this.UserLightProp.SetActive(false);
			WFCheckBox wfcheckBox = this.checkBoxPoint;
			bool check = false;
			this.checkBoxSpot.check = check;
			wfcheckBox.check = check;
			this.DirectionalLightWindow.UpdateGui();
		}
		else
		{
			Light componentInChildren = select.obj.GetComponentInChildren<Light>(false);
			if (componentInChildren == null)
			{
				Debug.LogError("Light設定がnullです");
				return;
			}
			this.checkBoxVisible.check = componentInChildren.enabled;
			this.directionalLightProp.SetActive(false);
			this.UserLightProp.SetActive(true);
			if (componentInChildren.type == LightType.Point)
			{
				UTY.GetChildObject(this.UserLightProp, "Point", false).SetActive(true);
				UTY.GetChildObject(this.UserLightProp, "Spot", false).SetActive(false);
				this.checkBoxPoint.check = true;
				this.checkBoxSpot.check = false;
				this.pointLightWindow.Init(componentInChildren, select, new Action(this.UpdateButtonInfo));
			}
			else
			{
				UTY.GetChildObject(this.UserLightProp, "Point", false).SetActive(false);
				UTY.GetChildObject(this.UserLightProp, "Spot", false).SetActive(true);
				this.checkBoxPoint.check = false;
				this.checkBoxSpot.check = true;
				this.spotLightWindow.Init(componentInChildren, select, new Action(this.UpdateButtonInfo));
			}
		}
		this.UpdateButtonInfo();
	}

	public void OnClickAddButton()
	{
		this.AddTransTargetObject(this.InstantiateLight(), "◆", PhotoTransTargetObject.Type.Light);
	}

	public void OnClickRemoveButton()
	{
		if (this.selectObject == null)
		{
			return;
		}
		this.RemoveTransTargetObject(this.selectObject.obj);
	}

	public void OnClickCheckBoxVisible(WFCheckBox checkbox)
	{
		if (this.selectObject == null || this.selectObject.obj == null)
		{
			return;
		}
		Light[] componentsInChildren = this.selectObject.obj.GetComponentsInChildren<Light>(true);
		foreach (Light light in componentsInChildren)
		{
			light.enabled = checkbox.check;
		}
		this.UpdateButtonInfo();
	}

	public void OnClickCheckBoxSwitching(WFCheckBox checkbox)
	{
		if (this.selectObject == null || this.selectObject.obj == null)
		{
			return;
		}
		if (checkbox == this.checkBoxPoint)
		{
			this.checkBoxSpot.check = !checkbox.check;
		}
		else
		{
			this.checkBoxPoint.check = !checkbox.check;
		}
		if (this.checkBoxSpot.check)
		{
			UTY.GetChildObject(this.selectObject.obj, "LightPoint", false).SetActive(false);
			UTY.GetChildObject(this.selectObject.obj, "LightSpot", false).SetActive(true);
		}
		else
		{
			UTY.GetChildObject(this.selectObject.obj, "LightPoint", false).SetActive(true);
			UTY.GetChildObject(this.selectObject.obj, "LightSpot", false).SetActive(false);
		}
		this.OnSelectItem(this.selectObject);
	}

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

	public override void OnSerializeEvent()
	{
		Dictionary<string, Dictionary<string, string>> woldStoreData = base.GetWoldStoreData();
		woldStoreData.Clear();
		this.DirectionalLightWindow.OnSerializeEvent();
		for (int i = 1; i < this.targetList.Count; i++)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			PhotoTransTargetObject photoTransTargetObject = this.targetList[i];
			Transform transform = photoTransTargetObject.obj.transform;
			Light componentInChildren = photoTransTargetObject.obj.GetComponentInChildren<Light>(false);
			LightType type = componentInChildren.type;
			dictionary["light_type"] = type.ToString();
			dictionary["visible"] = componentInChildren.enabled.ToString();
			dictionary["position"] = transform.position.ToString("G9");
			dictionary["rotation"] = transform.rotation.ToString("G9");
			dictionary["color"] = componentInChildren.color.ToString("G9");
			dictionary["intensity"] = componentInChildren.intensity.ToString("G9");
			dictionary["range"] = componentInChildren.range.ToString("G9");
			if (type == LightType.Spot)
			{
				dictionary["spot_angle"] = componentInChildren.spotAngle.ToString("G9");
			}
			woldStoreData.Add(i.ToString(), dictionary);
		}
	}

	public override void OnDeserializeEvent()
	{
		this.DirectionalLightWindow.OnDeserializeEvent();
		Dictionary<string, Dictionary<string, string>> woldStoreData = base.GetWoldStoreData();
		if (woldStoreData.Count != this.targetList.Count)
		{
			Debug.LogError("ライトのセーブデータ数が一致していません");
		}
		int num = 1;
		while (woldStoreData.ContainsKey(num.ToString()))
		{
			Dictionary<string, string> dictionary = woldStoreData[num.ToString()];
			Transform transform = this.targetList[num].obj.transform;
			if ((LightType)Enum.Parse(typeof(LightType), dictionary["light_type"]) == LightType.Spot)
			{
				UTY.GetChildObject(transform.gameObject, "LightPoint", false).SetActive(false);
				UTY.GetChildObject(transform.gameObject, "LightSpot", false).SetActive(true);
			}
			else
			{
				UTY.GetChildObject(transform.gameObject, "LightPoint", false).SetActive(true);
				UTY.GetChildObject(transform.gameObject, "LightSpot", false).SetActive(false);
			}
			Light componentInChildren = this.targetList[num].obj.GetComponentInChildren<Light>(false);
			componentInChildren.enabled = bool.Parse(dictionary["visible"]);
			transform.position = Parse.Vector3(dictionary["position"]);
			transform.rotation = Parse.Quaternion(dictionary["rotation"]);
			componentInChildren.color = Parse.Color(dictionary["color"]);
			componentInChildren.intensity = float.Parse(dictionary["intensity"]);
			componentInChildren.range = float.Parse(dictionary["range"]);
			if (componentInChildren.type == LightType.Spot)
			{
				componentInChildren.spotAngle = float.Parse(dictionary["spot_angle"]);
			}
			num++;
		}
		this.UpdateButtonInfo();
		this.tabPanel.Select(this.contentGrid.GetChild(0).gameObject.GetComponentInChildren<UIWFTabButton>());
	}

	public void UpdateButtonInfo()
	{
		foreach (Transform transform in this.contentGrid.GetChildList())
		{
			PhotoTransTargetObject photoTransTargetObject = null;
			foreach (PhotoTransTargetObject photoTransTargetObject2 in this.targetList)
			{
				if (photoTransTargetObject2.guid == transform.gameObject.name)
				{
					photoTransTargetObject = photoTransTargetObject2;
					break;
				}
			}
			if (photoTransTargetObject != null)
			{
				Light light = GameMain.Instance.MainLight.GetComponent<Light>();
				if (photoTransTargetObject.obj != null)
				{
					light = photoTransTargetObject.obj.GetComponentInChildren<Light>();
				}
				if (!(light == null))
				{
					UILabel componentInChildren = transform.GetComponentInChildren<UILabel>();
					if (componentInChildren != null && light.type != LightType.Directional)
					{
						string text = componentInChildren.text;
						text = text.Replace("ポイントライト", "◆").Replace("スポットライト", "◆");
						componentInChildren.text = text.Replace("◆", (light.type != LightType.Point) ? "スポットライト" : "ポイントライト");
					}
					UISprite component = UTY.GetChildObject(transform.gameObject, "Plate/FrameThumbnail", false).GetComponent<UISprite>();
					if (light.type == LightType.Directional)
					{
						component.spriteName = "photo_icon_light_d";
					}
					else if (light.type == LightType.Spot)
					{
						component.spriteName = "photo_icon_light_spot";
					}
					else if (light.type == LightType.Point)
					{
						component.spriteName = "photo_icon_light_point";
					}
					Color color = light.color;
					color.a = ((!light.enabled) ? 0.2f : 1f);
					component.color = color;
				}
			}
		}
	}

	public void AddTransTargetObject(GameObject target_obj, string draw_name, PhotoTransTargetObject.Type type)
	{
		if (target_obj == null)
		{
			return;
		}
		PhotoTransTargetObject photoTransTargetObject = new PhotoTransTargetObject(target_obj, draw_name, type, Vector2.one);
		this.targetList.Add(photoTransTargetObject);
		UIWFTabButton uiwftabButton = this.CreateTabButton(this.contentGrid.gameObject, photoTransTargetObject);
		UIEventTrigger[] componentsInChildren = uiwftabButton.gameObject.GetComponentsInChildren<UIEventTrigger>();
		List<EventDelegate> onPress = this.contentGrid.GetChild(0).GetComponentInChildren<UIEventTrigger>().onPress;
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			componentsInChildren[i].onPress = onPress;
		}
		Dictionary<string, Transform> dictionary = new Dictionary<string, Transform>();
		List<Transform> childList = this.contentGrid.GetChildList();
		for (int j = 0; j < childList.Count; j++)
		{
			dictionary.Add(childList[j].name, childList[j].transform);
		}
		for (int k = 0; k < this.targetList.Count; k++)
		{
			if (dictionary.ContainsKey(this.targetList[k].guid))
			{
				dictionary[this.targetList[k].guid].localPosition = new Vector3(0f, (float)(k * 10), 0f);
			}
		}
		this.tabPanel.UpdateChildren();
		this.contentGrid.Reposition();
		this.scrollView.UpdatePosition();
	}

	public void RemoveTransTargetObject(GameObject target_obj)
	{
		if (target_obj == null)
		{
			return;
		}
		UIWFTabButton selectButtonObject = this.tabPanel.GetSelectButtonObject();
		string text = string.Empty;
		for (int i = 0; i < this.targetList.Count; i++)
		{
			if (this.targetList[i].obj == target_obj)
			{
				text = this.targetList[i].guid;
				this.targetList[i].Delete();
				this.targetList.RemoveAt(i);
				this.DeletedLight(target_obj);
				break;
			}
		}
		if (!string.IsNullOrEmpty(text))
		{
			if (selectButtonObject != null && selectButtonObject.name == text)
			{
				this.tabPanel.Select(this.contentGrid.GetChild(0).gameObject.GetComponent<UIWFTabButton>());
			}
			List<Transform> childList = this.contentGrid.GetChildList();
			for (int j = 0; j < childList.Count; j++)
			{
				if (childList[j].gameObject.name == text)
				{
					childList[j].gameObject.transform.SetParent(null, false);
					UnityEngine.Object.Destroy(childList[j].gameObject);
				}
			}
			int num = 5;
			Utility.ResetNGUI(this.contentGrid);
			if (this.contentGrid.GetChildList().Count <= num)
			{
				Utility.ResetNGUI(this.scrollView);
			}
			else
			{
				this.scrollView.UpdatePosition();
				this.scrollView.UpdateScrollbars();
			}
			this.tabPanel.UpdateChildren();
		}
	}

	public Dictionary<string, Dictionary<string, string>> GetStoreData()
	{
		return base.mgr.GetWoldStoreData(this);
	}

	private UIWFTabButton CreateTabButton(GameObject parent_obj, PhotoTransTargetObject target)
	{
		GameObject gameObject = Utility.CreatePrefab(parent_obj, "ScenePhotoMode/WindowsListItemLight", true);
		gameObject.name = target.guid.ToString();
		gameObject.GetComponentInChildren<UILabel>().text = target.draw_name;
		UIWFTabButton componentInChildren = gameObject.GetComponentInChildren<UIWFTabButton>();
		EventDelegate.Add(componentInChildren.onSelect, delegate()
		{
			if (!UIWFSelectButton.current.isSelected)
			{
				return;
			}
			for (int i = 0; i < this.targetList.Count; i++)
			{
				if (this.targetList[i].guid == UIWFSelectButton.current.name)
				{
					this.OnSelectItem(this.targetList[i]);
					break;
				}
			}
		});
		return componentInChildren;
	}

	private GameObject InstantiateLight()
	{
		Transform transform = GameMain.Instance.BgMgr.bg_parent_object.transform.Find("LightObject");
		if (transform == null)
		{
			GameObject gameObject = new GameObject("LightObject");
			gameObject.transform.SetParent(GameMain.Instance.BgMgr.bg_parent_object.transform, false);
			transform = gameObject.transform;
		}
		GameObject gameObject2 = new GameObject();
		if (!string.IsNullOrEmpty(base.name))
		{
			gameObject2.name = Guid.NewGuid().ToString();
		}
		gameObject2.transform.SetParent(transform, false);
		gameObject2.transform.localPosition = new Vector3(0f, 1f, 0f);
		GameObject gameObject3 = new GameObject();
		gameObject3.name = "LightPoint";
		Light light = gameObject3.AddComponent<Light>();
		light.type = LightType.Point;
		light.range = 5f;
		light.enabled = false;
		gameObject3.transform.SetParent(gameObject2.transform, false);
		GameObject gameObject4 = new GameObject();
		gameObject4.name = "LightSpot";
		gameObject4.transform.localEulerAngles = new Vector3(90f, 0f, 0f);
		light = gameObject4.AddComponent<Light>();
		light.type = LightType.Spot;
		light.enabled = false;
		light.range = 3f;
		light.intensity = 5f;
		light.spotAngle = 30f;
		gameObject4.transform.SetParent(gameObject2.transform, false);
		gameObject3.SetActive(true);
		gameObject4.SetActive(false);
		return gameObject2;
	}

	private bool DeletedLight(GameObject obj)
	{
		Transform transform = GameMain.Instance.BgMgr.bg_parent_object.transform.Find("LightObject");
		if (transform == null)
		{
			return false;
		}
		for (int i = 0; i < transform.childCount; i++)
		{
			if (obj == transform.GetChild(i).gameObject)
			{
				UnityEngine.Object.DestroyImmediate(obj);
				return true;
			}
		}
		return false;
	}

	[SerializeField]
	private UIGrid contentGrid;

	[SerializeField]
	private UIButton addBtn;

	[SerializeField]
	private UIButton removeBtn;

	[SerializeField]
	private UIButton cameraFocusBtn;

	[SerializeField]
	private GameObject directionalLightProp;

	[SerializeField]
	private GameObject UserLightProp;

	[SerializeField]
	private WFCheckBox checkBoxVisible;

	[SerializeField]
	private WFCheckBox checkBoxPoint;

	[SerializeField]
	private WFCheckBox checkBoxSpot;

	[SerializeField]
	private WindowPartsPointLight pointLightWindow;

	[SerializeField]
	private WindowPartsSpotLight spotLightWindow;

	public WindowPartsDirectionalLight DirectionalLightWindow;

	private UIWFTabPanel tabPanel;

	private WfCameraMoveSupport cameraMoveSupport;

	private UIScrollView scrollView;

	private PhotoTransTargetObject selectObject;

	private List<PhotoTransTargetObject> targetList = new List<PhotoTransTargetObject>();
}
