package ru.dbotthepony.mc.otm.menu.widget

import ru.dbotthepony.mc.otm.block.entity.MachineJobEventLoop
import ru.dbotthepony.mc.otm.block.entity.MatteryWorkerBlockEntity
import ru.dbotthepony.mc.otm.core.FloatSupplier
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.network.synchronizer.FieldSynchronizer
import java.util.function.BooleanSupplier

@Suppress("unused")
class ProgressGaugeWidget(synchronizer: FieldSynchronizer) {
	constructor(menu: MatteryMenu) : this(menu.mSynchronizer)

	var progressSupplier: FloatSupplier = FloatSupplier { 0f }
	var stuckSupplier: BooleanSupplier = BooleanSupplier { false }

	val percentage by synchronizer.ComputedFloatField(getter = { progressSupplier.getAsFloat() }).property
	val isStuck by synchronizer.ComputedBooleanField(getter = { stuckSupplier.asBoolean }).property

	constructor(
		menu: MatteryMenu,
		progress: FloatSupplier
	) : this(menu) {
		this.progressSupplier = progress
	}

	constructor(
		menu: MatteryMenu,
		blockEntity: MatteryWorkerBlockEntity<*>?
	) : this(menu) {
		if (blockEntity != null) {
			with(blockEntity)
		}
	}

	constructor(
		menu: MatteryMenu,
		job: MachineJobEventLoop<*>?
	) : this(menu) {
		if (job != null) {
			with(job)
		}
	}

	constructor(
		synchronizer: FieldSynchronizer,
		progress: FloatSupplier
	) : this(synchronizer) {
		this.progressSupplier = progress
	}

	constructor(
		synchronizer: FieldSynchronizer,
		blockEntity: MatteryWorkerBlockEntity<*>?
	) : this(synchronizer) {
		if (blockEntity != null) {
			with(blockEntity)
		}
	}

	constructor(
		synchronizer: FieldSynchronizer,
		job: MachineJobEventLoop<*>?
	) : this(synchronizer) {
		if (job != null) {
			with(job)
		}
	}

	constructor(
		menu: MatteryMenu,
		progress: FloatSupplier,
		stuck: BooleanSupplier,
	) : this(menu) {
		this.progressSupplier = progress
		this.stuckSupplier = stuck
	}

	fun with(progress: FloatSupplier): ProgressGaugeWidget {
		this.progressSupplier = progress
		this.stuckSupplier = BooleanSupplier { false }
		return this
	}

	fun with(progress: FloatSupplier, stuck: BooleanSupplier): ProgressGaugeWidget {
		this.progressSupplier = progress
		this.stuckSupplier = stuck
		return this
	}

	fun with(blockEntity: MatteryWorkerBlockEntity<*>): ProgressGaugeWidget {
		with(blockEntity.jobEventLoops[0]::workProgress, blockEntity.jobEventLoops[0]::isUnableToProcess)
		return this
	}

	fun with(job: MachineJobEventLoop<*>): ProgressGaugeWidget {
		with(job::workProgress, job::isUnableToProcess)
		return this
	}
}
