local _, addonTable = ...

---------------------------------------------------------------------------

function cmd_print(msg, color)
    if not color then
        color = "|cff85c5ff"
    end
    print(color.. "ScritchPop:|r " .. msg)
end

SLASH_SCRITCHPOP1 = "/scritchpop"
SLASH_SCRITCHPOP2 = "/scritch"
SLASH_SCRITCHPOP3 = "/spcp"

SlashCmdList["SCRITCHPOP"] = function(msg)
    local cmd, arg = msg:match("^(%S*)%s*(.-)$")
    local config = addonTable.getConfig()
    local cp_frame = addonTable.cp_frame

    cmd = cmd:lower()

    if cmd == "scale" then
        if arg == "default" then
            arg = "1.5"
        end
        local scale = tonumber(arg)

        if not scale then
            cmd_print("Please provide a number, example: /scritchpop scale 2.5")
            return
        end
        
        local cp_frame = addonTable.cp_frame
        if cp_frame then
            config.scale = scale*0.1
            cp_frame:SetScale(scale*0.1)
            cmd_print("Scale set to " .. scale)
        else
            cmd_print("Bar not initialized yet.")
        end
    elseif cmd == "lock" then
        local parent = cp_frame:GetFrame()
        if parent:IsMovable() then
            config.movable = false
            parent:SetMovable(false)
            cmd_print("Bar locked.")
        else
            config.movable = true
            parent:SetMovable(true)
            cmd_print("Bar unlocked.")
        end
    elseif cmd == "color" then
        local validColors = { Blue=true, Red=true, Green=true, Yellow=true, Purple=true }
        arg = arg:sub(1,1):upper() .. arg:sub(2):lower()

        if validColors[arg] then
            config.color = arg
            cp_frame:UpdateDisplay()
            cmd_print("Color set to " .. arg .. ".")
        else
            cmd_print("Invalid color. Valid options are: Blue, Red, Green, Yellow, Purple.")
        end
    elseif cmd == "shape" then
        local validColors = { scritchPop=true, kitchPop=true }

        if validColors[arg] then
            config.shape = arg
            cp_frame:UpdateDisplay()
            cmd_print("Shape set to " .. arg .. ".")
        else
            cmd_print("Invalid shape. Valid options are: scritchPop, kitchPop")
        end
    elseif cmd == "position" then
        local arg1, arg2 = arg:match("^(%-?%d+)%s+(%-?%d+)$")
        config.position_x = arg1
        config.position_y = arg2
        cp_frame:ApplyLayout()
        cp_frame:UpdateDisplay()
        cmd_print("position set to " .. arg .. ".")
    elseif cmd == "position_x" then
        config.position_x = arg
        cp_frame:ApplyLayout()
        cp_frame:UpdateDisplay()
        cmd_print("position_x set to " .. arg .. ".")
    elseif cmd == "position_y" then
        config.position_y = arg
        cp_frame:ApplyLayout()
        cp_frame:UpdateDisplay()
        cmd_print("position_y set to " .. arg .. ".")
    else
        print("|cff85c5ffScritchPop Commands:")
        print("/scritchpop scale <arg> - sets bar size (default is 1.5)")
        print("/scritchpop lock - toggle bar movement through dragging")
        print("/scritchpop color <arg> - change point color (Blue, Red, Green, Yellow, Purple)")
        print("/scritchpop shape <arg> - change point shape (scritchPop, kitchPop)")
        print("/scritchpop position <arg> <arg> - change position of the anchor")
        print("/scritchpop position_x <arg> - change horizontal position of the anchor")
        print("/scritchpop position_y <arg> - change vertical position of the anchor")
    end
end
