﻿using UnityEngine;

namespace JLGames.RocketDriver.Games.RpgMaterial.User
{
    public class UniqueIdGenerator : IUniqueIdGenerator
    {
        private int m_MaxUId;

        public int MaxUId => m_MaxUId;

        public override string ToString()
        {
            return $"Unique[MaxId={m_MaxUId}]";
        }

        public UniqueIdGenerator()
        {
            m_MaxUId = 0;
        }

        public UniqueIdGenerator(int maxUId)
        {
            m_MaxUId = maxUId;
        }

        public void UpdateMaxUId(int maxUId)
        {
            m_MaxUId = maxUId;
        }

        public void UpdateMaxUId(IUserMaterial[] iums)
        {
            var maxId = 0;
            foreach (var ium in iums)
            {
                maxId = Mathf.Max(ium.UId, maxId);
            }

            m_MaxUId = maxId;
        }

        public int GenNextUId()
        {
            var nextUId = m_MaxUId + 1;
            m_MaxUId = nextUId;
            return nextUId;
        }
    }
}