package ru.dbotthepony.mc.otm.datagen.blocks

import net.minecraft.core.Direction
import ru.dbotthepony.mc.otm.block.tech.EnergyCounterBlock
import ru.dbotthepony.mc.otm.block.entity.WorkerState
import ru.dbotthepony.mc.otm.block.matter.PatternStorageBlock
import ru.dbotthepony.mc.otm.block.storage.DriveViewerBlock
import ru.dbotthepony.mc.otm.core.math.BlockRotationFreedom
import ru.dbotthepony.mc.otm.core.math.yRotationBlockstateNorth
import ru.dbotthepony.mc.otm.datagen.modLocation
import ru.dbotthepony.mc.otm.registry.MBlocks

fun addComplexBlockStates(provider: MatteryBlockStateProvider) {
	with(provider.getMultipartBuilder(MBlocks.DRIVE_VIEWER)) {
		for (facing in BlockRotationFreedom.ONE.possibleValues) {
			part()
				.modelFile(provider.models().getExistingFile(modLocation("block/drive_viewer_drive_part")))
				.rotationY(facing.front.yRotationBlockstateNorth())
				.addModel()
				.condition(BlockRotationFreedom.ONE.property, facing)
				.condition(DriveViewerBlock.DRIVE_PRESENT, true)

			for (workState in WorkerState.SEMI_WORKER_STATE.possibleValues) {
				part()
					.modelFile(provider.models().getExistingFile(modLocation("block/drive_viewer_${workState.name.lowercase()}")))
					.rotationY(facing.front.yRotationBlockstateNorth())
					.addModel()
					.condition(WorkerState.SEMI_WORKER_STATE, workState)
					.condition(BlockRotationFreedom.ONE.property, facing)
			}
		}
	}

	with(provider.getMultipartBuilder(MBlocks.PATTERN_STORAGE)) {
		for (facing in BlockRotationFreedom.ONE.possibleValues) {
			part()
					.modelFile(provider.models().getExistingFile(modLocation("block/pattern_storage")))
					.rotationY(facing.front.yRotationBlockstateNorth())
					.addModel()
					.condition(BlockRotationFreedom.ONE.property, facing)

			for (i in 0 .. 7) {
				part()
					.modelFile(provider.models().getExistingFile(modLocation("block/pattern/model$i")))
					.rotationY(facing.front.yRotationBlockstateNorth())
					.addModel()
					.condition(BlockRotationFreedom.ONE.property, facing)
					.condition(PatternStorageBlock.PATTERN_STORAGE_DISKS_PROPS[i], true)
			}
		}
	}

	with(provider.getMultipartBuilder(MBlocks.ENERGY_COUNTER)) {
		// даваааййй
		val up = provider.models().getExistingFile(modLocation("block/energy_counter_up"))
		val down = provider.models().getExistingFile(modLocation("block/energy_counter_down"))
		val west = provider.models().getExistingFile(modLocation("block/energy_counter_west"))
		val east = provider.models().getExistingFile(modLocation("block/energy_counter_east"))

		// ДАААА ДАВАЙЙ ДАААВАААЙЙЙЙЙЙ
		val north = provider.models().getExistingFile(modLocation("block/energy_counter_north"))
		val northDown = provider.models().getExistingFile(modLocation("block/energy_counter_north_down"))
		val northEast = provider.models().getExistingFile(modLocation("block/energy_counter_north_east"))
		val northWest = provider.models().getExistingFile(modLocation("block/energy_counter_north_west"))
		val south = provider.models().getExistingFile(modLocation("block/energy_counter_south"))
		val southDown = provider.models().getExistingFile(modLocation("block/energy_counter_south_down"))
		val southEast = provider.models().getExistingFile(modLocation("block/energy_counter_south_east"))
		val southWest = provider.models().getExistingFile(modLocation("block/energy_counter_south_west"))

		for (dir in arrayOf(Direction.EAST, Direction.WEST, Direction.SOUTH, Direction.NORTH)) {
			part().modelFile(down).rotationY(dir.yRotationBlockstateNorth()).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, Direction.UP).condition(EnergyCounterBlock.IF_DIRECTION, dir)
			part().modelFile(up).rotationY(dir.yRotationBlockstateNorth()).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, Direction.DOWN).condition(EnergyCounterBlock.IF_DIRECTION, dir)
		}

		// низкий поклон за полностью рабочий поворот вокруг оси Z
		part().modelFile(north).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, Direction.NORTH).condition(EnergyCounterBlock.IF_DIRECTION, Direction.UP)
		part().modelFile(northDown).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, Direction.NORTH).condition(EnergyCounterBlock.IF_DIRECTION, Direction.DOWN)
		part().modelFile(northEast).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, Direction.NORTH).condition(EnergyCounterBlock.IF_DIRECTION, Direction.EAST)
		part().modelFile(northWest).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, Direction.NORTH).condition(EnergyCounterBlock.IF_DIRECTION, Direction.WEST)

		part().modelFile(south).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, Direction.SOUTH).condition(EnergyCounterBlock.IF_DIRECTION, Direction.UP)
		part().modelFile(southDown).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, Direction.SOUTH).condition(EnergyCounterBlock.IF_DIRECTION, Direction.DOWN)
		part().modelFile(southEast).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, Direction.SOUTH).condition(EnergyCounterBlock.IF_DIRECTION, Direction.EAST)
		part().modelFile(southWest).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, Direction.SOUTH).condition(EnergyCounterBlock.IF_DIRECTION, Direction.WEST)

		for (dir in arrayOf(Direction.WEST, Direction.EAST)) {
			val mdl = if (dir === Direction.WEST) west else east

			part().modelFile(mdl).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, dir).condition(EnergyCounterBlock.IF_DIRECTION, Direction.NORTH)
			part().modelFile(mdl).rotationX(-90).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, dir).condition(EnergyCounterBlock.IF_DIRECTION, Direction.UP)
			part().modelFile(mdl).rotationX(180).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, dir).condition(EnergyCounterBlock.IF_DIRECTION, Direction.SOUTH)
			part().modelFile(mdl).rotationX(90).addModel().condition(EnergyCounterBlock.INPUT_DIRECTION, dir).condition(EnergyCounterBlock.IF_DIRECTION, Direction.DOWN)
		}
	}
}
