using System;
using Entitas;
using UnityEngine;
using Game;
/// 
/// 交互系统 带InteractComp的是可交互物 类型为门、宝箱、怪物等
/// 
public class InteractSystem : IExecuteSystem, IInitializeSystem
{
    private IGroup _entities;
    private const float SearchWaitTimeMax = 0.2f;
    private float _searchWaitTimeLeft = 0;
    public void Initialize()
    {
        _entities = Util.GetGroup(GameMatcher.Interact);
        EventManager.Instance.AddEvent(EEvent.PlayerInput, OnPlayerAction);
    }
    public void Execute()
    {
        if (!Util.IsControl())
        {
            return;
        }
        var master = Util.GetMaster();
        UpdateInteract(master);
    }
    public void UpdateInteract(GameEntity entity)
    {
        //TODO 瞬间触发多次的问题
        var combo = entity.combo;
        var target = combo.TargetInteract;
        _searchWaitTimeLeft -= Time.deltaTime;
        if (_searchWaitTimeLeft <= 0)
        {
            var newTarget = FindTarget(entity);
            if (newTarget != target.Value)
            {
                if (target.Value > 0)
                {
                    Util.SetIsTarget(target.Value, false);
                }
                if (newTarget > 0)
                {
                    Util.SetIsTarget(newTarget, true);
                }
            }
            target.Value = newTarget;
            _searchWaitTimeLeft = SearchWaitTimeMax;
        }
    }
    private int FindTarget(GameEntity entity)
    {
        var combo = entity.combo;
        var newTarget = 0;
        var distanceMin = float.MaxValue;
        foreach (var target in _entities)
        {
            if (!target.interact.IsActive.Value)
            {
                continue;
            }
            var entityID = target.ID();
            var distance = Vector3.Distance(entity.Pos(), target.Pos());
            if (distance > GameConst.InteractRange)
            {
                continue;
            }
            if (distance < distanceMin)
            {
                newTarget = entityID;
                distanceMin = distance;
            }
        }
        return newTarget;
    }
    private void Interact(GameEntity entity)
    {
        var interact = entity.interact;
        switch (interact.InteractType)
        {
            case EInteractType.Door:
                Util.EnterLevel(interact.TargetId);
                break;
            case EInteractType.Npc:
                Util.StartDrama(interact.TargetId);
                break;
            case EInteractType.Monster:
                Util.Print("处决", entity.ID());
                break;
            case EInteractType.Bless:
                Util.OpenBlessUI((EElementType)Enum.Parse(typeof(EElementType), interact.TargetId, true));
                Util.DestroyEntity(entity);
                break;
        }
    }
    private void OnPlayerAction(PPlayerInput info)
    {
        if (info.Action == EKeyActionType.Press && info.Key == EFunctionKey.Interact)
        {
            var master = Util.GetMaster();
            var combo = master.combo;
            var target = combo.TargetInteract;
            if (target.Value > 0)
            {
                Interact(Util.GetEntity(target.Value));
                target.Value = 0;
            }
        }
    }
}