Documentation for this module may be created at Module:Quotes/doc

local p = {}

function p.render(frame)
    local args = frame:getParent().args
    local title = args.Title or "Quotes"
    local icon = args.Icon or "Quote.png"
    local bg = args.bg or "#b06767"
    local bg2 = args.bg2 or "#9199a6"

    bg = frame:expandTemplate{title = "Color", args = {bg}}
    bg2 = frame:expandTemplate{title = "Color", args = {bg2}}

    local rows = {}
    local i = 1
    local nihongoJapaneseFound = false

    -- Iterating over the arguments to collect rows and check for nihongo-japanese class
    while args[i] do
        local row = args[i]
        table.insert(rows, row)
        if string.find(row, 'class="[^"]*nihongo%-japanese[^"]*"') then
            nihongoJapaneseFound = true
        end
        i = i + 1
    end

    local boxArgs = {
        Title = title,
        Icon = icon,
        bg = bg,
        bg2 = bg2,
        height = '400px'
    }

    for i, row in ipairs(rows) do
        boxArgs["r"..i] = row
    end

    -- Only add buttonsHtml if nihongoJapaneseFound is true
    local buttonsHtml = ''
    if nihongoJapaneseFound then
        buttonsHtml = '<div class="quote-buttons" style="text-align:center; background-color:' .. bg2 .. ';">'
        buttonsHtml = buttonsHtml .. '<span class="quote-button mw-ui-button" id="showAllButton">Show All</span>'
        buttonsHtml = buttonsHtml .. '<span class="quote-button mw-ui-button" id="showEnglishButton">English</span>'
        buttonsHtml = buttonsHtml .. '<span class="quote-button mw-ui-button" id="showJapaneseButton">Japanese</span>'
        buttonsHtml = buttonsHtml .. '<span class="quote-button mw-ui-button" id="showRomajiButton">Romaji</span>'
        buttonsHtml = buttonsHtml .. '</div>'
    end

    local boxHtml = frame:expandTemplate{title = "Box", args = boxArgs}

    local finalHtml = '<div class="quoteBox boxTemplate">'
    finalHtml = finalHtml .. boxHtml
    finalHtml = finalHtml:gsub('(<div class="boxTemplate2 volume".->)', '%1' .. frame:preprocess(buttonsHtml))
    finalHtml = finalHtml .. '</div>'

    return finalHtml
end

return p