using System;
using FishNet.Object;
using FishNet.Object.Synchronizing;
using IStalk.Data;
using UnityEngine;

namespace IStalk.Eye
{
    public class EyeFlashReceiver : NetworkBehaviour
    {
        private readonly SyncVar<float> _blindTimer = new(new SyncTypeSettings(0.5f));
        private readonly SyncVar<float> _teamFlashCooldownTimer = new(new SyncTypeSettings(1f));

        private EyeEyelidController _eyelid;
        private EyeLaserController _laser;
        private EyeScanAbility _scan;
        private EyeTrapPlacer _traps;
        private EyeIncarnationController _incarnation;

        private GameObject _flashOverlay;

        public bool IsBlinded => _blindTimer.Value > 0f;
        public float BlindTimeRemaining => _blindTimer.Value;
        public bool IsFlashOnCooldown => _teamFlashCooldownTimer.Value > 0f;

        public event Action OnFlashDodged;
        public event Action<float> OnFlashHit;

        public override void OnStartNetwork()
        {
            base.OnStartNetwork();
            _eyelid = GetComponent<EyeEyelidController>();
            _laser = GetComponent<EyeLaserController>();
            _scan = GetComponent<EyeScanAbility>();
            _traps = GetComponent<EyeTrapPlacer>();
            _incarnation = GetComponent<EyeIncarnationController>();
        }

        public override void OnStartClient()
        {
            base.OnStartClient();
            if (IsOwner)
                CreateFlashOverlay();
        }

        private void Update()
        {
            if (IsServerInitialized)
            {
                if (_blindTimer.Value > 0f)
                    _blindTimer.Value -= Time.deltaTime;
                if (_teamFlashCooldownTimer.Value > 0f)
                    _teamFlashCooldownTimer.Value -= Time.deltaTime;
            }

            if (IsOwner && _flashOverlay != null)
                _flashOverlay.SetActive(_blindTimer.Value > 0f);
        }

        [Server]
        public bool TryApplyFlash()
        {
            if (_teamFlashCooldownTimer.Value > 0f)
                return false;

            var rules = GlobalRules_SO.Instance;
            _teamFlashCooldownTimer.Value = rules.flashTeamCooldown;

            if (_eyelid != null && _eyelid.IsClosed)
            {
                FlashDodgedObservers();
                return false;
            }

            _blindTimer.Value = rules.flashDuration;
            FlashHitObservers(rules.flashDuration);
            return true;
        }

        [ObserversRpc]
        private void FlashDodgedObservers()
        {
            OnFlashDodged?.Invoke();
        }

        [ObserversRpc]
        private void FlashHitObservers(float duration)
        {
            OnFlashHit?.Invoke(duration);
        }

        private void CreateFlashOverlay()
        {
            var canvas = new GameObject("FlashOverlay");
            var canvasComp = canvas.AddComponent<Canvas>();
            canvasComp.renderMode = RenderMode.ScreenSpaceOverlay;
            canvasComp.sortingOrder = 998;

            var panel = new GameObject("WhitePanel");
            panel.transform.SetParent(canvas.transform, false);
            var img = panel.AddComponent<UnityEngine.UI.Image>();
            img.color = Color.white;
            var rect = panel.GetComponent<RectTransform>();
            rect.anchorMin = Vector2.zero;
            rect.anchorMax = Vector2.one;
            rect.offsetMin = Vector2.zero;
            rect.offsetMax = Vector2.zero;

            _flashOverlay = canvas;
            _flashOverlay.SetActive(false);
        }

        public override void OnStopClient()
        {
            base.OnStopClient();
            if (_flashOverlay != null)
                Destroy(_flashOverlay);
        }
    }
}
