local p = {}

-- shallow copy di un oggetto table
function table_shallow_copy(obj)
    local copy
    if type(obj) == 'table' then
        copy = {}
        for key, val in pairs(obj) do
            copy[key] = val
        end
    else
        copy = obj
    end
    return copy
end

-- parametri posizionali: {{New Grove|edizione|nome|cognome|voce|volume|pagine|cid|wl}}
-- edizioni: 1, 2, inst, opera, american, art1, art2, jazz1, jazz2

function p.citation(frame)
    
    local wl = frame.args[8] or frame.args['wl'] or Nil
    wl = wl and wl ~= ''
    
    local edition = frame.args[1] or frame.args['edizione'] or Nil
    
    -- shallow copy dei parametri
    local inner_args = table_shallow_copy(frame.args)
    
    -- controllo per parametri posizionali
    inner_args['nome']    = frame.args[2] or frame.args['nome']
    inner_args['cognome'] = frame.args[3] or frame.args['cognome']
    inner_args['voce']    = frame.args[4] or frame.args['voce']
    inner_args['volume']  = frame.args[5] or frame.args['volume']
    inner_args['pagine']  = frame.args[6] or frame.args['pagine']
    inner_args['cid']     = frame.args[7] or frame.args['cid']
        
    -- New Grove ed. 1
    if edition == "1" then
        if wl then
            inner_args['titolo'] = "The New Grove Dictionary of Music and Musicians"
        else
            inner_args['titolo'] = "[[Grove Dictionary of Music and Musicians|The New Grove Dictionary of Music and Musicians]]"
        end
        inner_args['curatore'] = "Stanley Sadie"
        inner_args['edizione'] = "1ª ed."
        inner_args['editore']  = "MacMillian"
        inner_args['città']    = "Londra"
        inner_args['anno']     = "1980"
        inner_args['ISBN']     = "978-0333231111"
        
    -- New Grove ed. 2
    elseif edition == "2" then
        if wl then
            inner_args['titolo'] = "The New Grove Dictionary of Music and Musicians"
        else
            inner_args['titolo'] = "[[Grove Dictionary of Music and Musicians|The New Grove Dictionary of Music and Musicians]]"
        end
        inner_args['curatore']  = "Stanley Sadie"
        inner_args['curatore2'] = "John Tyrrell"
        inner_args['edizione']  = "2ª ed."
        inner_args['editore']   = "Oxford University Press"
        inner_args['anno']      = "2001"
        inner_args['ISBN']      = "978-0195170672"
        
    -- Grove Dictionary of Musical Instruments
    elseif edition == "inst" then
        inner_args['titolo']   = "The New Grove Dictionary of Musical Instruments"
        inner_args['curatore'] = "Stanley Sadie"
        inner_args['editore']  = "MacMillian"
        inner_args['città']    = "Londra"
        inner_args['anno']     = "1984"
        inner_args['ISBN']     = "0943818052"
        
    -- Grove Dictionary of Opera
    elseif edition == "opera" then
        if wl then
            inner_args['titolo'] = "[[New Grove Dictionary of Opera|The New Grove Dictionary of Opera]]"
        else
            inner_args['titolo'] = "The New Grove Dictionary of Opera"
        end
        inner_args['curatore'] = "Stanley Sadie"
        inner_args['edizione'] = "1ª ed."
        inner_args['editore']  = "MacMillian"
        inner_args['città']    = "Londra"
        inner_args['anno']     = "1992"
        inner_args['ISBN']     = "0333485521"
        
    -- Grove Dictionary of American Music
    elseif edition == "american" then
        inner_args['titolo']   = "The New Grove Dictionary of American Music"
        inner_args['curatore'] = "Stanley Sadie"
        inner_args['curatore'] = "Hugh Wiley Hitchcock"
        inner_args['editore']  = "MacMillian"
        inner_args['città']    = "Londra"
        inner_args['anno']     = "1986"
        inner_args['ISBN']     = "0333378792"
        
    -- Grove Dictionary of Art ed. 1
    elseif edition == "art1" then
        inner_args['titolo']    = "The Grove Dictionary of Art"
        inner_args['curatore']  = "Jane Turner"
        inner_args['edizione']  = "1ª ed."
        inner_args['editore']   = "Oxford University Press"
        inner_args['anno']      = "1996"
        inner_args['ISBN']      = "1884446000"
        
    -- Grove Dictionary of Art ed. 2
    elseif edition == "art2" then
        inner_args['titolo']    = "The Grove Dictionary of Art"
        inner_args['curatore']  = "Jane Turner"
        inner_args['edizione']  = "2ª ed."
        inner_args['editore']   = "Oxford University Press"
        inner_args['anno']      = "2003"
        inner_args['ISBN']      = "0195170687"
    
    -- Grove Dictionary of Jazz ed. 1
    elseif edition == "jazz1" then
        inner_args['titolo']    = "The New Grove Dictionary of Jazz"
        inner_args['curatore']  = "Barry Kernfeld"
        inner_args['edizione']  = "1ª ed."
        inner_args['editore']   = "MacMillian"
        inner_args['anno']      = "1988"
        inner_args['ISBN']      = "0312113579"
    
    -- Grove Dictionary of Jazz ed. 2
    elseif edition == "jazz2" then
        inner_args['titolo']    = "The New Grove Dictionary of Jazz"
        inner_args['curatore']  = "Barry Kernfeld"
        inner_args['edizione']  = "2ª ed."
        inner_args['editore']   = "Oxford University Press"
        inner_args['anno']      = "2003"
        inner_args['ISBN']      = "1561592846"
    end
    
    -- oggetto ambiente per la chiamata del Modulo:Citazione
    local cite_object = {
        cite_module = require("Modulo:Citazione"),
        args = inner_args,
        frame = {
            args = inner_args
        }
    }
    function cite_object.getParent()
        return cite_object.frame
    end
    
    -- chiama funzione dal Modulo:Citazione
    return cite_object.cite_module.citation(cite_object)
end

return p