using System;
using UnityEngine;

namespace AlicizaX.UI
{
    [Serializable]
    public class AlignableLinearLayoutManager : LinearLayoutManager
    {
        [SerializeField] private float alignmentCount = 0f; // 对齐比例 (0=顶部, 1=底部, 0.5=居中)

        public override Vector2 CalculatePosition(int index)
        {
            float position;

            if (direction == Direction.Vertical)
            {
                position = index * (lineHeight + spacing.y) - ScrollPosition ;
                return new Vector2(0, position + padding.y);
            }

            position = index * (lineHeight + spacing.x) - ScrollPosition ;
            var a = new Vector2(position + padding.x, 0);
            return a;
        }

        public override float IndexToPosition(int index)
        {
            if (index < 0 || index >= adapter.GetItemCount()) return 0;

            float len, viewLength, position;

            if (direction == Direction.Vertical)
            {
                len = index * (lineHeight + spacing.y)  - ((lineHeight + spacing.y) * alignmentCount);
                viewLength = viewportSize.y;
                position = len + viewLength > contentSize.y ? contentSize.y - viewportSize.y : len;
            }
            else
            {
                len = index * (lineHeight + spacing.x);
                viewLength = viewportSize.x;
                position = len + viewLength > contentSize.x ? contentSize.x - viewportSize.x : len;
            }

            return position;
        }
    }
}
