local basalt = require("basalt")
local libcredit = require("/lib/libcredit_mobile")

if fs.exists("/config.json") then
    
    while true do
        shell.run("/slabos.lua")
        term.clear()
        print("Starting...")
        sleep(1)
    end

end

local main = basalt.getMainFrame()

local contentFrame = main:addFrame()
    :setPosition(1,1)
    :setSize(26,20)
    :setBackground(colours.white)

local colourSelectionFrame = main:addFrame()
    :setPosition(1,1)
    :setSize(26,20)
    :setBackground(colours.white)
    :setVisible(false)

local usernameSetupFrame = main:addFrame()
    :setPosition(1,1)
    :setSize(26,20)
    :setBackground(colours.white)
    :setVisible(false)

local passwordSetupFrame = main:addFrame()
    :setPosition(1,1)
    :setSize(26,20)
    :setBackground(colours.white)
    :setVisible(false)

local selectedColourButton = nil
local selectedWallpaperColourName = "white"
local selectedUsername = ""
local selectedPassword = ""

local welcomeBigFont = contentFrame:addBigFont()
    :setText("Welcome!")
    :setBackground(colours.white)
    :setSize(40,5)
    :setPosition(2, 2)

contentFrame:addButton()
    :setText("Begin Setup")
    :setBackground(colours.lime)
    :setPosition(6, 8)
    :setSize(15, 3)
    :onClick(function(self)
        contentFrame:setVisible(false)
        colourSelectionFrame:setVisible(true)
    end)

contentFrame:addLabel()
    :setText("Put me in your main hand and right click")
    :setSize(26,2)
    :setAutoSize(false)
    :setPosition(2,15)

colourSelectionFrame:addLabel()
    :setText("Choose your wallpaper")
    :setPosition(3, 2)
    :setSize(22, 1)

local wallpaperColours = {}

for name, colourValue in pairs(colours) do
    if type(colourValue) == "number" then
        local textColour = colours.black
        if colourValue == colours.black or colourValue == colours.blue or colourValue == colours.red or
           colourValue == colours.purple or colourValue == colours.brown or colourValue == colours.gray then
            textColour = colours.white
        end
        table.insert(wallpaperColours, {name = name, colour = colourValue, textColour = textColour})
    end
end

table.sort(wallpaperColours, function(a, b)
    return a.name < b.name
end)

local startX = 2
local startY = 4
local buttonWidth = 5
local buttonHeight = 3
local paddingX = 1
local paddingY = 1
local coloursPerRow = 4
local maxRows = 3
local maxColoursToShow = coloursPerRow * maxRows

local navButtonHeight = 3
local buttonPaddingBottom = 1
local buttonPaddingSide = 2

for i = 1, math.min(#wallpaperColours, maxColoursToShow) do
    local colourData = wallpaperColours[i]
    local col = ((i - 1) % coloursPerRow)
    local row = math.floor((i - 1) / coloursPerRow)

    local x = startX + (buttonWidth + paddingX) * col
    local y = startY + (buttonHeight + paddingY) * row

    colourSelectionFrame:addButton()
        :setText("")
        :setBackground(colourData.colour)
        :setForeground(colourData.textColour)
        :setPosition(x, y)
        :setSize(buttonWidth, buttonHeight)
        :onClick(function(self)
            if selectedColourButton then
                selectedColourButton:setText("")
            end
            self:setText("X")
            selectedColourButton = self
            selectedWallpaperColourName = colourData.name
        end)
end

colourSelectionFrame:addButton()
    :setText("Next >")
    :setBackground(colours.green)
    :setPosition(colourSelectionFrame:getWidth() - 7 - buttonPaddingSide, colourSelectionFrame:getHeight() - navButtonHeight - buttonPaddingBottom)
    :setSize(8,navButtonHeight)
    :onClick(function(self)
        colourSelectionFrame:setVisible(false)
        usernameSetupFrame:setVisible(true)
    end)

colourSelectionFrame:addButton()
    :setText("< Back")
    :setBackground(colours.red)
    :setPosition(buttonPaddingSide, colourSelectionFrame:getHeight() - navButtonHeight - buttonPaddingBottom)
    :setSize(8,navButtonHeight)
    :onClick(function(self)
        colourSelectionFrame:setVisible(false)
        contentFrame:setVisible(true)
    end)

usernameSetupFrame:addLabel()
    :setText("Set your Username")
    :setPosition(4, 2)
    :setSize(22, 1)

local usernameInput = usernameSetupFrame:addInput()
    :setPosition(3, 4)
    :setSize(20, 1)
    :setText("")

usernameSetupFrame:addLabel()
    :setText("WARNING: Use your In-Game Name (IGN) for eCash to ensure proper transactions and avoid issues!")
    :setPosition(4, 6)
    :setSize(20, 4)
    :setAutoSize(false)
    :setForeground(colours.red)

local confirmUsernameButton = usernameSetupFrame:addButton()
    :setText("Next >")
    :setBackground(colours.green)
    :setPosition(usernameSetupFrame:getWidth() - 7 - buttonPaddingSide, usernameSetupFrame:getHeight() - navButtonHeight - buttonPaddingBottom)
    :setSize(8,navButtonHeight)
    :onClick(function(self)
        local inputUsername = usernameInput:getText()
        if inputUsername and inputUsername ~= "" and inputUsername ~= "Your IGN here" then
            selectedUsername = inputUsername
            usernameSetupFrame:setVisible(false)
            passwordSetupFrame:setVisible(true)
        end
    end)

usernameSetupFrame:addButton()
    :setText("< Back")
    :setBackground(colours.red)
    :setPosition(buttonPaddingSide, usernameSetupFrame:getHeight() - navButtonHeight - buttonPaddingBottom)
    :setSize(8,navButtonHeight)
    :onClick(function(self)
        usernameSetupFrame:setVisible(false)
        colourSelectionFrame:setVisible(true)
    end)

passwordSetupFrame:addLabel()
    :setText("Set your password (optional)")
    :setAutoSize(false)
    :setPosition(4, 2)
    :setSize(22, 1)

local passwordInput = passwordSetupFrame:addInput()
    :setPosition(3, 4)
    :setSize(20, 1)
    :setText("")
    :setReplaceChar("*")

local confirmPasswordButton = passwordSetupFrame:addButton()
    :setText("Done >")
    :setBackground(colours.green)
    :setPosition(passwordSetupFrame:getWidth() - 7 - buttonPaddingSide, passwordSetupFrame:getHeight() - navButtonHeight - buttonPaddingBottom)
    :setSize(8,navButtonHeight)
    :onClick(function(self)
        local inputPassword = passwordInput:getText()
        if inputPassword and inputPassword ~= "" then
            selectedPassword = inputPassword
        else
            selectedPassword = ""
        end

        libcredit.createAccount(selectedUsername,os.getComputerID(),function()
        
        
        end)

        local configData = {
            wallpaperColour = selectedWallpaperColourName,
            username = selectedUsername,
            password = selectedPassword,
        }

        local jsonString = textutils.serialiseJSON(configData)

        local filePath = "config.json"

        local file = fs.open(filePath, "w")
        if file then
            file.write(jsonString)
            file.close()
        end
        os.reboot()
    end)

passwordSetupFrame:addButton()
    :setText("< Back")
    :setBackground(colours.red)
    :setPosition(buttonPaddingSide, passwordSetupFrame:getHeight() - navButtonHeight - buttonPaddingBottom)
    :setSize(8,navButtonHeight)
    :onClick(function(self)
        passwordSetupFrame:setVisible(false)
        usernameSetupFrame:setVisible(true)
    end)

basalt.run()