package com.example.rgcas;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Arc;
import javafx.scene.shape.Circle;
import javafx.scene.shape.CubicCurve;
import javafx.scene.shape.Ellipse;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
	private static double WIDTH  = 600;
	private static double HEIGHT = 600;
	
	private static Group getSmiley ( double width, double height ) {
		final double radius = Math.min ( width, height ) * 0.45;
		Group smiley = new Group ( );
		
		Circle face = new Circle ( radius );
		face.setFill ( null );
		face.setStroke ( Color.BLACK );
		face.setStrokeWidth ( 10 );
		
		Circle leftEye = new Circle ( 0.2 * radius );
		leftEye.setFill ( Color.BLACK );
		leftEye.getTransforms ( ).add (
				new Translate ( -radius / 2, -radius / 2 )
		);
		
		Circle rightEye = new Circle ( 0.2 * radius );
		rightEye.setFill ( Color.BLACK );
		rightEye.getTransforms ( ).add (
				new Translate ( radius / 2, -radius / 2 )
		);
		
		Arc mouth = new Arc ( 0, 0, radius / 2, radius / 2, 0, -180 );
		mouth.setFill ( null );
		mouth.setStroke ( Color.BLACK );
		mouth.setStrokeWidth ( 10 );
	
		smiley.getChildren ( ).addAll ( face, leftEye, rightEye, mouth );
		smiley.getTransforms ( ).add (
				new Translate ( width / 2, height / 2 )
		);
		
		return smiley;
	}
	
	private static Group getBalloons ( int numberOfBalloons, double width, double height ) {
		Group root = new Group ( );
		
		for ( int i = 0; i < numberOfBalloons; ++i ) {
			Group balloon = new Group ( );
			
			Color color = Color.color (
					Math.random ( ),
					Math.random ( ),
					Math.random ( ),
					Math.random ( ) * 0.9 + 0.1
			);
			
			Ellipse ellipse = new Ellipse ( 0.05 * width, 0.1 * height );
			ellipse.setFill ( color );
			balloon.getChildren ( ).add ( ellipse );
			
			balloon.getTransforms ( ).add (
					new Translate ( Math.random ( ) * width, Math.random ( ) * height )
			);
			
			root.getChildren ( ).add ( balloon );
			
			final double stringWidth = 0.05 * width;
			final double stringHeight = 0.1 * height;
			
			CubicCurve string = new CubicCurve (
					0, -stringHeight / 2,
					Math.random ( ) * stringWidth - stringWidth / 2, -stringHeight / 6,
					Math.random ( ) * stringWidth - stringWidth / 2, stringHeight / 6,
					Math.random ( ) * stringWidth - stringWidth / 2, stringHeight / 2
			);
			string.getTransforms ( ).add (
					new Translate ( 0, stringHeight / 2 + 0.1 * height )
			);
			string.setFill ( null );
			string.setStroke ( Color.BLACK );
			string.setStrokeWidth ( 2 );
			
			balloon.getChildren ( ).add ( string );
		}
		
		return root;
	}
	
	private static Group getFlower ( int numberOfPetals, double width, double height ) {
		Group flower = new Group ( );
		
		final double radius = 0.05 * Math.min ( width, height );
		final double petalRadiusX = 0.1 * Math.min ( width, height );
		final double petalRadiusY = 0.035 * Math.min ( width, height );
		
		for ( int i = 0; i < numberOfPetals; ++i ) {
			Ellipse petal = new Ellipse ( petalRadiusX, petalRadiusY );
			petal.getTransforms ( ).addAll (
				new Rotate ( i * 360. / numberOfPetals ),
				new Translate ( petalRadiusX + radius, 0 )
			);
			
			LinearGradient linearGradient = new LinearGradient (
					0, 0, 1, 1, true, CycleMethod.NO_CYCLE,
					new Stop ( 0, Color.YELLOW ),
					new Stop ( 1, Color.RED )
			);
			petal.setFill ( linearGradient );
			
			flower.getChildren ( ).add ( petal );
		}
		
		flower.getTransforms ( ).add (
				new Translate ( width / 2, height / 2 )
		);
		
		return flower;
	}
	
	@Override
	public void start ( Stage stage ) throws IOException {
		// Group root  = new Group ( HelloApplication.getSmiley ( WIDTH, HEIGHT ) );
		// Group root  = new Group ( HelloApplication.getBalloons ( 50, WIDTH, HEIGHT ) );
		Group root  = new Group ( HelloApplication.getFlower ( 10, WIDTH, HEIGHT ) );
		Scene scene = new Scene ( root, WIDTH, HEIGHT );
		
		stage.setTitle ( "Hello!" );
		stage.setScene ( scene );
		stage.show ( );
	}
	
	public static void main ( String[] args ) {
		launch ( );
	}
}