local GameMode = require 'tetris.modes.gamemode'
local Bag7Randomizer = require 'tetris.randomizers.bag7'

local SurvivalGTEGame = GameMode:extend()

SurvivalGTEGame.name = "Survival GTE"
SurvivalGTEGame.hash = "SurvivalGTE"
SurvivalGTEGame.tagline = "A well-known Master mode that ramps up in difficulty quickly!"

function SurvivalGTEGame:new()
    SurvivalGTEGame.super:new()

    self.lock_drop = true
	self.lock_hard_drop = true
	self.instant_hard_drop = true
	self.instant_soft_drop = false
	self.enable_hold = true
    self.next_queue_length = 4
    self.randomizer = Bag7Randomizer()
end

function SurvivalGTEGame:getARE() return 6 end
function SurvivalGTEGame:getLineARE() return 6 end
function SurvivalGTEGame:getGravity() return 20 end
function SurvivalGTEGame:getARR() return 2 end

function SurvivalGTEGame:getDasLimit()
    return math.min(math.floor(self:getLockDelay() * 2/3), 10)
end

function SurvivalGTEGame:getLockDelay()
        if self.lines < 100 then return 29
    elseif self.lines < 110 then return 27
    elseif self.lines < 120 then return 25
    elseif self.lines < 130 then return 22
    elseif self.lines < 140 then return 20
    elseif self.lines < 150 then return 17
    elseif self.lines < 160 then return 16
    elseif self.lines < 180 then return 15
    elseif self.lines < 190 then return 13
    elseif self.lines < 210 then return 11
    elseif self.lines < 230 then return 10
    elseif self.lines < 240 then return 9
    elseif self.lines < 260 then return 8
    elseif self.lines < 280 then return 7
    elseif self.lines < 290 then return 6
    else return 5 end
end

function SurvivalGTEGame:getLineClearDelay()
    return math.max(30 - math.floor(self.lines / 10) * 3, 1)
end

function SurvivalGTEGame:onLineClear(cleared_row_count)
    self.lines = self.lines + cleared_row_count
    self.completed = self.lines >= 300
end

local function getLowestBlockY(offsets)
    local res = -math.huge
    for _, o in pairs(offsets) do
        if o.y > res then
            res = o.y
        end
    end
    return res
end

function SurvivalGTEGame:onEnterOrHold(...)
    while (
        getLowestBlockY(self.piece:getBlockOffsets()) + self.piece.position.y
    ) < 4 do
        self.piece.position.y = self.piece.position.y + 1
    end
    self.super.onEnterOrHold(self, ...)
end

function SurvivalGTEGame:advanceOneFrame()
    if self.ready_frames == 0 then
        self.frames = self.frames + 1
    end
end

function SurvivalGTEGame:getBackground()
    return math.floor(self.lines / 10) % 20
end

function SurvivalGTEGame:getHighscoreData()
    return {
        lines = self.lines,
        frames = self.frames
    }
end

function SurvivalGTEGame:drawScoringInfo()
    love.graphics.setColor(1, 1, 1, 1)

	if config["side_next"] then
        love.graphics.printf("NEXT", 240, 72, 40, "left")
    else
        love.graphics.printf("NEXT", 64, 40, 40, "left")
    end

    local text_x = config["side_next"] and 316 or 240
    
    love.graphics.setFont(font_3x5_2)
	love.graphics.print(
		self.das.direction .. " " ..
		self.das.frames .. " " ..
		strTrueValues(self.prev_inputs)
	)
    love.graphics.printf("SPEED LEVEL", text_x, 280, 160, "left")
    love.graphics.printf("LINES", text_x, 350, 160, "left")

    love.graphics.setFont(font_3x5_3)
    love.graphics.printf(
        "M" .. math.min(30, math.floor(self.lines / 10) + 1),
        text_x, 300, 160, "left"
    )
    love.graphics.printf(self.lines, text_x, 370, 160, "left")

    love.graphics.setFont(font_8x11)
	love.graphics.printf(formatTime(self.frames), 64, 420, 160, "center")
end

return SurvivalGTEGame