package ru.dbotthepony.mc.otm.client.screen.storage

import net.minecraft.network.chat.Component
import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import ru.dbotthepony.kommons.util.Delegate
import ru.dbotthepony.mc.otm.client.render.ItemStackIcon
import ru.dbotthepony.mc.otm.client.screen.MatteryScreen
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.client.screen.panels.*
import ru.dbotthepony.mc.otm.client.screen.panels.button.BooleanButtonPanel
import ru.dbotthepony.mc.otm.client.screen.panels.button.DeviceControls
import ru.dbotthepony.mc.otm.client.screen.panels.slot.AbstractSlotPanel
import ru.dbotthepony.mc.otm.client.screen.panels.slot.BatterySlotPanel
import ru.dbotthepony.mc.otm.client.screen.panels.slot.FilterSlotPanel
import ru.dbotthepony.mc.otm.client.screen.panels.slot.SlotPanel
import ru.dbotthepony.mc.otm.client.screen.panels.util.GridPanel
import ru.dbotthepony.mc.otm.client.screen.widget.WideProfiledPowerGaugePanel
import ru.dbotthepony.mc.otm.core.util.ItemStorageStackSorter
import ru.dbotthepony.mc.otm.item.PortableCondensationDriveItem
import ru.dbotthepony.mc.otm.menu.storage.DriveViewerMenu
import ru.dbotthepony.mc.otm.registry.game.MItems
import yalter.mousetweaks.api.MouseTweaksDisableWheelTweak

@MouseTweaksDisableWheelTweak
class DriveViewerScreen(menu: DriveViewerMenu, inventory: Inventory, title: Component) :
	MatteryScreen<DriveViewerMenu>(menu, inventory, title) {

	override fun makeMainFrame(): FramePanel<MatteryScreen<*>> {
		val frame = FramePanel(this, null, 0f, 0f, 200f, 114f, getTitle())

		frame.makeCloseButton()
		frame.onClose { onClose() }

		val controls = DeviceControls(this, frame, redstoneConfig = menu.redstoneConfig, energyConfig = menu.energyConfig)

		controls.sortingButtons(Delegate.Of(menu.settings::isAscending), Delegate.Of(menu.settings::sorting), ItemStorageStackSorter.DEFAULT) {
			for (v in ItemStorageStackSorter.entries) {
				add(v, v.icon, v.title)
			}
		}

		val view = ArrayList<EditablePanel<*>>()
		val settings = ArrayList<EditablePanel<*>>()

		view.add(EditablePanel(this, frame, width = AbstractSlotPanel.SIZE).also {
			it.dock = Dock.LEFT
			WideProfiledPowerGaugePanel(this, it, menu.profiledEnergy).also {
				it.dock = Dock.TOP
				it.dockBottom = 6f
			}

			BatterySlotPanel(this, it, menu.batterySlot).dock = Dock.TOP
			SlotPanel(this, it, menu.driveSlot).dock = Dock.TOP
		})

		view.add(NetworkedItemGridPanel(this, frame, menu.networkedItemView).also {
			it.dock = Dock.FILL
			it.setDockMargin(4f, 0f, 0f, 0f)
		})

		val filterGrid = GridPanel(this, frame, width = AbstractSlotPanel.SIZE * 3f, height = AbstractSlotPanel.SIZE * 4f, rows = 3, columns = 4)
		filterGrid.dock = Dock.FILL
		filterGrid.dockResize = DockResizeMode.NONE
		settings.add(filterGrid)

		for (i in 0 until PortableCondensationDriveItem.MAX_FILTERS) {
			FilterSlotPanel(this, filterGrid, menu.driveFilterSlots[i], 0f, 0f)
		}

		settings.add(EditablePanel(this, frame, width = 90f).also {
			it.dock = Dock.LEFT
			BooleanButtonPanel.Checkbox(this, it, menu.isWhitelist, TranslatableComponent("otm.gui.filter.is_whitelist")).also { it.dockTop = 20f; it.dock = Dock.TOP }
			BooleanButtonPanel.Checkbox(this, it, menu.matchTag, TranslatableComponent("otm.gui.filter.match_tag")).also { it.dockTop = 4f; it.dock = Dock.TOP }
			BooleanButtonPanel.Checkbox(this, it, menu.matchComponents, TranslatableComponent("otm.gui.filter.match_nbt")).also { it.dockTop = 4f; it.dock = Dock.TOP }
		})

		frame.CustomTab(view, activeIcon = ItemStackIcon(ItemStack(MItems.PORTABLE_CONDENSATION_DRIVE)))
		frame.CustomTab(settings, activeIcon = ItemStackIcon(ItemStack(Items.HOPPER)))

		return frame
	}
}
