using Godot;
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

public partial class Player : CharacterBody2D
{
	[Export] float Speed;
	[Export] float sugar_speed;
	[Export] AnimationPlayer anim;
	[Export] Camera2D cam;
	[Export] TextureProgressBar bar;
	[ExportGroup("controls")]
	[Export] Control menu;
	[Export] float minute_part_second = 1;
	[Export] Label time;
	[Export] Sprite2D over;
	[Export] CanvasLayer gameover;
	[Export] AudioStreamPlayer audio;
	float minute;
	int heure = 18;

	[ExportGroup("gameover")]
	[Export] Texture2D caniche;
	[Export] Texture2D shotgun;
	[Export] Texture2D caramel;
	[Export] Texture2D glissemie;
	[Export] Texture2D voiture;
	[Export] Texture2D win;

	[Export] AudioStream aud_caniche;
	[Export] AudioStream aud_shotgun;
	[Export] AudioStream aud_caramel;
	[Export] AudioStream aud_glissemie;
	[Export] AudioStream aud_voiture;
	[Export] AudioStream aud_win;


	


	public float PixelSize = 1.0f;

	float sugar { get; set;}= 50;
	const float MAX_SUGAR = 100;
	bool end = false;

	bool pausable { get; set; } = true;
	
	public override void _Ready()
	{
        cam.PositionSmoothingEnabled = false;
        this.AddToGroup("player");
		
	}

	public override void _PhysicsProcess(double delta)
	{
		Vector2 velocity = Velocity;
		

		if (end == false)
		{
			sugar -= (float)delta / sugar_speed;
		}	


		bar.Value = sugar;
		
		if(sugar <= 5)
		{
			sugar = 50;
			end = true;
			OverGlissemie();
		}
		else if (sugar >= 90)
		{
			sugar = 50;
			end = true;
			OverCaramel();
		}

		minute += minute_part_second * (float) delta;

		if (minute >= 60)
		{
			
			minute = 0;
			heure +=1;
		}
		UpdateTime();

		if (heure >= 22)
		{
			heure = 0;
			OverWin();
		}
	
		Vector2 direction = Input.GetVector("left", "right", "up", "down");
		//direction = direction.Normalized();
		if (direction != Vector2.Zero)
		{
			velocity = direction * Speed * (float)delta;
		}
		else
		{
			velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
			velocity.Y = Mathf.MoveToward(Velocity.Y, 0, Speed);

			velocity = velocity.Round();
		}
		
		
		if(Input.IsActionPressed("quit") && pausable)
		{
			Input.MouseMode = Input.MouseModeEnum.Visible;
			GetTree().Paused= true;
			anim.Play("paused");
		}
		else if(velocity.X > 0)
		{
			anim.Play("right");
		}
		else if (velocity.X < 0)
		{
			anim.Play("left");
		}
		else if (velocity.Y > 0)
		{
			anim.Play("down");
		}
		else if (velocity.Y < 0)
		{
			anim.Play("up");
		}
		else
		{
			anim.Play("idle");
			
		}
		

		Velocity = velocity;
		MoveAndSlide();

		Position = Position.Round();
	}

	void UpdateTime()
	{
		time.Text = string.Format("{0:D2}:{1:D2}", heure, (int)Math.Floor(minute));
	}

	async void OverCanicheAsync()
	{
		await ToSignal(GetTree().CreateTimer(3), "timeout");
		Input.MouseMode = Input.MouseModeEnum.Visible;
		GetTree().Paused = true;
		audio.Stream = aud_caniche;
		audio.Play();
		over.Texture = caniche;
		gameover.Visible = true;
		end = true;
	}
	async void OverShotgun()
	{
		await ToSignal(GetTree().CreateTimer(0.8), "timeout");
		Input.MouseMode = Input.MouseModeEnum.Visible;
		audio.Stream = aud_shotgun;
		audio.Play();
		GetTree().Paused = true;
		over.Texture = shotgun;
		gameover.Visible = true;
		end = true;
	}
	void OverCaramel()
	{
		Input.MouseMode = Input.MouseModeEnum.Visible;
		audio.Stream = aud_caramel;
		audio.Play();

		GetTree().Paused = true;
		over.Texture = caramel;
		gameover.Visible = true;
		end = true;
	}
	void OverGlissemie()
	{
		Input.MouseMode = Input.MouseModeEnum.Visible;
		audio.Stream = aud_glissemie;
		audio.Play();
		GetTree().Paused = true;
		over.Texture = glissemie;
		gameover.Visible = true;
		end = true;
	}
	void OverVoiture()
	{
		Input.MouseMode = Input.MouseModeEnum.Visible;
		audio.Stream = aud_voiture;
		audio.Play();
		GetTree().Paused = true;
		over.Texture = voiture;
		gameover.Visible = true;
		end = true;
	}
	void OverWin()
	{
		Input.MouseMode = Input.MouseModeEnum.Visible;
		audio.Stream = aud_win;
		audio.Play();
		GetTree().Paused = true;
		over.Texture = win;
		gameover.Visible = true;
		end = true;
	}

}
