using System;
using System.Collections.Generic;
using com.workman.cm3d2.scene.dailyEtc;
using UnityEngine;
using wf;

public class DeskItemWindow : BasePhotoWindow
{
	public override string windowName
	{
		get
		{
			return this.InitTitle;
		}
	}

	public override void Awake()
	{
		base.Awake();
		this.camera_move_support_ = base.gameObject.AddComponent<WfCameraMoveSupport>();
		this.PartsPopUpList.onChangePopUpListValue.Add(new Action<KeyValuePair<string, UnityEngine.Object>>(this.OnChangePopUpList));
		this.PartsTransForm.onChangeAxisVisibleType.Add(new Action(this.OnChangeAxisVisibleType));
		this.CheckBoxVisible.onClick.Add(new Action<WFCheckBox>(this.OnClickVisible));
		this.CheckBoxSeasonalVisible.onClick.Add(new Action<WFCheckBox>(this.OnClickSeasonalVisible));
	}

	public override void Start()
	{
		base.Start();
		base.SetButtonEnabled(BasePhotoWindow.BtnType.Help, true, false);
		base.GetButtonOnClickEventList(BasePhotoWindow.BtnType.Help).Add(new Action(this.OnClickHelp));
		if (string.IsNullOrEmpty(GameMain.Instance.BgMgr.GetBGName()))
		{
			GameMain.Instance.BgMgr.ChangeBg(DailyAPI.dayBg);
		}
		List<DeskManager.InstansData> item_inst_data = DeskManager.item_inst_data;
		int num = 0;
		foreach (DeskManager.InstansData instansData in item_inst_data)
		{
			if (instansData.item_data != null && instansData.IsPossessing() && !(instansData.item_object == null))
			{
				int category_id = instansData.item_data.category_id;
				if (!this.category_dic_.ContainsKey(category_id))
				{
					this.category_dic_.Add(category_id, new List<DeskManager.InstansData>());
				}
				this.category_dic_[category_id].Add(instansData);
				num = System.Math.Max(num, this.category_dic_[category_id].Count);
				DeskItemWindow.UIObject uiobject = DeskItemWindow.UIObject.Create(instansData.item_object);
				uiobject.axis_obj.offsetScale = instansData.item_data.init_ui_position_scale;
				uiobject.axis_obj.ScaleMinMax = new Vector2(this.PartsTransForm.ScaleInput.SliderMin, this.PartsTransForm.ScaleInput.SliderMax);
				if (GameMain.Instance.VRMode)
				{
					MeshRenderer componentInChildren = uiobject.axis_obj.GetComponentInChildren<MeshRenderer>();
					if (componentInChildren != null)
					{
						componentInChildren.enabled = false;
					}
					BoxCollider[] componentsInChildren = uiobject.axis_obj.GetComponentsInChildren<BoxCollider>();
					foreach (BoxCollider boxCollider in componentsInChildren)
					{
						boxCollider.enabled = false;
					}
				}
				uiobject.rotate_obj.offsetScale = instansData.item_data.init_ui_position_scale;
				this.uiobject_dic_.Add(instansData.item_object, uiobject);
			}
		}
		foreach (KeyValuePair<int, List<DeskManager.InstansData>> keyValuePair in this.category_dic_)
		{
			keyValuePair.Value.Sort((DeskManager.InstansData a, DeskManager.InstansData b) => a.id - b.id);
		}
		for (int j = 0; j < num; j++)
		{
			GameObject gameObject = Utility.CreatePrefab(this.PartsTabPabel.gameObject, "ScenePhotoMode/WindowListItemBig", true);
		}
		List<KeyValuePair<string, UnityEngine.Object>> list = new List<KeyValuePair<string, UnityEngine.Object>>();
		foreach (KeyValuePair<int, List<DeskManager.InstansData>> keyValuePair2 in this.category_dic_)
		{
			list.Add(new KeyValuePair<string, UnityEngine.Object>(DeskManager.item_category_data_dic[keyValuePair2.Key], null));
		}
		this.PartsPopUpList.popup_value_list = list;
		if (0 < this.PartsPopUpList.popup_value_list.Count)
		{
			this.PartsPopUpList.SetPopupValue(this.PartsPopUpList.popup_value_list[0].Key);
		}
		if (2 <= list.Count)
		{
			this.WindowSize.y = this.WindowSize.y + 30f;
			base.ResizeWindow();
			Transform transform = UTY.GetChildObject(base.gameObject, "Parent/ContentParent", false).transform;
			Vector3 localPosition = transform.localPosition;
			localPosition.y -= 30f;
			transform.localPosition = localPosition;
			transform = UTY.GetChildObject(base.gameObject, "Parent/ListParent", false).transform;
			localPosition = transform.localPosition;
			localPosition.y -= 30f;
			transform.localPosition = localPosition;
		}
	}

