using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using UnityEngine.UI;

public class SlowMotionController : MonoBehaviour
{
    public Transform GroundCheck;
    public Transform Character;
    public Transform Target;
    public Button JumpBtn;
    public Transform AttackBtn;

    public PostProcessVolume PostProcess;
    private ChromaticAberration _chromaticAberration;
    //
    private Vector3 _orgCharacterPos;
    
    private float _checkRadius = 0.2f;
    private int _layerMask;
    private readonly float GRAVITY = -9.81f * 3;
    private bool _isGrounded;
    private float _jumpHeight = 0f;
    private Vector2 _moveDirection = Vector2.zero;
    private float _flashMoveSpeed = 0f;
    private Vector3 _moveVelocity = Vector3.zero;
    
    private Animator _animator;
    private CharacterController _characterController;
    //
    private bool _isSlowMotion = false;
    private bool _isPressAttack = false;
    private bool _isArrived = false;
    private bool _isReset = false;
    //
    private float _timeScale = 0.1f;
    private int Hash_IsAttack;
    private int Hash_AttackSpeed;
    private int Hash_Reset;

    private float _attackTotalTime;
    void Start()
    {
        _orgCharacterPos  = Character.position;
        
        _animator = Character.GetComponent<Animator>();
        _characterController = Character.GetComponent<CharacterController>();
        _layerMask = LayerMask.GetMask("Default");

        Hash_IsAttack = Animator.StringToHash("isAttack");
        Hash_AttackSpeed = Animator.StringToHash("attackSpeed");
        Hash_Reset = Animator.StringToHash("reset");
        //
        AttackBtn.gameObject.SetActive(false);
        
        PostProcess.profile.TryGetSettings(out _chromaticAberration);
    }

    void Update()
    {
        _isGrounded = Physics.CheckSphere(GroundCheck.position, _checkRadius, _layerMask);
        //
        if (Input.GetKeyDown(KeyCode.Space) && _isGrounded)
        {
            _jumpHeight = 1f;
            var y = Mathf.Sqrt(-2 * GRAVITY * _jumpHeight);
            _moveVelocity.y = y;

            _isSlowMotion = true;
            _chromaticAberration.enabled.value = true;
        }
        //
        if (_isSlowMotion)
        {
            Time.timeScale = _timeScale;

            if (Input.GetKeyDown(KeyCode.A))
            {
                _isPressAttack = true;
            }
            
            AttackBtn.gameObject.SetActive(_isSlowMotion && !_isPressAttack);
        }
        
        if (_isGrounded)
        {
            if (_moveVelocity.y < -2f)
            {
                _moveVelocity.y = -2f;
            }

            if (_isPressAttack)
            {
                if (!_isArrived)
                {
                    //快速移动到目录前
                    var targetPos = Target.position + Target.forward.normalized * 2f;
                    var dir = (targetPos - Character.position).normalized;
                    _moveDirection = new Vector2(dir.x, dir.z);
                    _flashMoveSpeed = 10f;
                    if (Vector3.Distance(targetPos, Character.position) <= _flashMoveSpeed * Time.unscaledDeltaTime)
                    {
                        Character.position = targetPos;
                        _isArrived = true;
                        _attackTotalTime = 0;
                        //
                        return;
                    }
                }
                else
                {
                    //开始攻击
                    _moveDirection = Vector2.zero;
                    _flashMoveSpeed = 0f;
                    //
                    _animator.SetBool(Hash_IsAttack,true);
                    _animator.SetFloat(Hash_AttackSpeed, 1 / _timeScale);
                    
                    _attackTotalTime += Time.unscaledDeltaTime;
                    if (_attackTotalTime > 3f)
                    {
                        Reset();
                    }
                }
            }
        }
        var moveVelocity = _moveDirection.normalized * _flashMoveSpeed;
        _moveVelocity.x = moveVelocity.x;
        _moveVelocity.z = moveVelocity.y;
        //计算重力加速度和移动
        var targetDeltaTime = _isPressAttack ? Time.unscaledDeltaTime : Time.deltaTime;
        _moveVelocity.y += GRAVITY * targetDeltaTime;
        _characterController.Move(_moveVelocity * targetDeltaTime);
    }

    private void Reset()
    {
        _isReset = true;
        
        _isSlowMotion  = false;
        _isPressAttack = false;
        _isArrived  = false;
        
        _animator.SetBool(Hash_IsAttack,false);
        _animator.SetFloat(Hash_AttackSpeed, 1);
        _animator.SetTrigger(Hash_Reset);

        _attackTotalTime = 0f;

        Time.timeScale = 1f;
        _characterController.enabled = false;
        Character.transform.position = _orgCharacterPos;
        
        _chromaticAberration.enabled.value = false;
    }

    private void LateUpdate()
    {
        if (_isReset)
        {
            _isReset = false;
            _characterController.enabled = true;
        }
    }
}
