using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using System.Text;
using System.IO;
using System.Xml;

namespace GhostArena6
{
    class DifficultySetting
    {
        public string Name;
        public short HealthDrain;
        public float GhostSpeed;
        public short GhostHealth;
        public float GhostSpawnTime;
        public short GhostScore;

        public DifficultySetting(XmlNode node)
        {
            Name = node.Attributes["name"].Value.ToString();
            HealthDrain = Convert.ToInt16(node.SelectSingleNode("descendant::HealthDrain").InnerText);
            GhostSpeed = Convert.ToSingle(node.SelectSingleNode("descendant::GhostSpeed").InnerText);
            GhostHealth = Convert.ToInt16(node.SelectSingleNode("descendant::GhostHealth").InnerText);
            GhostSpawnTime = Convert.ToSingle(node.SelectSingleNode("descendant::GhostSpawnTime").InnerText);
            GhostScore = Convert.ToInt16(node.SelectSingleNode("descendant::GhostScore").InnerText);
        }
    }

    public class DifficultySystem
    {
        private int _difficulty;
        private float _levelSpeed;

        private List<DifficultySetting> _settings;

        public DifficultySystem()
        {
            //read file
            XmlDocument doc = new XmlDocument();

            if (File.Exists(StorageContainer.TitleLocation + "\\DifficultySettings.xml"))
            {
                doc.Load(StorageContainer.TitleLocation + "\\DifficultySettings.xml");

                XmlNode node;

                node = doc.DocumentElement.ChildNodes[0];

                //set default level
                _difficulty = Convert.ToInt16(node.InnerText);

                node = doc.DocumentElement.ChildNodes[1];

                _settings = new List<DifficultySetting>();

                foreach (XmlNode child in node.ChildNodes)
                    _settings.Add(new DifficultySetting(child));
            }

            _levelSpeed = 0.0f;

        }

        public int HealthDrain
        {
            get { return _settings[_difficulty].HealthDrain; }
        }

        public float GhostSpeed
        {
            get { return _settings[_difficulty].GhostSpeed + _levelSpeed; }
        }

        public int GhostHealth
        {
            get { return _settings[_difficulty].GhostHealth; }
        }

        public float GhostSpawnTime
        {
            get { return _settings[_difficulty].GhostSpawnTime; }
        }

        public short GhostScore
        {
            get { return _settings[_difficulty].GhostScore; }
        }

        public void SetDifficulty(int difficulty)
        {
            _difficulty = difficulty;
        }
        
        public string[] GetDifficultyNames()
        {
            List<string>names = new List<string>();

            foreach (DifficultySetting setting in _settings)
                names.Add(setting.Name);

            return names.ToArray();
        }

        public void IncreaseGhostSpeed()
        {
            _levelSpeed += .2f;
        }
    }
}
