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

public class PlayerController : MonoBehaviour
{
    protected CharacterMovement _movement;
    protected CharacterLooking _looking;
    protected Interactor _interactor;
    protected Lifter _lifter;
    protected bool _freezed;

    private void Awake()
    {
        _movement = GetComponent<CharacterMovement>();
        _looking = GetComponent<CharacterLooking>();
        _interactor = GetComponent<Interactor>();
        _lifter = GetComponent<Lifter>();
    }

    public void Freeze()
    {
        _movement.SetInput(Vector3.zero);
        enabled = false;
    }

    public void UnFreeze()
    {
        enabled = true;
    }

    private void Update()
    {
        float x = Input.GetAxisRaw("Horizontal");
        float y = Input.GetAxisRaw("Vertical");

        Vector3 v = transform.forward * y + transform.right * x;
        if (v.magnitude > 1f)
            v.Normalize();
        _movement.SetInput(v);

        bool jump = Input.GetButtonDown("Jump");
        if (jump)
            _movement.Jump();

        float mx = Input.GetAxisRaw("Mouse X");
        float my = Input.GetAxisRaw("Mouse Y");

        _looking.LookX(mx);
        _looking.LookY(-my);

        bool interact = Input.GetKeyDown(KeyCode.E);
        if (interact)
            _interactor.OnInteract();

        bool m1 = Input.GetMouseButtonDown(0);
        bool m2 = Input.GetMouseButtonDown(1);
        bool drop = Input.GetKeyDown(KeyCode.G);
        Liftable liftable = _interactor.GetCandidate()?.GetComponent<Liftable>();
        if (m1) {
            if (liftable != null)
                _lifter.Lift(liftable);
        }
        if (m2)
        {
            _lifter.Place();
        }
        if (drop)
            _lifter.Drop();
    }
}
