using HISP.Server;
using System;
using System.Collections.Generic;

namespace HISP.Player
{
    public class Award
    {
        public struct AwardEntry
        {
            public int Id;
            public int Sort;
            public string Title;
            public int IconId;
            public int MoneyBonus;
            public string CompletionText;
            public string Description;
        }

        public static AwardEntry[] GlobalAwardList;

        public static AwardEntry GetAwardById(int id)
        {
            try
            {
                AwardEntry award = GlobalAwardList[id - 1];
                if (award.Id == id)
                    return award;
            }
            catch (Exception) { };

            foreach(AwardEntry award in GlobalAwardList)
            {
                if (award.Id == id)
                    return award;
            }

            throw new KeyNotFoundException("Award ID " + id + " Does not exist.");
        }


        private List<AwardEntry> awardsEarned;
        private User baseUser;
        public AwardEntry[] AwardsEarned
        {
            get
            {
                return awardsEarned.ToArray();
            }
        }

        public bool HasAward(AwardEntry award)
        {
            foreach(AwardEntry awardEntry in AwardsEarned)
            {
                if (awardEntry.Id == award.Id)
                    return true;
            }
            return false;
        }

        public void AddAward(AwardEntry award,bool addToDatabase=true)
        {
            if (HasAward(award))
                return;

            if (addToDatabase)
            {
                Database.AddAward(baseUser.Id, award.Id);

                baseUser.Money += award.MoneyBonus;

                byte[] chatPacket = PacketBuilder.CreateChat(award.CompletionText, PacketBuilder.CHAT_BOTTOM_RIGHT);
                baseUser.LoggedinClient.SendPacket(chatPacket);
            }
                

            awardsEarned.Add(award);
        }

        public Award(User user)
        {
            baseUser = user;
            int[] awards = Database.GetAwards(user.Id);
            awardsEarned = new List<AwardEntry>();

            foreach (int awardid in awards)
            {
                AddAward(GetAwardById(awardid), false);
            }

        }



    }
}
