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

public class WaiterMovement : MonoBehaviour
{
    private JointMotor2D leftArmMotor;
    private JointMotor2D rightArmMotor;
    private SliderJoint2D leftArm;
    private SliderJoint2D rightArm;
    private Rigidbody2D body;
    private Animator anim;

    private Transform food;

    private string leftString;
    private string rightString;
    private string triggerString;

    public SpriteRenderer bowTie;

    public bool foodOnPlate = false;

    public float armSpeed;
    public float legForce;
    private int player;
    private float xPos;

    // Use this for initialization
    void Start ()
    {
        leftArm = GetComponents<SliderJoint2D>()[0];
        rightArm = GetComponents<SliderJoint2D>()[1];

        leftArmMotor = leftArm.motor;
        rightArmMotor = rightArm.motor;

        body = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();

        xPos = transform.position.x;
        food = FindObjectOfType<Food>().GetComponent<Transform>();

    }

    public void SetPlayerBelonging(int player)
    {
        switch(player)
        {
            case 0:
                leftString = "LeftArm1";
                rightString = "RightArm1";
                triggerString = "Triggers1";
                break;
            case 1:
                leftString = "LeftArm2";
                rightString = "RightArm2";
                triggerString = "Triggers2";
                break;
            case 2:
                leftString = "LeftArm3";
                rightString = "RightArm3";
                triggerString = "Triggers3";
                break;
        }
    }

    // Update is called once per frame
    void Update ()
    {
        leftArmMotor.motorSpeed = Input.GetAxis(leftString) * armSpeed;
        rightArmMotor.motorSpeed = Input.GetAxis(rightString) * armSpeed;

        leftArm.motor = leftArmMotor;
        rightArm.motor = rightArmMotor;

        body.AddForce(new Vector2(Input.GetAxis(triggerString) * legForce, 0.0f));

        anim.SetFloat("Walkspeed", Mathf.Abs(transform.position.x - xPos));
        xPos = transform.position.x;

        if(food.transform.position.y < bowTie.transform.position.y)
        {
            anim.SetBool("Panic", true);
        }
    }
}
