#region Using directives

using System;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
#endregion

namespace PunkMan.Game
{
    /// <summary>
    /// Player representation
    /// </summary>
    public class Player : Entity
    {
        /// <summary>
        /// map object
        /// </summary>
        protected Map map;
        /// <summary>
        /// map object
        /// </summary>
        /// <value></value>
        public Map Map
        {
            set { map = value; }
        }
        /// <summary>
        /// alcohol in blood
        /// </summary>
        protected int c2h5oh = 0;
        /// <summary>
        /// alcohol in blood
        /// </summary>
        /// <value>bloody alcohol</value>
        public int C2H5OH
        {
            get { return c2h5oh; }
        }
        /// <summary>
        /// delirium value of alcohol in blood
        /// </summary>
        protected readonly int __delirium = 100;
        /// <summary>
        /// is delirium
        /// </summary>
        public bool delirium = false;
        /// <summary>
        /// is delirium?
        /// </summary>
        /// <value></value>
        public bool Delirium
        {
            get { return c2h5oh >= __delirium; }
        }

        /// <summary>
        /// player's score
        /// </summary>
        protected int score = 0;
        /// <summary>
        /// player's score
        /// </summary>
        /// <value></value>
        public int Score
        {
            get { return score; }
            set { score = value; }
        }

        /// <summary>
        /// game owner object
        /// </summary>
        private Game owner;
        /// <summary>
        /// is player alive?
        /// </summary>
        /// <value></value>
        public bool Alive
        {
            get { return lives > 0; }
        }

        /// <summary>
        /// number of lifes
        /// </summary>
        protected int lives;
        /// <summary>
        /// number of lifes
        /// </summary>
        /// <value></value>
        public int Lives
        {
            get { return lives; }
        }
        /// <summary>
        /// call when player killed by monster
        /// </summary>
        public void Killed()
        {
            if (lives > 0) lives--;
            Detox();
        }

        /// <summary>
        /// particle system
        /// </summary>
        private ParticleSystem blitkomet;


        /// <summary>
        /// Ooo mighty constructor
        /// </summary>
        /// <param name="owner">game owner object</param>
        /// <param name="dev">Direct3D device</param>
        /// <param name="fileName">entity model file name</param>
        public Player(Game owner, Device dev, string fileName): base(dev,fileName)
        {
            lives = 5;
            this.owner = owner;
            OnItemPicked += new Game.GameEventHandler(OnItemPickedHandler);
            blitkomet = new ParticleSystem(dev, 10000, position, direction);
        }

        /// <summary>
        /// Detoxicate player
        /// </summary>
        public void Detox()
        {
            c2h5oh = 0;
        }

        /// <summary>
        /// Item picked event
        /// </summary>
        public event Game.GameEventHandler OnItemPicked;

        /// <summary>
        /// the Time in bloody detox will come
        /// </summary>
        private float detoxTime = -1;
        /// <summary>
        /// update player state
        /// </summary>
        protected override void Update()
        {
            MapCell c = map[mapPosition];
            if (c.Entity == MapCell.EntityType.Absinth || c.Entity == MapCell.EntityType.Rum)
            {
                if (OnItemPicked != null)
                    OnItemPicked(this, mapPosition.X, mapPosition.Y);
            }
            if (c.Entity == MapCell.EntityType.End && !map.HasItem())
                owner.EndLevel();
            if (detoxTime > 0 && detoxTime < Game.Time())
            {
                Detox();
                detoxTime = -1;
            }
            base.Update();
        }


        private float lastSoundPlay = 0;
        /// <summary>
        /// draw player
        /// </summary>
        public override void Render()
        {
            if (!Alive) return;
            blitkomet.Position = position;
            if (moving)
                blitkomet.Direction = new Vector3(0, 0, 0);
            else
                blitkomet.Direction = direction;
            base.Render();
            if (Delirium) blitkomet.Render();
            if (Delirium && lastSoundPlay > 0 && Game.Time() < lastSoundPlay + 1)
                owner.SFXPlay(SoundFX.SFX.Blinker);
            lastSoundPlay = Game.Time();
            
        }


        /// <summary>
        /// On Item Picked event handler 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="x">x map position</param>
        /// <param name="y">y map position</param>
        protected void OnItemPickedHandler(object sender, int x, int y)
        {
            //Game.LogFile.WriteLine("Item Picked happend at {0},{1}", x, y);

            MapCell.EntityType et = map.Data[x, y].Entity;
            if (detoxTime > 0)
            {
            }
            else
            {
                c2h5oh += (et == MapCell.EntityType.Rum) ? 10 : 40;
                if (Delirium) detoxTime = Game.Time() + 10f;
                blitkomet = new ParticleSystem(dev, 1000, position, direction);
            }
            score += (et == MapCell.EntityType.Rum) ? 10 : 40;
            owner.SFXPlay((et == MapCell.EntityType.Rum) ? SoundFX.SFX.Rum : SoundFX.SFX.Absinth);
            map.Data[x, y].Entity = MapCell.EntityType.None;
        }

        /// <summary>
        /// Called when player killed monster
        /// </summary>
        public void PlayerKillMonster() { score += 150; }

    }
}
