Utilisateur:Maxime2024/Module:Infobox Robot

-- Module:Infobox/Robot
-- Module Lua pour l'infobox Robot sur Wikipedia français
-- Créé pour standardiser la présentation des informations sur les robots

local p = {}
local localdata = require('Module:Infobox/Localdata')
local item = localdata.item
local general = require('Module:Infobox/Fonctions')
local wikidata = require('Module:Wikidata')
local convert = require('Module:Conversion')

-- Configuration des couleurs de l'infobox selon le type de robot
local function getTypeColor(robotType)
    local colors = {
        ['Humanoïde'] = '87CEEB',      -- Bleu ciel pour robots humanoïdes
        ['Industriel'] = 'FFA500',      -- Orange pour robots industriels  
        ['Militaire'] = '8B0000',       -- Rouge foncé pour robots militaires
        ['Domestique'] = '32CD32',      -- Vert lime pour robots domestiques
        ['Spatial'] = '4B0082',         -- Indigo pour robots spatiaux
        ['Médical'] = 'FF69B4',         -- Rose pour robots médicaux
        ['Sous-marin'] = '006994',      -- Bleu marine pour robots sous-marins
        ['Aérien'] = 'B0C4DE',          -- Bleu acier pour drones/robots volants
        ['Agricole'] = '228B22',        -- Vert forêt pour robots agricoles
        ['Éducatif'] = 'FFD700'         -- Doré pour robots éducatifs
    }
    return colors[robotType] or 'B0C4DE' -- Couleur par défaut
end

-- Fonction pour formater les unités avec conversion métrique
local function formatUnit(value, unit, precision)
    if not value or value == '' then
        return nil
    end
    
    if unit == 'm' and tonumber(value) then
        return convert.length(value, 'm', precision or 2)
    elseif unit == 'kg' and tonumber(value) then
        return convert.mass(value, 'kg', precision or 1) 
    elseif unit == 'm/s' and tonumber(value) then
        return value .. ' m/s (' .. string.format("%.1f", tonumber(value) * 3.6) .. ' km/h)'
    end
    
    return value
end

-- Fonction pour gérer les liens Wikidata
local function wikidataLink(property, formatting)
    if not item then
        return nil
    end
    return wikidata.formatAndCat({
        entity = item,
        property = property,
        formatting = formatting
    })
end

