using System;
using UnityEngine;
using wf;

public class SubSlotEditManager : MonoBehaviour
{
	public MPN targetMpn { get; private set; }

	public SubSlotEditItem[] sloatItems { get; private set; }

	public bool visible
	{
		get
		{
			return this.sloatItems != null && this.sloatItems.Length != 0 && this.sloatItems[0].gameObject == this.grid;
		}
		set
		{
			if (this.sloatItems == null || this.sloatItems.Length == 0)
			{
				return;
			}
			if (value && this.sloatItems[0].transform.parent != this.grid.transform)
			{
				foreach (SubSlotEditItem subSlotEditItem in this.sloatItems)
				{
					subSlotEditItem.transform.SetParent(this.grid.transform, false);
				}
				Utility.ResetNGUI(this.grid);
			}
			else if (!value && this.sloatItems[0].transform.parent == this.grid.transform)
			{
				foreach (SubSlotEditItem subSlotEditItem2 in this.sloatItems)
				{
					subSlotEditItem2.transform.SetParent(this.standbyObject.transform, false);
				}
				Utility.ResetNGUI(this.grid);
			}
		}
	}

	private void Awake()
	{
		this.targetMpn = MPN.null_mpn;
		try
		{
			this.targetMpn = (MPN)Enum.Parse(typeof(MPN), this.targetMpnName);
		}
		catch (Exception e)
		{
			NDebug.AssertParseError("MPN", e);
		}
		this.standbyObject = new GameObject();
		this.standbyObject.transform.SetParent(GameObject.Find("UI Root").transform, false);
		this.standbyObject.transform.localScale = Vector3.zero;
		this.standbyObject.name = "_subSlot_standbyObject_" + this.targetMpnName.ToString();
	}

	private void Start()
	{
		if (this.sloatItems == null)
		{
			this.sloatItems = new SubSlotEditItem[this.maxSlot];
			for (int i = 0; i < this.maxSlot; i++)
			{
				this.sloatItems[i] = this.CreateSubSloatEditItem(this.standbyObject, i);
			}
		}
	}

	private void OnDestroy()
	{
		if (this.sloatItems != null)
		{
			for (int i = 0; i < this.sloatItems.Length; i++)
			{
				if (this.sloatItems[i] != null && this.sloatItems[i].gameObject != null)
				{
					UnityEngine.Object.DestroyImmediate(this.sloatItems[i].gameObject);
				}
			}
		}
		UnityEngine.Object.DestroyImmediate(this.standbyObject);
	}

	public void SetItem(SceneEdit.SMenuItem menu)
	{
		if (menu.m_boDelOnly)
		{
			this.sceneEdit.maid.SetProp(menu.m_strCateName, menu.m_strMenuFileName, menu.m_nMenuFileRID, false, false);
			return;
		}
		bool flag = false;
		bool flag2 = false;
		SceneEdit.SMenuItem[] array = new SceneEdit.SMenuItem[this.maxSlot];
		for (int i = 0; i < this.maxSlot; i++)
		{
			if (this.sloatItems[i].menuItem == null)
			{
				flag2 = true;
			}
			else if (this.sloatItems[i].menuItem != menu)
			{
				array[i] = this.sloatItems[i].menuItem;
			}
			else
			{
				flag = true;
			}
		}
		if (!flag2 && !flag)
		{
			flag = true;
			array[array.Length - 1] = menu;
		}
		if (flag)
		{
			MaidProp prop = this.sceneEdit.maid.GetProp(menu.m_strCateName);
			this.sceneEdit.maid.SetProp(menu.m_strCateName, prop.strFileName, prop.nFileNameRID, false, false);
			int num = 0;
			for (int j = 0; j < array.Length; j++)
			{
				if (array[j] != null)
				{
					this.sloatItems[num++].SetItem(array[j]);
				}
			}
			return;
		}
		for (int k = 0; k < this.maxSlot; k++)
		{
			if (this.sloatItems[k].menuItem == null)
			{
				this.sloatItems[k].SetItem(menu);
				break;
			}
		}
		if (!flag2)
		{
			this.sloatItems[this.sloatItems.Length - 1].SetItem(menu);
		}
	}

	public void UpdateAllItem()
	{
		foreach (SubSlotEditItem subSlotEditItem in this.sloatItems)
		{
			subSlotEditItem.UpdateSloatInformation();
		}
	}

	private void OnClickItemEvent(SubSlotEditItem editItem)
	{
		MPN mpn = editItem.menuItem.m_mpn;
		this.SetItem(editItem.menuItem);
		this.sceneEdit.maid.AllProcProp();
		this.sceneEdit.UpdateCurrentItemPanel(false);
		this.sceneEdit.customViewWindow.UpdateAllItem();
	}

	private SubSlotEditItem CreateSubSloatEditItem(GameObject parentObject, int sloatNo)
	{
		GameObject gameObject = Utility.CreatePrefab(parentObject, "SceneEdit/MainMenu/Prefab/ButtonItem", true);
		SubSlotEditItem subSlotEditItem = gameObject.AddComponent<SubSlotEditItem>();
		subSlotEditItem.sceneEdit = this.sceneEdit;
		subSlotEditItem.targetMpn = this.targetMpn;
		subSlotEditItem.sloatNo = sloatNo;
		SubSlotEditItem subSlotEditItem2 = subSlotEditItem;
		subSlotEditItem2.onClickEvent = (Action<SubSlotEditItem>)Delegate.Combine(subSlotEditItem2.onClickEvent, new Action<SubSlotEditItem>(this.OnClickItemEvent));
		return subSlotEditItem;
	}

	[SerializeField]
	private SceneEdit sceneEdit;

	[SerializeField]
	private UIGrid grid;

	[SerializeField]
	public int maxSlot;

	[SerializeField]
	public string targetMpnName;

	private GameObject standbyObject;
}
