using LeMangeurDEnfants.Core.Scenes;
using Microsoft.Xna.Framework;
using MonoGame.FourEngine;
using MonoGame.FourEngine.Extensions;
using System;
using System.Linq;

namespace LeMangeurDEnfants.Core.Entities
{
    public class Win : BaseObject
    {
        public override string FileName => Resources.Sprites.Gate;
        public override Point Size => new Point(16);
        
        public int Kind
        {
            get => _kind;
            set
            {
                _kind = value;
                Frame = new Point(Frame.X, value);
            }
        }
        private int _kind;

        private bool _disabled;

        public override void OnUpdate(UpdateEventArgs args)
        {
            base.OnUpdate(args);
            Visibility = false;
            SpriteComponent.Layer = 1;
            if (Scene is GameScene gameScene && gameScene.BigBoss != null)
            {
                CheckCollision(gameScene);
            }
        }

        private void CheckCollision(GameScene scene)
        {
            if (!_disabled && Collide(scene.BigBoss.Position) && !scene.Entities.Filter<Prisoner>().Any())
            {
                _disabled = true;
                scene.Win();
            }
        }

        public override bool Collide(Point center)
        {
            var diff = Position - center;
            return Math.Abs(diff.X) < Size.X / 2 && Math.Abs(diff.Y) < Size.Y / 2;
        }
    }
}