-- Construction de l'infobox
function p.build()
    local rows = {}
    
    -- En-tête avec nom et image
    table.insert(rows, general.title())
    table.insert(rows, general.mainimage('Article à illustrer Robot'))
    
    -- Informations générales
    if localdata['nom complet'] then
        table.insert(rows, {
            type = 'row',
            label = 'Nom complet',
            value = localdata['nom complet']
        })
    end
    
    -- Classification
    table.insert(rows, general.header('Classification'))
    
    table.insert(rows, {
        type = 'row', 
        label = 'Type',
        value = localdata['type'] or wikidataLink('P31'),
        property = 'P31'
    })
    
    if localdata['sous-type'] then
        table.insert(rows, {
            type = 'row',
            label = 'Sous-type', 
            value = localdata['sous-type']
        })
    end
    
    -- Développement et origine
    table.insert(rows, general.header('Développement'))
    
    table.insert(rows, {
        type = 'row',
        label = 'Concepteur',
        value = localdata['concepteur'] or localdata['inventeur'] or wikidataLink('P287'),
        property = 'P287'
    })
    
    table.insert(rows, {
        type = 'row', 
        label = 'Fabricant',
        value = localdata['fabricant'] or wikidataLink('P176'),
        property = 'P176'
    })
    
    table.insert(rows, {
        type = 'row',
        label = 'Développeur', 
        value = localdata['développeur'] or wikidataLink('P178'),
        property = 'P178'
    })
    
    table.insert(rows, {
        type = 'row',
        label = 'Pays d\'origine',
        value = localdata['pays d\'origine'] or localdata['pays'] or wikidataLink('P17'),
        property = 'P17'
    })
    
    table.insert(rows, {
        type = 'row',
        label = 'Année de création',
        value = localdata['année de création'] or wikidataLink('P571'),
        property = 'P571'
    })
    
    if localdata['première activation'] then
        table.insert(rows, {
            type = 'row',
            label = 'Première activation',
            value = localdata['première activation']
        })
    end
    
    table.insert(rows, {
        type = 'row',
        label = 'Statut',
        value = localdata['statut']
    })
    
    -- Caractéristiques physiques
    table.insert(rows, general.header('Caractéristiques physiques'))
    
    local height = localdata['hauteur'] or localdata['taille'] or wikidataLink('P2048')
    if height then
        table.insert(rows, {
            type = 'row',
            label = 'Hauteur',
            value = formatUnit(height, 'm'),
            property = 'P2048'
        })
    end
    
    local width = localdata['largeur'] or wikidataLink('P2049')  
    if width then
        table.insert(rows, {
            type = 'row',
            label = 'Largeur',
            value = formatUnit(width, 'm'),
            property = 'P2049'
        })
    end
    
    if localdata['profondeur'] then
        table.insert(rows, {
            type = 'row',
            label = 'Profondeur',
            value = formatUnit(localdata['profondeur'], 'm')
        })
    end
    
    local weight = localdata['poids'] or localdata['masse'] or wikidataLink('P2067')
    if weight then  
        table.insert(rows, {
            type = 'row',
            label = 'Poids',
            value = formatUnit(weight, 'kg'),
            property = 'P2067'
        })
    end
    
    local maxSpeed = localdata['vitesse maximale'] or localdata['vitesse max']
    if maxSpeed then
        table.insert(rows, {
            type = 'row',
            label = 'Vitesse maximale',
            value = formatUnit(maxSpeed, 'm/s')
        })
    end
    
    -- Caractéristiques techniques
    table.insert(rows, general.header('Caractéristiques techniques'))
    
    if localdata['source d\'énergie'] or localdata['énergie'] or localdata['alimentation'] then
        table.insert(rows, {
            type = 'row',
            label = 'Source d\'énergie',
            value = localdata['source d\'énergie'] or localdata['énergie'] or localdata['alimentation']
        })
    end
    
    if localdata['autonomie'] then
        table.insert(rows, {
            type = 'row',
            label = 'Autonomie',
            value = localdata['autonomie']
        })
    end
    
    if localdata['degrés de liberté'] then
        table.insert(rows, {
            type = 'row',
            label = 'Degrés de liberté',
            value = localdata['degrés de liberté']
        })
    end
    
    if localdata['articulations'] then
        table.insert(rows, {
            type = 'row',
            label = 'Articulations',
            value = localdata['articulations']
        })
    end
    
    if localdata['capteurs'] then
        table.insert(rows, {
            type = 'row', 
            label = 'Capteurs',
            value = localdata['capteurs']
        })
    end
    
    if localdata['processeur'] then
        table.insert(rows, {
            type = 'row',
            label = 'Processeur', 
            value = localdata['processeur']
        })
    end
    
    if localdata['système d\'exploitation'] or localdata['os'] then
        table.insert(rows, {
            type = 'row',
            label = 'Système d\'exploitation',
            value = localdata['système d\'exploitation'] or localdata['os']
        })
    end
    
    if localdata['intelligence artificielle'] then
        table.insert(rows, {
            type = 'row',
            label = 'Intelligence artificielle',
            value = localdata['intelligence artificielle']
        })
    end
    
    -- Capacités et applications
    table.insert(rows, general.header('Capacités'))
    
    if localdata['capacités'] then
        table.insert(rows, {
            type = 'row',
            label = 'Capacités',
            value = localdata['capacités']
        })
    end
    
    if localdata['applications'] then
        table.insert(rows, {
            type = 'row',
            label = 'Applications',
            value = localdata['applications']
        })
    end
    
    if localdata['environnement'] then
        table.insert(rows, {
            type = 'row',
            label = 'Environnement',
            value = localdata['environnement']
        })
    end
    
    if localdata['charge utile'] then
        table.insert(rows, {
            type = 'row',
            label = 'Charge utile',
            value = formatUnit(localdata['charge utile'], 'kg')
        })
    end
    
    if localdata['précision'] then
        table.insert(rows, {
            type = 'row',
            label = 'Précision', 
            value = localdata['précision']
        })
    end
    
    -- Communication et interaction
    if localdata['communication'] or localdata['langues'] then
        table.insert(rows, general.header('Communication'))
        
        if localdata['communication'] then
            table.insert(rows, {
                type = 'row',
                label = 'Communication',
                value = localdata['communication']
            })
        end
        
        if localdata['langues'] then
            table.insert(rows, {
                type = 'row',
                label = 'Langues supportées',
                value = localdata['langues']
            })
        end
    end
    
    -- Spécificités militaires/sécuritaires
    if localdata['armement'] or localdata['protection'] then
        table.insert(rows, general.header('Spécifications militaires'))
        
        if localdata['armement'] then
            table.insert(rows, {
                type = 'row',
                label = 'Armement',
                value = localdata['armement']
            })
        end
        
        if localdata['protection'] then
            table.insert(rows, {
                type = 'row',
                label = 'Protection',
                value = localdata['protection']
            })
        end
    end
    
    -- Informations économiques et production
    if localdata['coût'] or localdata['nombre produit'] then
        table.insert(rows, general.header('Production'))
        
        if localdata['coût'] then
            table.insert(rows, {
                type = 'row',
                label = 'Coût',
                value = localdata['coût']
            })
        end
        
        if localdata['nombre produit'] then
            table.insert(rows, {
                type = 'row',
                label = 'Nombre produit',
                value = localdata['nombre produit']
            })
        end
    end
    
    -- Relations avec d'autres modèles
    if localdata['prédécesseur'] or localdata['successeur'] or localdata['variantes'] then
        table.insert(rows, general.header('Évolution'))
        
        if localdata['prédécesseur'] then
            table.insert(rows, {
                type = 'row',
                label = 'Prédécesseur',
                value = localdata['prédécesseur']
            })
        end
        
        if localdata['successeur'] then
            table.insert(rows, {
                type = 'row',
                label = 'Successeur',
                value = localdata['successeur']
            })
        end
        
        if localdata['variantes'] then
            table.insert(rows, {
                type = 'row',
                label = 'Variantes',
                value = localdata['variantes']
            })
        end
    end
    
    -- Site web
    table.insert(rows, general.website())
    
    -- Configuration de l'infobox
    local infoboxConfig = {
        maincolor = getTypeColor(localdata['type']),
        secondcolor = '#FFFFFF',
        parts = rows,
        class = 'infobox_v2 infobox-robot',
        wikidata = 'Q11012', -- ID Wikidata pour "robot"
        trackingcat = 'Article utilisant une infobox robot'
    }
    
    return require('Module:Infobox').build(infoboxConfig)
end

-- Fonction appelée par le modèle {{Infobox Robot}}
function p.main(frame)
    localdata = require('Module:Infobox/Localdata')(frame.args, frame:getParent().args)
    return p.build()
end

return p