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

namespace SceneEditWindow
{
	[AddComponentMenu("SceneEditWindow/CustomViewItem")]
	[RequireComponent(typeof(UITexture))]
	public class CustomViewItem : MonoBehaviour
	{
		private void Awake()
		{
			this.iconTex = base.GetComponent<UITexture>();
			this.button = base.GetComponentInChildren<UIButton>();
			NDebug.AssertNull(this.button);
			EventDelegate.Add(this.button.onClick, new EventDelegate.Callback(this.OnClickButton));
			this.buttonTweenTarget = this.button.tweenTarget;
		}

		public bool UpdateIcon(Maid maid = null)
		{
			if (maid == null)
			{
				maid = GameMain.Instance.CharacterMgr.GetMaid(0);
				if (maid == null)
				{
					return false;
				}
			}
			if (this.mpn == MPN.chikubi)
			{
				this.mpn = MPN.chikubicolor;
			}
			bool result = false;
			int num = maid.GetProp(this.mpn).nFileNameRID;
			if (this.sceneEdit.modeType == SceneEdit.ModeType.CostumeEdit && maid.GetProp(this.mpn).nTempFileNameRID != 0)
			{
				num = maid.GetProp(this.mpn).nTempFileNameRID;
			}
			foreach (SceneEdit.SubPropMpnData subPropMpnData in this.sceneEdit.subPropDatas)
			{
				if (this.mpn == subPropMpnData.mpn)
				{
					SubProp subProp = maid.GetSubProp(this.mpn, 0);
					if (subProp != null)
					{
						num = subProp.nFileNameRID;
					}
				}
			}
			if (num != this.settingRid)
			{
				result = true;
				this.settingRid = num;
				SceneEdit.SMenuItem menuItem = this.GetMenuItem(maid, this.mpn);
				if (menuItem == null || (menuItem.m_boDelOnly && this.defaultIconTexture != null) || (menuItem.m_texIcon == null && this.defaultIconTexture != null))
				{
					this.iconTex.mainTexture = this.defaultIconTexture;
					this.button.tweenTarget = null;
					this.iconTex.color = new Color(1f, 1f, 1f, 0.58f);
					TweenColor component = base.GetComponent<TweenColor>();
					if (component != null)
					{
						component.from = (component.to = this.iconTex.color);
					}
				}
				else if (menuItem.m_texIcon)
				{
					this.iconTex.mainTexture = menuItem.m_texIcon;
					this.button.tweenTarget = this.buttonTweenTarget;
					this.iconTex.color = Color.white;
					TweenColor component2 = base.GetComponent<TweenColor>();
					if (component2 != null)
					{
						component2.from = (component2.to = this.iconTex.color);
					}
				}
			}
			if (this.mpn == MPN.chikubicolor)
			{
				this.mpn = MPN.chikubi;
			}
			this.button.isEnabled = this.sceneEdit.isEnabledButton(this.mpn);
			if (!this.button.isEnabled)
			{
				this.iconTex.color = this.button.disabledColor;
			}
			return result;
		}

		public SceneEdit.SMenuItem GetMenuItem(Maid maid, MPN mpn)
		{
			int num = maid.GetProp(mpn).nFileNameRID;
			if (this.sceneEdit.modeType == SceneEdit.ModeType.CostumeEdit)
			{
				num = maid.GetProp(mpn).nTempFileNameRID;
			}
			foreach (SceneEdit.SubPropMpnData subPropMpnData in this.sceneEdit.subPropDatas)
			{
				if (mpn == subPropMpnData.mpn)
				{
					SubProp subProp = maid.GetSubProp(mpn, 0);
					if (subProp != null)
					{
						num = subProp.nFileNameRID;
					}
				}
			}
			List<SceneEdit.SCategory> categoryList = this.sceneEdit.CategoryList;
			SceneEditInfo.EMenuCategory category = this.GetCategory(mpn);
			bool flag = false;
			switch (mpn)
			{
			case MPN.skin:
				mpn = MPN.folder_skin;
				flag = true;
				break;
			default:
				if (mpn == MPN.chikubicolor)
				{
					mpn = MPN.chikubi;
					flag = true;
				}
				break;
			case MPN.underhair:
				mpn = MPN.folder_underhair;
				flag = true;
				break;
			case MPN.mayu:
				mpn = MPN.folder_mayu;
				flag = true;
				break;
			case MPN.eye:
				mpn = MPN.folder_eye;
				flag = true;
				break;
			}
			for (int j = 0; j < categoryList.Count; j++)
			{
				if (categoryList[j].m_eCategory == category)
				{
					for (int k = 0; k < categoryList[j].m_listPartsType.Count; k++)
					{
						if (categoryList[j].m_listPartsType[k].m_mpn == mpn)
						{
							for (int l = 0; l < categoryList[j].m_listPartsType[k].m_listMenu.Count; l++)
							{
								SceneEdit.SMenuItem smenuItem = categoryList[j].m_listPartsType[k].m_listMenu[l];
								if (smenuItem.m_nMenuFileRID == num)
								{
									return smenuItem;
								}
								if (flag)
								{
									foreach (SceneEdit.SMenuItem smenuItem2 in smenuItem.m_listColorSet)
									{
										if (smenuItem2.m_nMenuFileRID == num)
										{
											return smenuItem;
										}
									}
								}
							}
						}
					}
				}
			}
			return null;
		}

		public void OnClickButton()
		{
			this.sceneEdit.ClickEmulate(this.mpn, false);
		}

		private SceneEditInfo.EMenuCategory GetCategory(MPN mpn)
		{
			Dictionary<MPN, SceneEditInfo.CCateNameType> dicPartsTypePair = SceneEditInfo.m_dicPartsTypePair;
			if (!dicPartsTypePair.ContainsKey(mpn))
			{
				return SceneEditInfo.EMenuCategory.プロフィ\u30FCル;
			}
			return dicPartsTypePair[mpn].m_eMenuCate;
		}

		[SerializeField]
		public MPN mpn;

		[SerializeField]
		public SceneEdit sceneEdit;

		[NonSerialized]
		public Texture2D defaultIconTexture;

		private UIButton button;

		private GameObject buttonTweenTarget;

		private UITexture iconTex;

		private int settingRid;
	}
}
