context: using System;
  using System.Collections.Generic;
  using Server.Accounting;
  using Server.Items;
  using Server.Mobiles;
  
  namespace Server.Engines.VeteranRewards
  {
      public interface IRewardItem
      {
          bool IsRewardItem { get; set; }
      }
  
      public class RewardSystem
      {
  		public static bool Enabled = Config.Get("VetRewards.Enabled", true);
          public static bool SkillCapRewards = Config.Get("VetRewards.SkillCapRewards", true);
          public static int SkillCap = Config.Get("PlayerCaps.TotalSkillCap", 7000);
          public static int SkillCapBonus = Config.Get("VetRewards.SkillCapBonus", 200);
          public static int SkillCapBonusLevels = Config.Get("VetRewards.SkillCapBonusLevels", 4);
          public static float SkillCapBonusIncrement = SkillCapBonus / SkillCapBonusLevels;
          public static TimeSpan RewardInterval = Config.Get("VetRewards.RewardInterval", TimeSpan.FromDays(30.0d));
  		public static int StartingLevel = Config.Get("VetRewards.StartingLevel", 0);
  
  		private static RewardCategory[] m_Categories;
          private static RewardList[] m_Lists;
          public static RewardCategory[] Categories
          {
              get
              {
                  if (m_Categories == null)
                      SetupRewardTables();
  
                  return m_Categories;
              }
          }
          public static RewardList[] Lists
          {
              get
              {
                  if (m_Lists == null)
                      SetupRewardTables();
  
                  return m_Lists;
              }
          }
          public static bool HasAccess(Mobile mob, RewardCategory category)
          {
              List<RewardEntry> entries = category.Entries;
  
              for (int j = 0; j < entries.Count; ++j)
              {
                  //RewardEntry entry = entries[j];
                  if (RewardSystem.HasAccess(mob, entries[j]))
                  {
                      return true;
                  }
              }
              return false;
          }
  
          public static bool HasAccess(Mobile mob, RewardEntry entry)
          {
              if (Core.Expansion < entry.RequiredExpansion)
              {
                  return false;
              }
  
              TimeSpan ts;
              return HasAccess(mob, entry.List, out ts);
          }
  
          public static bool HasAccess(Mobile mob, RewardList list, out TimeSpan ts)
          {
              if (list == null)
              {
                  ts = TimeSpan.Zero;
                  return false;
              }
  
              Account acct = mob.Account as Account;
  
              if (acct == null)
              {
                  ts = TimeSpan.Zero;
                  return false;
              }
  
  			TimeSpan totalTime = (DateTime.UtcNow - acct.Created) + TimeSpan.FromDays(RewardInterval.TotalDays * StartingLevel);
  
  			ts = (list.Age - totalTime);
  
  
              if (ts <= TimeSpan.Zero)
                  return true;
  
              return false;
          }
  
          public static int GetRewardLevel(Mobile mob)
          {
              Account acct = mob.Account as Account;
  
              if (acct == null)
                  return 0;
  
              return GetRewardLevel(acct);
          }
  
          public static int GetRewardLevel(Account acct)
          {
              TimeSpan totalTime = (DateTime.UtcNow - acct.Created);
  
              int level = (int)(totalTime.TotalDays / RewardInterval.TotalDays);
  
              if (level < 0)
                  level = 0;
  
  			level += StartingLevel;
  
              return level;
          }
  
          public static bool HasHalfLevel(Mobile mob)
          {
              Account acct = mob.Account as Account;
  
              if (acct == null)
                  return false;
  
              return HasHalfLevel(acct);
          }
  
          public static bool HasHalfLevel(Account acct)
          {
              TimeSpan totalTime = (DateTime.UtcNow - acct.Created);
  
              Double level = (totalTime.TotalDays / RewardInterval.TotalDays);
  
              return level >= 0.5;
          }
  
          public static bool ConsumeRewardPoint(Mobile mob)
          {
              int cur, max;
  
              ComputeRewardInfo(mob, out cur, out max);
  
              if (cur >= max)
                  return false;
  
              Account acct = mob.Account as Account;
  
              if (acct == null)
                  return false;
  
              //if ( mob.AccessLevel < AccessLevel.GameMaster )
              acct.SetTag("numRewardsChosen", (cur + 1).ToString());
  
              return true;
          }
  
          public static void ComputeRewardInfo(Mobile mob, out int cur, out int max)
          {
              int level;
  
              ComputeRewardInfo(mob, out cur, out max, out level);
          }
  
          public static void ComputeRewardInfo(Mobile mob, out int cur, out int max, out int level)
          {
              Account acct = mob.Account as Account;
  
              if (acct == null)
              {
                  cur = max = level = 0;
                  return;
              }
  
              level = GetRewardLevel(acct);
  
              if (level == 0)
              {
                  cur = max = 0;
                  return;
              }
  
              string tag = acct.GetTag("numRewardsChosen");
  
              if (String.IsNullOrEmpty(tag))
                  cur = 0;
              else
                  cur = Utility.ToInt32(tag);
  
              if (level >= 6)
                  max = 9 + ((level - 6) * 2);
              else
                  max = 2 + level;
          }
  
          public static bool CheckIsUsableBy(Mobile from, Item item, object[] args)
          {
              if (from.AccessLevel > AccessLevel.GameMaster || UseableByAnyone(item.GetType()))
                  return true;
  
              if (m_Lists == null)
                  SetupRewardTables();
  
              Type type = item.GetType();
  
              for (int i = 0; i < m_Lists.Length; ++i)
              {
                  RewardList list = m_Lists[i];
                  RewardEntry[] entries = list.Entries;
                  TimeSpan ts;
  
                  for (int j = 0; j < entries.Length; ++j)
                  {
                      if (entries[j].ItemType == type)
                      {
  
gt:                         if (args == null && entries[j].Args.Length == 0)