using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
    public PlayerInputControl inputControl;
    public Vector2 inputDirection;

    private Rigidbody2D rb;
    private PhysicsCheck physicsCheck;
    private PlayerAnimation playerAnimation;

    [Header("基本参数")]
    public float speed;
    public float jumpForce;

    [Header("状态")]
    public bool isDead;
    public bool isAttack;

    [Header("受伤反弹参数")]
    public bool isHurt;
    public float hurtForce;//反弹的力

    [Header("物理材质")]
    public PhysicsMaterial2D Normal;
    public PhysicsMaterial2D Wall;
    private CapsuleCollider2D coll;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        physicsCheck = GetComponent<PhysicsCheck>();
        playerAnimation = GetComponent<PlayerAnimation>();
        coll = GetComponent<CapsuleCollider2D>();
        inputControl = new PlayerInputControl();
        //跳跃
        inputControl.GamePlayerInput.Jump.started += Jump;
        //攻击
        inputControl.GamePlayerInput.Attack.started += PlayerAttack;
        
    }

    

    private void OnEnable()
    {
        inputControl.Enable();
    }

    private void OnDisable()
    {
        inputControl.Disable();
    }

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        inputDirection = inputControl.GamePlayerInput.Move.ReadValue<Vector2>();
        CheckState();//检查切换光滑不光滑材质
    }

    private void FixedUpdate()
    {
        if(!isHurt && !isAttack)
        {
            Move();
        }
       
    }

    //移动
    public void Move()
    {
        
        rb.velocity = new Vector2(inputDirection.x*speed * Time.deltaTime, rb.velocity.y);
        //翻转
        int faceDir = (int)transform.localScale.x;
        if(inputDirection.x > 0 )
        {
            faceDir = 1;
        }
        if (inputDirection.x < 0)
        {
            faceDir = -1;
        }

        transform.localScale = new Vector3(faceDir, transform.localScale.y, transform.localScale.z);
    }

    //跳跃
    private void Jump(InputAction.CallbackContext context)
    {
        if(physicsCheck.isGround)
        {
            rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
        }
    }

    //攻击
    private void PlayerAttack(InputAction.CallbackContext context)
    {
        playerAnimation.PlayAttack();
        isAttack = true;
    }

    //受伤反弹
    public void GetHurt(Transform attacker)
    {
        isHurt = true;
        rb.velocity = Vector2.zero;
        Vector2 dir = new Vector2((transform.position.x - attacker.position.x),0).normalized;//归一取1，定方向

        rb.AddForce(dir*hurtForce, ForceMode2D.Impulse);
    }


    //死亡暂停一切动作
    public void PlayerDead()
    {
        isDead = true;
        inputControl.GamePlayerInput.Disable();
    }

    //切换材质
    public void CheckState()
    {
        coll.sharedMaterial = physicsCheck.isGround ? Normal : Wall;
    }

}
