using System.Collections;
using System.ComponentModel.DataAnnotations;

namespace SharedKernel.Attributes
{
    /*
     * 파일 개수 제한
     */
    [AttributeUsage(AttributeTargets.Property)]
    public class MaxFileCountAttribute : ValidationAttribute
    {
        private readonly int _maxCount;
        public MaxFileCountAttribute(int maxCount)
        {
            _maxCount = maxCount;
        }

        public override bool IsValid(object? value)
        {
            if (value is IEnumerable files)
            {
                return files.Cast<object>().Count() <= _maxCount;
            }

            return true;
        }

        public override string FormatErrorMessage(string name)
        {
            var template = string.IsNullOrWhiteSpace(ErrorMessage) ? "{0}은(는) 최대 {1}개까지 등록 가능합니다." : ErrorMessage;

            return string.Format(template, name, _maxCount);
        }
    }
}