local just = require("just")
local imgui = require("imgui")
local ModalImView = require("ui.imviews.ModalImView")
local _transform = require("gfx_util").transform
local spherefonts = require("sphere.assets.fonts")

local TimingsSelectorView = require("ui.views.SelectView.TimingsSelectorView")

local transform = {{1 / 2, -16 / 9 / 2}, 0, 0, {0, 1 / 1080}, {0, 1 / 1080}, 0, 0, 0, 0}

local scrollY = 0

local w, h = 512, 512
local _w, _h = w / 2, 55
local r = 8
local window_id = "TimingsSelectorViewModal"

return ModalImView(function(self, quit)
	---@type sphere.GameController
	local game = self.game

	local replayBase = game.replayBase

	if quit then
		if replayBase.timings then
			game.rhythmModel.scoreEngine:createByTimings(replayBase.timings, replayBase.subtimings, true)
		end
		return true
	end

	imgui.setSize(w, h, _w, _h)

	love.graphics.setFont(spherefonts.get("Noto Sans", 24))

	love.graphics.replaceTransform(_transform(transform))
	love.graphics.translate((1920 - w) / 2, (1080 - h) / 2)

	love.graphics.setColor(0, 0, 0, 0.8)
	love.graphics.rectangle("fill", 0, 0, w, h, r)
	love.graphics.setColor(1, 1, 1, 1)

	just.push()

	imgui.Container(window_id, w, h, _h / 3, _h * 2, scrollY)

	TimingsSelectorView(game)

	if replayBase.timings and imgui.button("applyTimings", "apply") then
		game.rhythmModel.scoreEngine:createByTimings(replayBase.timings, replayBase.subtimings, true)
	end

	scrollY = imgui.Container()
	just.pop()

	love.graphics.setColor(1, 1, 1, 1)
	love.graphics.rectangle("line", 0, 0, w, h, r)
end)