	protected void OnChangePopUpList(KeyValuePair<string, UnityEngine.Object> popup_val)
	{
		if (this.select_pop_up_value_name_ == popup_val.Key)
		{
			return;
		}
		this.select_pop_up_value_name_ = popup_val.Key;
		int num = -1;
		foreach (KeyValuePair<int, string> keyValuePair in DeskManager.item_category_data_dic)
		{
			if (keyValuePair.Value == popup_val.Key)
			{
				num = keyValuePair.Key;
				break;
			}
		}
		if (num < 0)
		{
			return;
		}
		List<DeskManager.InstansData> list = null;
		if (this.category_dic_.ContainsKey(num))
		{
			list = this.category_dic_[num];
		}
		if (list == null)
		{
			return;
		}
		Transform transform = this.PartsTabPabel.gameObject.transform;
		for (int i = 0; i < transform.childCount; i++)
		{
			if (i < list.Count)
			{
				DeskManager.InstansData instansData = list[i];
				GameObject gameObject = transform.GetChild(i).gameObject;
				gameObject.SetActive(true);
				string name = instansData.item_data.name;
				gameObject.name = instansData.id.ToString();
				UILabel componentInChildren = gameObject.GetComponentInChildren<UILabel>();
				componentInChildren.text = name;
				int j = componentInChildren.width;
				componentInChildren.width = 0;
				componentInChildren.MakePixelPerfect();
				while (j < componentInChildren.width)
				{
					componentInChildren.fontSize--;
					componentInChildren.width = 0;
					componentInChildren.MakePixelPerfect();
				}
				componentInChildren.width = j;
				UIWFTabButton component = gameObject.GetComponent<UIWFTabButton>();
				component.onSelect.Clear();
				EventDelegate.Add(component.onSelect, new EventDelegate.Callback(this.OnSelectItem));
			}
			else
			{
				transform.GetChild(i).gameObject.SetActive(false);
			}
		}
		UIGrid component2 = this.PartsTabPabel.GetComponent<UIGrid>();
		this.PartsTabPabel.UpdateChildren();
		Utility.ResetNGUI(component2);
		Utility.ResetNGUI(this.PartsScrollView);
		this.PartsTabPabel.Select(component2.GetChild(0).GetComponentInChildren<UIWFTabButton>());
		if (this.category_dic_.Count <= 1)
		{
			this.PartsPopUpList.gameObject.SetActive(false);
		}
	}

	protected void OnSelectItem()
	{
		if (!UIWFSelectButton.current.isSelected)
		{
			return;
		}
		int num = int.Parse(UIWFSelectButton.current.name);
		GameObject gameObject = null;
		DeskManager.InstansData instansData = null;
		List<DeskManager.InstansData> item_inst_data = DeskManager.item_inst_data;
		int num2 = 0;
		while (num2 < item_inst_data.Count && gameObject == null)
		{
			if (item_inst_data[num2].id == num)
			{
				gameObject = item_inst_data[num2].item_object;
				instansData = item_inst_data[num2];
			}
			num2++;
		}
		KeyValuePair<string, UnityEngine.Object> keyValuePair = new KeyValuePair<string, UnityEngine.Object>(string.Empty, gameObject);
		this.select_item_ = null;
		GameObject target_object = (!(keyValuePair.Value == null)) ? (keyValuePair.Value as GameObject) : null;
		this.CheckBoxVisible.enabled = false;
		this.CheckBoxSeasonalVisible.visible = false;
		if (keyValuePair.Value == null)
		{
			this.PartsTransForm.SetObject(null);
			return;
		}
		if (instansData == null)
		{
			return;
		}
		this.PartsTransForm.SetObject(target_object, instansData.item_data.init_position, instansData.item_data.init_rotation, instansData.item_data.init_scale);
		this.select_item_ = instansData;
		if (this.select_item_.item_data.seasonal)
		{
			this.CheckBoxSeasonalVisible.text = "特定期間のみ表示する(" + this.select_item_.item_data.seasonal_month[0].ToString() + "月)";
		}
		this.UpdateUI();
	}

