local currentscore = 0
local maxCells = 12
local maxStrokesPerCell = 5
local widthScoreBoard = 1920
local heightScoreBoard = 1080
local row = 2
local widthCell = row * widthScoreBoard / maxCells
local distBetweenStrokes = widthCell / maxStrokesPerCell
local midHeightCell = heightScoreBoard / (row * 2)

Cell = {
	x = 0,
	y = midHeightCell,
	width = widthCell,
	strokes = {},

	getScoreCount = function(self)
		return #self.strokes 
	end,

	addScore = function(self)
		count = self:getScoreCount()
		self.strokes[#self.strokes + 1] = {x = self.x + count * distBetweenStrokes, y = self.y, img = "MOD/images/stroke" .. math.random(1, 9) .. ".png"}
		
		if #self.strokes == maxStrokesPerCell then 
			self.strokes[#self.strokes] = {x = self.x, y = self.y, img = "MOD/images/stroke" .. math.random(10, 11) .. ".png"} 
		end
	end,

	draw = function(self, dt)
		for i = 1, #self.strokes do
			UiPush()
				UiTranslate(self.strokes[i].x, self.strokes[i].y)
				UiAlign("middle")
				UiImage(self.strokes[i].img)
			UiPop()
		end
	end,

	clean = function(self)
		self.strokes = {}
	end
}

function Cell:new(x, y)
	local obj = {
		x = x,
		y = y,
		width = widthCell,
		strokes = {}
	}
	setmetatable(obj, { __index = Cell })
	return obj
end

ScoreBoard = {
	content = {},

	addScore = function(self)
		local cell = self.content[#self.content]
		if cell:getScoreCount() == maxStrokesPerCell then
			self:addCell(cell)
			cell = self.content[#self.content]
		end
		cell:addScore()
	end,

	addCell = function(self, prevCell)
		local newCell = Cell:new(prevCell.x + prevCell.width, prevCell.y)
		self.content[#self.content + 1] = newCell
		if #self.content == maxCells / 2 + 1 then 
			newCell = Cell:new(prevCell.x - (maxCells / row - 1) * prevCell.width, prevCell.y * 3)
			self.content[#self.content] = newCell
		end
	end,

	drawBoard = function(self, dt)
		for _, cell in pairs(self.content) do
			cell:draw(dt)
		end
	end,

	clean = function(self)
		Cell:clean()
		self.content = {Cell}
	end
}

function tick(dt)
	local score = GetInt("game.mod.shootergame.scores")

	if score == 0 or currentscore == 0 then
		ScoreBoard:clean()
		currentscore = 0
	end

	if score > currentscore then
		local arraylength = score - currentscore
		for i = 1, arraylength do 
			ScoreBoard:addScore()
		end
		currentscore = score
	end
end

function draw(dt)
	local timeScore = GetInt("game.mod.shootergame.timescore")
	if timeScore > 0 then
		UiFont("bold.ttf", 200)
		UiTranslate(UiCenter(), UiMiddle()-50)
		UiAlign("center middle")
		UiText(GetTranslatedStringByKey("UI_TEXT_SCORE_TIME").. " : " .. timeScore)
	else
		ScoreBoard:drawBoard(dt)
	end
end