package com.isartdigital.ui.screens;

import com.isartdigital.game.LevelManager;
import com.isartdigital.game.sprites.Animation_titlecard;
import com.isartdigital.ui.buttons.CreditButton;
import com.isartdigital.ui.buttons.HelpButton;
import com.isartdigital.ui.buttons.PlayButton;
import com.isartdigital.ui.popin.Annonce;
import com.isartdigital.ui.popin.Confirm;
import com.isartdigital.utils.Config;
import com.isartdigital.utils.events.EventType;
import com.isartdigital.utils.events.MouseEventType;
import com.isartdigital.utils.events.TouchEventType;
import com.isartdigital.utils.game.GameStage;
import com.isartdigital.utils.sounds.SoundManager;
import com.isartdigital.utils.system.DeviceCapabilities;
import com.isartdigital.utils.ui.Button;
import com.isartdigital.utils.ui.Screen;
import pixi.core.math.Point;
import pixi.core.sprites.Sprite;
import pixi.core.textures.Texture;
import pixi.interaction.EventTarget;
import pixi.interaction.InteractionData;

	
/**
 * Exemple de classe héritant de Screen
 * @author Mathieu ANTHOINE
 */
class TitleCard extends Screen 
{
	private var background:Sprite;
	
	/**
	 * instance unique de la classe TitleCard
	 */
	private static var instance: TitleCard;
	
	private var playButton:PlayButton;
	private var buttonPlayFinalPosition: Point;
	
	private var helpButton:HelpButton;
	private var buttonHelpFinalPosition: Point;
	
	private var creditsButton:CreditButton;
	private var buttonCreditFinalPosition: Point;
	
	private var countdown: Int = 45;
	private var tweenOn: Bool;
	private var haveASound:Bool = false;
	
	private var playBlobCount: Float;
	private var creditBlobCount: Float;
	private var helpBlobCount: Float;
	
	/**
	 * Retourne l'instance unique de la classe, et la crée si elle n'existait pas au préalable
	 * @return instance unique
	 */
	public static function getInstance (): TitleCard {
		if (instance == null) instance = new TitleCard();
		return instance;
	}
	
	/**
	 * constructeur privé pour éviter qu'une instance soit créée directement
	 */
	public function new() 
	{
		super();
		
		background = new Sprite(Texture.fromImage(Config.assetsPath+"TitleCard_bg.png"));
		background.anchor.set(0.5, 0.5);
		addChild(background);
		
		playButton = new PlayButton();
		playButton.position.set(-playButton.width/2, -GameStage.getInstance().height);
		buttonPlayFinalPosition = new Point(-playButton.width/2, GameStage.getInstance().height * (1/3));
		playButton.scale.x = playButton.scale.y *= 0.6;
		addChild(playButton);
		
		creditsButton = new CreditButton();
		creditsButton.position.set( -GameStage.getInstance().width, GameStage.getInstance().height * (1 / 3));
		buttonCreditFinalPosition = new Point(buttonPlayFinalPosition.x - creditsButton.width * 1.25, GameStage.getInstance().height * (1 / 3));
		creditsButton.scale.x = creditsButton.scale.x *= 0.6;
		addChild(creditsButton);
		
		helpButton = new HelpButton();
		helpButton.position.set(GameStage.getInstance().width, GameStage.getInstance().height * (1 / 3));
		buttonHelpFinalPosition = new Point(buttonPlayFinalPosition.x + helpButton.width * 1.5, GameStage.getInstance().height * (1 / 3));
		helpButton.scale.x = helpButton.scale.x *= 0.6;
		addChild(helpButton);
		
		addChild(Animation_titlecard.getInstance());
		Animation_titlecard.getInstance().position.set( -Animation_titlecard.getInstance().width/1.75, -Animation_titlecard.getInstance().height/3);
		
		playButton.click = onClickOnPlay;
		creditsButton.click = onClickOnCredit;
		helpButton.click = onClickOnHelp;
		
		Main.getInstance().on(EventType.GAME_LOOP, TCLoop);
		
		
	}
	
	/*private function onClick (pEvent:EventTarget): Void {
		SoundManager.getSound("click").play();
		UIManager.getInstance().openPopin(Confirm.getInstance());
	}*/
	
	private function onClickOnPlay (pData:EventTarget): Void {
		SoundManager.getSound("Ronronnement1").play();
		LevelManager.getInstance().setObjects();
		UIManager.getInstance().openPopin(Annonce.getInstance());
		
	}
	
	private function onClickOnCredit (pData:EventTarget): Void {
		SoundManager.getSound("Ronronnement2").play();
		UIManager.getInstance().openPopin(Credit.getInstance());
	}
	
	private function onClickOnHelp (pData:EventTarget):Void {
		SoundManager.getSound("Ronronnement2").play();
		UIManager.getInstance().openPopin(Help.getInstance());
	}
	
	private function TCLoop (pEvent:EventTarget): Void {
		
		if (!haveASound) {
			SoundManager.getSound("MenuLoop").play();
		
			haveASound = true;
		}
		
		if (countdown > 0) {
			countdown --;
			return;
		}
		
		// tween de la position des boutons
		if (tweenOn) {
			playButton.position.y += (buttonPlayFinalPosition.y - playButton.position.y) * 0.1;		
			creditsButton.position.x += (buttonCreditFinalPosition.x - creditsButton.position.x) * 0.1;
			helpButton.position.x += (buttonHelpFinalPosition.x - helpButton.position.x) * 0.1;
			
			// si on arrive suffisamment proche de la position souhaitée, on set la position définitive et on sort de la condition
			if ((playButton.position.y - buttonPlayFinalPosition.y) > -5 && (creditsButton.position.x - buttonCreditFinalPosition.x) > -50 && (helpButton.position.x - buttonHelpFinalPosition.x) > 50) {
				playButton.position.y = buttonPlayFinalPosition.y;
				creditsButton.position.x = buttonCreditFinalPosition.x;
				helpButton.position.x = buttonHelpFinalPosition.x;
				tweenOn = false;
			}
		}
		
		// blob du bouton play
		playButton.scale.x = 1 + Math.sin(playBlobCount) * 0.04;
		playButton.scale.y = 1 + Math.cos(playBlobCount) * 0.04;
		playBlobCount += 0.1;
		
		creditsButton.scale.x = 1 + Math.sin(creditBlobCount) * 0.04;
		creditsButton.scale.y = 1 + Math.cos(creditBlobCount) * 0.04;
		creditBlobCount += 0.1;
		
		helpButton.scale.x = 1 + Math.sin(helpBlobCount) * 0.04;
		helpButton.scale.y = 1 + Math.cos(helpBlobCount) * 0.04;
		helpBlobCount += 0.1;
	}
	
	
	override public function open():Void 
	{
		super.open();
		playBlobCount = 0;
		creditBlobCount = 0;
		helpBlobCount = 0;
		tweenOn = true;
	}
	
	override public function close():Void 
	{
		Main.getInstance().off(EventType.GAME_LOOP, TCLoop);
		super.close();
	}
	
	/**
	 * détruit l'instance unique et met sa référence interne à null
	 */
	override public function destroy (): Void {
		instance = null;
		super.destroy();
	}

}