using System;
using EFT;
using EFT.InventoryLogic;
using EscapeFromTarkovCheat.Data;
using EscapeFromTarkovCheat.Utils;
using UnityEngine;
using static Systems.Effects.Effects;

namespace EscapeFromTarkovCheat.Feauters
{
    class Aimbot : MonoBehaviour
    {
        public void Update()
        {
            if (Settings.NoRecoil)
                NoRecoil();
            if (Main.GameWorld != null && Settings.Aimbot && Input.GetKey(Settings.AimbotKey))
                this.Aim();
        }

        private void Aim()
        {
            Vector3 target = Vector3.zero;
            float distanceOfTarget = 9999f;
            foreach (GamePlayer gamePlayer in Main.GamePlayers)
            {
                if (gamePlayer == null)
                    // 未开局 : Not loaded
                    continue;
                if (Main.LocalPlayer.HandsController == null)
                    // 卡手 : Invalid hand
                    continue;
                Weapon weapon = Main.LocalPlayer.HandsController.Item as Weapon;
                if (weapon == null)
                    // 无武器 : No weapon
                    continue;
                AmmoTemplate currentAmmoTemplate = weapon.CurrentAmmoTemplate;
                if (currentAmmoTemplate == null)
                    // 无弹药 : Out of ammo
                    continue;
                Vector3 destination = GameUtils.GetBonePosByEID(gamePlayer.Player, BoneType.HumanHead);
                float distance = Vector3.Distance(Main.MainCamera.transform.position, gamePlayer.Player.Transform.position);
                if (distance > Settings.AimbotRange)
                    // 大于瞄准范围 : Too far
                    continue;
                if (destination == Vector3.zero)
                    // 无法找到头部 : Head not found
                    continue;
                if (Aimbot.CalculateInFov(destination) > Settings.AimbotFOV) 
                    // 不在瞄准圈内 : Not in fov
                    continue;
                if (Settings.VisibleOnly && !GameUtils.IsPlayerVisible(gamePlayer.Player))
                    // 开启仅瞄准可见时不可见 : Not visible while Visible Only is toggled on
                    continue;
                
                if (distanceOfTarget > distance) // 寻找最近 : Aim at the nearest
                {
                    distanceOfTarget = distance;
                    float timeToHit = distance / currentAmmoTemplate.InitialSpeed; // 子弹飞行时间 : Time to hit the target
                    destination.x += gamePlayer.Player.Velocity.x * timeToHit; // 速度时间预判，下同 : Add some drifts to the target for hitting moving targets
                    destination.y += gamePlayer.Player.Velocity.y * timeToHit;
                    target = destination; // 建立瞄准点 : Construct the final aiming point
                }
            }
            if (target != Vector3.zero)
            {
                Aimbot.AimAtPos(target);
            }
        }
        private void NoRecoil()
        {
            if (Main.LocalPlayer == null)
                return;
            if (Main.LocalPlayer.ProceduralWeaponAnimation == null)
                return;
            var effect = Main.LocalPlayer.ProceduralWeaponAnimation.Shootingg?.CurrentRecoilEffect;
            if (effect == null)
                return;
            effect.CameraRotationRecoilEffect.Intensity = 0f;
            effect.HandPositionRecoilEffect.Intensity = 0f;
            effect.HandRotationRecoilEffect.Intensity = 0f;
        }

        public void OnGUI()
        {
            if (Settings.AimbotDrawFOV)
            {
                Render.DrawCircle(new Vector2((float)Screen.width / 2f, (float)Screen.height / 2f), Screen.width * Settings.AimbotFOV / 75, 64, Color.red, true, 1);
            }
        }

        public static float CalculateInFov(Vector3 position1)
        {
            Vector3 position2 = Main.MainCamera.transform.position;
            Vector3 forward = Main.MainCamera.transform.forward;
            Vector3 normalized = (position1 - position2).normalized;
            return Mathf.Acos(Mathf.Clamp(Vector3.Dot(forward, normalized), -1f, 1f)) * 57.29578f;
        }

        public static void AimAtPos(Vector3 position)
        {
            if (Settings.SilentAim)
            {
                Main.LocalPlayer.ProceduralWeaponAnimation.ShotNeedsFovAdjustments = false;
                Vector3 normalizedPosition = (position - Main.LocalPlayer.Fireport.position).normalized;
                Vector3 shotDirection = Main.LocalPlayer.Fireport.InverseTransformVector(normalizedPosition);
                Main.LocalPlayer.ProceduralWeaponAnimation._shotDirection = shotDirection;
            }
            else
            {
                Vector3 b = Main.LocalPlayer.Fireport.position - Main.LocalPlayer.Fireport.up * 1f;
                Vector3 eulerAngles = Quaternion.LookRotation((position - b).normalized).eulerAngles;
                if (eulerAngles.x > 180f)
                {
                    eulerAngles.x -= 360f;
                }
                Main.LocalPlayer.MovementContext.Rotation = new Vector2(eulerAngles.y, eulerAngles.x);
            }
        }
    }
}