	public void UpdateVisible()
	{
		if (this.select_item_ == null)
		{
			this.CheckBoxVisible.enabled = false;
			this.CheckBoxSeasonalVisible.visible = false;
			return;
		}
		this.CheckBoxVisible.enabled = true;
		this.CheckBoxVisible.check = this.select_item_.visible;
		if (this.select_item_.item_data.seasonal)
		{
			this.CheckBoxSeasonalVisible.visible = true;
			this.CheckBoxSeasonalVisible.check = this.select_item_.only_season_visible;
		}
		else
		{
			this.CheckBoxSeasonalVisible.visible = false;
		}
		List<DeskManager.InstansData> item_inst_data = DeskManager.item_inst_data;
		for (int i = 0; i < item_inst_data.Count; i++)
		{
			if (!(item_inst_data[i].item_object == null))
			{
				if (!item_inst_data[i].item_data.seasonal || !item_inst_data[i].visible || item_inst_data[i] == this.select_item_)
				{
					item_inst_data[i].item_object.SetActive(item_inst_data[i].visible);
				}
				else if (item_inst_data[i].only_season_visible)
				{
					item_inst_data[i].item_object.SetActive(item_inst_data[i].IsNowMonthSeason());
				}
			}
		}
	}

	public void UpdateUI()
	{
		this.UpdateVisible();
		this.OnChangeAxisVisibleType();
	}

	protected void OnChangeAxisVisibleType()
	{
		if (this.select_item_ == null)
		{
			return;
		}
		GameObject item_object = this.select_item_.item_object;
		WindowPartsTransform.AxisVisibleType axis_visible_type = this.PartsTransForm.axis_visible_type;
		foreach (KeyValuePair<GameObject, DeskItemWindow.UIObject> keyValuePair in this.uiobject_dic_)
		{
			DeskItemWindow.UIObject value = keyValuePair.Value;
			if (axis_visible_type == WindowPartsTransform.AxisVisibleType.UnVisible || item_object != keyValuePair.Key || !item_object.activeSelf)
			{
				value.axis_obj.Visible = false;
				value.rotate_obj.Visible = false;
			}
			else if (axis_visible_type == WindowPartsTransform.AxisVisibleType.Position)
			{
				value.axis_obj.Visible = true;
				value.rotate_obj.Visible = false;
			}
			else if (axis_visible_type == WindowPartsTransform.AxisVisibleType.Rotate)
			{
				value.axis_obj.Visible = false;
				value.rotate_obj.Visible = true;
			}
		}
	}

	public void OnClickCameraFocus()
	{
		if (this.select_item_ == null || this.select_item_.item_object == null)
		{
			return;
		}
		Transform transform = this.select_item_.item_object.transform;
		Vector3 position = transform.position;
		float distance = 1.6f;
		Vector2 aroundAngle = new Vector2(-90f, 4.7f);
		this.camera_move_support_.StartCameraPosition(position, distance, aroundAngle);
	}

	protected void OnClickVisible(WFCheckBox checkbox)
	{
		if (this.select_item_ == null)
		{
			return;
		}
		this.select_item_.visible = checkbox.check;
		this.UpdateUI();
	}

	protected void OnClickSeasonalVisible(WFCheckBox checkbox)
	{
		if (this.select_item_ == null || !this.select_item_.item_data.seasonal)
		{
			return;
		}
		this.select_item_.only_season_visible = checkbox.check;
		this.UpdateUI();
	}

	protected void OnClickHelp()
	{
		uGUITutorialPanel.OpenTutorial("SceneDeskCustomize", null, true);
	}

	public WFCheckBox CheckBoxVisible;

	public WFCheckBox CheckBoxSeasonalVisible;

	public WindowPartsPopUpList PartsPopUpList;

	public WindowPartsTransform PartsTransForm;

	public UIWFTabPanel PartsTabPabel;

	public UIScrollView PartsScrollView;

	private SortedDictionary<int, List<DeskManager.InstansData>> category_dic_ = new SortedDictionary<int, List<DeskManager.InstansData>>();

	private Dictionary<GameObject, DeskItemWindow.UIObject> uiobject_dic_ = new Dictionary<GameObject, DeskItemWindow.UIObject>();

	private DeskManager.InstansData select_item_;

	private string select_pop_up_value_name_ = string.Empty;

	private WfCameraMoveSupport camera_move_support_;

	public class UIObject
	{
		private UIObject()
		{
		}

		public static DeskItemWindow.UIObject Create(GameObject target_obj)
		{
			return new DeskItemWindow.UIObject
			{
				target_obj = target_obj,
				rotate_obj = PhotoWindowManager.CreateWorldTransformRotate(target_obj, false),
				axis_obj = PhotoWindowManager.CreateWorldTransformAxis(target_obj, false, false)
			};
		}

		public WorldTransformAxis axis_obj;

		public GizmoRender rotate_obj;

		public GameObject target_obj;
	}
}
