using System;
using UnityEngine;

namespace I2.Loc
{
	public class LocalizeTarget_NGUI_Label : LocalizeTarget<UILabel>
	{
		static LocalizeTarget_NGUI_Label()
		{
			LocalizeTarget_NGUI_Label.AutoRegister();
		}

		[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
		private static void AutoRegister()
		{
			LocalizationManager.RegisterTarget(new LocalizeTargetDesc_Type<UILabel, LocalizeTarget_NGUI_Label>
			{
				Name = "NGUI Label",
				Priority = 100
			});
		}

		public override eTermType GetPrimaryTermType(Localize cmp)
		{
			return eTermType.Text;
		}

		public override eTermType GetSecondaryTermType(Localize cmp)
		{
			return eTermType.UIFont;
		}

		public override bool CanUseSecondaryTerm()
		{
			return true;
		}

		public override bool AllowMainTermToBeRTL()
		{
			return true;
		}

		public override bool AllowSecondTermToBeRTL()
		{
			return false;
		}

		public override void GetFinalTerms(Localize cmp, string Main, string Secondary, out string primaryTerm, out string secondaryTerm)
		{
			primaryTerm = ((!this.mTarget) ? null : this.mTarget.text);
			secondaryTerm = ((!(this.mTarget.ambigiousFont != null)) ? string.Empty : this.mTarget.ambigiousFont.name);
		}

		public override void DoLocalize(Localize cmp, string mainTranslation, string secondaryTranslation)
		{
			Font secondaryTranslatedObj = cmp.GetSecondaryTranslatedObj<Font>(ref mainTranslation, ref secondaryTranslation);
			if (secondaryTranslatedObj != null)
			{
				if (secondaryTranslatedObj != this.mTarget.ambigiousFont)
				{
					this.mTarget.ambigiousFont = secondaryTranslatedObj;
				}
			}
			else
			{
				UIFont secondaryTranslatedObj2 = cmp.GetSecondaryTranslatedObj<UIFont>(ref mainTranslation, ref secondaryTranslation);
				if (secondaryTranslatedObj2 != null && this.mTarget.ambigiousFont != secondaryTranslatedObj2)
				{
					this.mTarget.ambigiousFont = secondaryTranslatedObj2;
				}
			}
			if (this.mInitializeAlignment)
			{
				this.mInitializeAlignment = false;
				this.mAlignment_LTR = (this.mAlignment_RTL = this.mTarget.alignment);
				if (LocalizationManager.IsRight2Left && this.mAlignment_RTL == NGUIText.Alignment.Right)
				{
					this.mAlignment_LTR = NGUIText.Alignment.Left;
				}
				if (!LocalizationManager.IsRight2Left && this.mAlignment_LTR == NGUIText.Alignment.Left)
				{
					this.mAlignment_RTL = NGUIText.Alignment.Right;
				}
			}
			UIInput uiinput = NGUITools.FindInParents<UIInput>(this.mTarget.gameObject);
			if (uiinput != null && uiinput.label == this.mTarget)
			{
				if (mainTranslation != null && uiinput.defaultText != mainTranslation)
				{
					if (cmp.CorrectAlignmentForRTL && (uiinput.label.alignment == NGUIText.Alignment.Left || uiinput.label.alignment == NGUIText.Alignment.Right))
					{
						uiinput.label.alignment = ((!LocalizationManager.IsRight2Left) ? this.mAlignment_LTR : this.mAlignment_RTL);
					}
					uiinput.defaultText = mainTranslation;
				}
			}
			else if (mainTranslation != null && this.mTarget.text != mainTranslation)
			{
				if (cmp.CorrectAlignmentForRTL && (this.mTarget.alignment == NGUIText.Alignment.Left || this.mTarget.alignment == NGUIText.Alignment.Right))
				{
					this.mTarget.alignment = ((!LocalizationManager.IsRight2Left) ? this.mAlignment_LTR : this.mAlignment_RTL);
				}
				if (((!this.mTarget.useFloatSpacing && (this.mTarget.spacingX != 0 || this.mTarget.spacingY != 0)) || (this.mTarget.useFloatSpacing && (this.mTarget.floatSpacingX != 0f || this.mTarget.floatSpacingY != 0f))) && cmp.GetComponent<NGUILabelSpacingHold>() == null)
				{
					cmp.gameObject.AddComponent<NGUILabelSpacingHold>();
				}
				this.mTarget.overflowMethod = UILabel.Overflow.ShrinkContent;
				this.mTarget.text = mainTranslation;
			}
		}

		private NGUIText.Alignment mAlignment_RTL = NGUIText.Alignment.Right;

		private NGUIText.Alignment mAlignment_LTR = NGUIText.Alignment.Left;

		private bool mAlignmentWasRTL;

		private bool mInitializeAlignment = true;
	}
}
