using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace GhostArena6
{
    //this enum must be synched up with the order in the XML file
    public enum Achievements
    {
        Dodger,
        Unstoppable,
        QuickDead,
        EagleEye
    }

    [Serializable]
    public class Achievement
    {
        public int ID;
        public string Name;
        public int Points;
        public string Description;
    }

    [Serializable]
    public class PlayerAchievement
    {
        public int ID;
        public DateTime DateUnlocked;
    }

    //used in AchievementScreen
    public class AchievementDisplay
    {
        public Texture2D Icon; 
        public string Name;
        public string Points;
        public string DateUnlocked;
    }
    
    public class AchievementManager
    {
        private List<PlayerAchievement> _playerAchievements;
        private List<Achievement> _achievements;

        private Game _game;

        public AchievementManager(Game game)
        {
            _achievements = new List<Achievement>();

            //load achievements
            FileStream fs = new FileStream(StorageContainer.TitleLocation + "\\achievements.xml", FileMode.Open);
            XmlSerializer serializer = new XmlSerializer(typeof(List<Achievement>));

            _achievements=(List<Achievement>)serializer.Deserialize(fs);

            fs.Close();

            //load player achievements
            if (File.Exists(StorageContainer.TitleLocation + "\\playerachievements.xml"))
            {
                serializer = new XmlSerializer(typeof(List<PlayerAchievement>));
                fs = new FileStream(StorageContainer.TitleLocation + "\\playerachievements.xml", FileMode.Open);

                _playerAchievements = (List<PlayerAchievement>)serializer.Deserialize(fs);
            }
            
            _game = game;
        }

        public void Save()
        {
            //achievements
            XmlSerializer serializer = new XmlSerializer(typeof(PlayerAchievement));
            FileStream fs = new FileStream(StorageContainer.TitleLocation + "\\playerachievements.xml", FileMode.Create);

            serializer.Serialize(fs, _playerAchievements);

            fs.Close();
        }

        public bool PlayerHasAchievements()
        {
            if (_playerAchievements != null)
                return (_playerAchievements.Count > 0);
            else
                return false;
        }

        public int PlayerAchievementCount()
        {
            if (_playerAchievements != null)
                return _playerAchievements.Count;
            else
                return 0;
        }

        public List<AchievementDisplay> Achievements()
        {
            List<AchievementDisplay> achievements = new List<AchievementDisplay>();

            AchievementDisplay display;

            ContentManager content = new ContentManager(_game.Services);

            foreach (Achievement achievement in _achievements)
            {
                display = new AchievementDisplay();

                display.Icon = content.Load<Texture2D>(StorageContainer.TitleLocation + "\\Content\\achievement" + achievement.ID.ToString());
                display.Name = achievement.Name;
                display.Points = achievement.Points.ToString();
                display.DateUnlocked = GetDateUnlocked(achievement.ID);

                achievements.Add(display);
            }

            return achievements;
        }

        private string GetDateUnlocked(int id)
        {
            if (_playerAchievements != null)
            {
                foreach (PlayerAchievement achievement in _playerAchievements)
                    if (achievement.ID == id)
                        return achievement.DateUnlocked.ToString();
            }

            return "";
        }

        public void AddAchievement(Achievements type)
        {
            PlayerAchievement achievement = new PlayerAchievement();

            achievement.DateUnlocked = DateTime.Now;
            achievement.ID = _achievements[(int)type].ID;

            if (_playerAchievements == null)
                _playerAchievements = new List<PlayerAchievement>();

            _playerAchievements.Add(achievement);
        }

        public bool IsAchievementEarned(Achievements type)
        {
            if (_playerAchievements != null)
            {
                for (int i = 0; i < _playerAchievements.Count; i++)
                    if (_playerAchievements[i].ID == _achievements[(int)type].ID)
                        return true;
            }

            return false;

        }
    }
}
