package eu.printingin3d.models.models;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import eu.printingin3d.javascad.coords.Angles3d;
import eu.printingin3d.javascad.coords.Coords3d;
import eu.printingin3d.javascad.coords.Dims3d;
import eu.printingin3d.javascad.models.Abstract3dModel;
import eu.printingin3d.javascad.models.Cube;
import eu.printingin3d.javascad.models.Cylinder;
import eu.printingin3d.javascad.tranzitions.Difference;
import eu.printingin3d.javascad.tranzitions.Mirror;
import eu.printingin3d.javascad.tranzitions.Union;
import eu.printingin3d.javascad.utils.SaveScadFiles;

public class Shelf extends Union {
	private static final double SIZE = 60.0;
	private static final double THICKNESS = 3.0;
	private static final double BIG_PATTERN_RAD = 27.0;
	private static final double BIG_PATTERN_GAP = 4.0;
	private static final double SMALL_PATTERN_RAD = 5.5;
	private static final double SMALL_PATTERN_CENTER = 12.0;
	private static final double SMALL_PATTERN_GAP = SMALL_PATTERN_RAD*1.4;

	private static final double HOLDER_THICK = 3.0;
	private static final double HOLDER_SIZE = 8.0;

	public Shelf() {
		super(createMainSupporter(), createHolder());
	}
	
	private static Abstract3dModel createMainSupporter() {
		return new Difference(
						new Cube(new Dims3d(SIZE, SIZE, THICKNESS)),
						new Union(
								new Cylinder(THICKNESS+1.0, BIG_PATTERN_RAD).moves(
											Arrays.asList(new Coords3d(SIZE/2.0, -SIZE/2.0+BIG_PATTERN_RAD+BIG_PATTERN_GAP, 0.0),
														new Coords3d(-SIZE/2.0+BIG_PATTERN_RAD+BIG_PATTERN_GAP, SIZE/2.0, 0.0),
														new Coords3d(+SIZE/2.0, SIZE/2.0, 0.0))),
								new Cylinder(THICKNESS+1.0, SMALL_PATTERN_RAD).move(new Coords3d(-SMALL_PATTERN_CENTER, -SMALL_PATTERN_CENTER, 0)),
								new Cylinder(THICKNESS+1.0, SMALL_PATTERN_RAD)
										.move(new Coords3d(-SMALL_PATTERN_CENTER, -SMALL_PATTERN_CENTER, 0))
										.moves(Arrays.asList(
												Coords3d.xOnly(+SMALL_PATTERN_GAP), 
												Coords3d.xOnly(-SMALL_PATTERN_GAP),
												Coords3d.yOnly(+SMALL_PATTERN_GAP),
												Coords3d.yOnly(-SMALL_PATTERN_GAP)))
								));
	}
	
	private static Abstract3dModel createHolder() {
		return 
			new Union(
				new Difference(
					new Cube(new Dims3d(SIZE+HOLDER_SIZE+HOLDER_THICK, HOLDER_THICK, THICKNESS*2)),
					new Cylinder(HOLDER_THICK+1.0, 2.0).rotate(Angles3d.ROTATE_PLUS_X).move(Coords3d.xOnly(-SIZE/2-HOLDER_THICK/2)))
				.move(new Coords3d(-HOLDER_SIZE/2-HOLDER_THICK/2, -SIZE/2-HOLDER_THICK/2+0.01, THICKNESS/2)),
			new Cube(new Dims3d(HOLDER_THICK, SIZE, THICKNESS*2)).move(new Coords3d(-SIZE/2-HOLDER_THICK/2, 0.0, THICKNESS/2)));
	}
	
	public static void main(String[] args) throws IOException {
		new SaveScadFiles(new File("C:/temp")).
				addModel("shelf.right.print.scad", new Shelf()).
				addModel("shelf.left.print.scad", Mirror.mirrorX(new Shelf())).
				saveScadFiles();
	}
}
