﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Input;
using System.Threading;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.GamerServices;

namespace Sims2Online.UI.Elements
{
    class UIButton : UIControl
    {
        MouseState oldMouseState;
        KeyboardState oldKBState;
        Vector2 selected;
        Texture2D backtex;

        public string text;
        public Color color = Color.White;
        public event EventHandler onClicked;
        public event EventHandler onHover;
        public event EventHandler onMouseLeft;
        public float scale;

        public override void Init(SpriteBatch spb)
        {
            selected = Vector2.Zero;
            using (var stream1 = new FileStream("./content/colour.png", FileMode.Open))
            {
                backtex = Texture2D.FromStream(spb.GraphicsDevice, stream1);
            }
        }
        public override void Update(SpriteBatch spb, S2OGame game)
        {
            spb.Draw(backtex, rect, Color.Black * 0.25f);
            var currentMouseState = Mouse.GetState();
            var currentKBState = Keyboard.GetState();
            if (rect.Contains(new Point(currentMouseState.X, currentMouseState.Y)) && currentMouseState.LeftButton == ButtonState.Pressed)
            {
                this.onClicked?.Invoke(this, EventArgs.Empty);
            }
            else if (rect.Contains(new Point(currentMouseState.X, currentMouseState.Y)))
            {
                this.onHover?.Invoke(this, EventArgs.Empty);
            }
            else
            {
                this.onMouseLeft?.Invoke(this, EventArgs.Empty);
            }
            spb.DrawString(S2OGame.Font1, text, new Vector2(rect.X + ((rect.Width / 2) - ((S2OGame.Font1.MeasureString(text) * scale).X / 2)), rect.Y + ((rect.Height / 2) - ((S2OGame.Font1.MeasureString(text) * scale).Y / 2))), color, 0, Vector2.Zero, scale, SpriteEffects.None, 0.5f);
            oldMouseState = currentMouseState;
            oldKBState = currentKBState;
        }

    }
}
