using System;
using iiie.CacheControl.Attributes;
using iiie.CacheControl.DBO;

namespace iiie.CacheControl.Business.Attributes
{
    /// <summary>
    /// A basic cache control based on time
    /// </summary>
    public class TimeCacheControlAttribute : CacheControlAttribute
    {
        /// <summary>
        /// The number of seconds the cache is valid
        /// </summary>
        protected int Seconds { get; set; }

        /// <summary>
        /// Contruct a cache control based on time
        /// </summary>
        public TimeCacheControlAttribute(int seconds, bool excludeGet = false, bool excludePost = false)
        {
            Seconds = seconds;
            ExcludePostFromCacheKey = excludePost;
            ExcludeQueryStringFromCacheKey = excludeGet;
        }

        /// <summary>
        /// Check if data has been cached for less than Senconds seconds
        /// </summary>
        /// <param name="data">The cached data</param>
        /// <returns>True if cache is still valid</returns>
        protected override bool IsValid(CacheDbo data)
        {
            return data.Date.AddSeconds(Seconds) >= DateTime.Now;
        }
    }
}