/* Copyright (C) Itseez3D, Inc. - All Rights Reserved
* You may not use this file except in compliance with an authorized license
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* UNLESS REQUIRED BY APPLICABLE LAW OR AGREED BY ITSEEZ3D, INC. IN WRITING, SOFTWARE DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED
* See the License for the specific language governing permissions and limitations under the License.
* Written by Itseez3D, Inc. <support@avatarsdk.com>, October 2026
*/

using UnityEditor;
using UnityEngine;

namespace AvatarSDKMove.Editor
{
	internal static class CoinIcon
	{
		private const float IconToFontRatio = 1.6f;
		private const float DefaultIconSize = 20.0f;
		private const float IconGap = 4.0f;

		private static Texture2D texture;
		private static GUIStyle priceButtonStyle;

		private static Texture2D Texture
		{
			get
			{
				if (texture == null)
					texture = AssetDatabase.LoadAssetAtPath<Texture2D>(AssetDatabase.GUIDToAssetPath(AvatarSDKMoveSettings.CoinIconGuid));
				return texture;
			}
		}

		private static Color Tint
		{
			get
			{
				Color tint = EditorGUIUtility.isProSkin ? new Color(1.0f, 0.78f, 0.25f) : new Color(0.8f, 0.56f, 0.05f);
				if (!GUI.enabled)
					tint.a *= 0.5f;
				return tint;
			}
		}

		public static void Draw(Rect rect)
		{
			if (Texture != null)
				GUI.DrawTexture(rect, Texture, ScaleMode.ScaleToFit, true, 0.0f, Tint, 0.0f, 0.0f);
		}

		public static void LayoutAmount(int amount, GUIStyle style, float height)
		{
			var content = new GUIContent(amount.ToString());
			GUILayout.Label(content, style, GUILayout.ExpandWidth(false), GUILayout.Height(height));
			float textCenterY = GetTextCenterY(GUILayoutUtility.GetLastRect(), style, content);

			float size = Mathf.Min(GetIconSize(style), height);
			Rect rect = GUILayoutUtility.GetRect(size, height, GUILayout.Width(size), GUILayout.Height(height));
			Draw(new Rect(rect.x, textCenterY - size * 0.5f, size, size));
		}

		private static float GetTextCenterY(Rect rect, GUIStyle style, GUIContent content)
		{
			Rect contentRect = style.padding.Remove(rect);
			float textHeight = style.CalcHeight(content, contentRect.width) - style.padding.vertical;
			float offset = style.contentOffset.y;

			switch (style.alignment)
			{
				case TextAnchor.UpperLeft:
				case TextAnchor.UpperCenter:
				case TextAnchor.UpperRight:
					return contentRect.y + textHeight * 0.5f + offset;
				case TextAnchor.LowerLeft:
				case TextAnchor.LowerCenter:
				case TextAnchor.LowerRight:
					return contentRect.yMax - textHeight * 0.5f + offset;
				default:
					return contentRect.center.y + offset;
			}
		}

		public static bool LayoutPriceButton(string text, int price, params GUILayoutOption[] options)
		{
			if (price == 0)
				return GUILayout.Button(string.Format("{0} · Free", text), options);

			GUIStyle buttonStyle = GUI.skin.button;
			float size = GetIconSize(buttonStyle);
			if (priceButtonStyle == null || priceButtonStyle.padding.right != buttonStyle.padding.right + Mathf.RoundToInt(size + IconGap))
			{
				priceButtonStyle = new GUIStyle(buttonStyle);
				priceButtonStyle.padding.right += Mathf.RoundToInt(size + IconGap);
			}

			var content = new GUIContent(string.Format("{0} · {1}", text, price));
			Rect rect = GUILayoutUtility.GetRect(content, priceButtonStyle, options);
			bool isClicked = GUI.Button(rect, content, priceButtonStyle);

			Rect contentRect = priceButtonStyle.padding.Remove(rect);
			float textWidth = priceButtonStyle.CalcSize(content).x - priceButtonStyle.padding.horizontal;
			float iconX = contentRect.center.x + textWidth * 0.5f + IconGap;
			Draw(new Rect(iconX, rect.center.y - size * 0.5f, size, size));

			return isClicked;
		}

		private static float GetIconSize(GUIStyle style)
		{
			return style.fontSize > 0 ? Mathf.Round(style.fontSize * IconToFontRatio) : DefaultIconSize;
		}
	}
}
