using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;


public class PlayerMovement : MonoBehaviour
{
    public float speed;
    public float rotSpeed;

    public GameObject plane;

    public float speedBoostTimer;
    public ParticleSystem speedEffect;

    public Animator anim;
  
    public bool moveLeft;
    public bool moveRight;
    public bool notMove;

    void Start()
    {
       
        anim = plane.GetComponent<Animator>();
        notMove = true;
    }


    void Update()
    {
        Move();

        if (speedBoostTimer>0)
            speedBoostTimer-=Time.deltaTime;

        if(speedBoostTimer > 0 && !speedEffect.isPlaying)
        {
            Debug.Log("Speed effect is playing!");
            speedEffect.Play();
        }
        else if(speedBoostTimer <= 0 && speedEffect.isPlaying)
        {
            speedEffect.Stop();
        }

        transform.Translate(0, 0, speed * (speedBoostTimer >0? 2f:1f) * Time.deltaTime);

    }

    public void Move()
    {
        if(Input.GetKey("a"))
        {
            notMove = false;
            moveLeft = true;
        }
        else if (Input.GetKey("d")) 
        {
            notMove = false;
            moveLeft = false;
        }

        if(Input.GetKeyUp("a") || Input.GetKeyUp("d"))
        {
            notMove = true;
        }
        
        if (notMove)
        {
            Stop();
        }
        else
        {
            if (moveLeft)
            {
                left();
            }
            else if (!moveLeft)
            {
                right();
            }
            
        }
    }

    public void Stop()
    {
        anim.SetInteger("controll", 0);
    }

    public void left()
    {
        anim.SetInteger("controll", 1);

        transform.Rotate(0, -rotSpeed * Time.deltaTime, 0);
    }



    public void right()
    {
        anim.SetInteger("controll", 2);

        transform.Rotate(0, rotSpeed * Time.deltaTime, 0);
    }

    public void AllowMovement(bool movement)
    {
        notMove = false;
        moveLeft = movement;
    }
    
    public void DontAllowMovement()
    {
        notMove = true;
    }
}
