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

namespace IStalk.Eye
{
    public class EyeTypeController : NetworkBehaviour
    {
        private readonly SyncVar<string> _eyeTypeId = new();

        private EyeType_SO _eyeType;
        private EyeLaserController _laser;
        private EyeScanAbility _scan;
        private EyeTrapPlacer _traps;
        private EyeIncarnationController _incarnation;
        private EyeEyelidController _eyelid;
        private DreadEconomy _dread;

        public EyeType_SO CurrentType => _eyeType;

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

            _eyeTypeId.OnChange += OnTypeIdChanged;
        }

        public override void OnStartServer()
        {
            base.OnStartServer();
            if (!string.IsNullOrEmpty(_eyeTypeId.Value))
                ApplyType();
        }

        [Server]
        public void SetEyeType(string typeId)
        {
            _eyeTypeId.Value = typeId;
            ApplyType();
        }

        private void OnTypeIdChanged(string prev, string next, bool asServer)
        {
            if (!asServer)
                ApplyType();
        }

        private void ApplyType()
        {
            _eyeType = ItemDatabase.Instance.GetEyeType(_eyeTypeId.Value);
            if (_eyeType == null)
            {
                Debug.LogError($"[EyeTypeController] Unknown eye type: {_eyeTypeId.Value}");
                return;
            }

            if (_laser != null)
            {
                bool hasLaser = _eyeType.HasAbility(EyeAbility.Laser);
                _laser.enabled = hasLaser;
                if (hasLaser)
                    _laser.ApplyTypeModifiers(_eyeType.laserCooldownMultiplier, _eyeType.laserRadiusMultiplier);
            }

            if (_scan != null)
            {
                bool hasScan = _eyeType.HasAbility(EyeAbility.Scan);
                _scan.enabled = hasScan;
                if (hasScan)
                    _scan.ApplyTypeModifiers(_eyeType.scanCooldownMultiplier, _eyeType.scanRadiusMultiplier);
            }

            if (_traps != null)
            {
                bool hasTraps = _eyeType.HasAbility(EyeAbility.Traps);
                _traps.enabled = hasTraps;
                if (hasTraps)
                    _traps.ApplyTypeModifiers(_eyeType.trapCostMultiplier, _eyeType.trapMaxActiveBonus);
            }

            if (_incarnation != null)
                _incarnation.enabled = _eyeType.HasAbility(EyeAbility.Incarnation);

            if (_eyelid != null)
                _eyelid.enabled = _eyeType.hasEyelids;

            if (_dread != null && IsServerInitialized)
            {
                if (_eyeType.dreadStartOverride >= 0)
                    _dread.ApplyStartOverride(_eyeType.dreadStartOverride);
                if (_eyeType.dreadRegenOverride >= 0)
                    _dread.ApplyRegenOverride(_eyeType.dreadRegenOverride);
            }
        }
    }
}
