-- Modul ini masih lagi dalam pembangunan. Harap ia tidak dibuang.
-- Ce module implémente [[Modèle:Wikiprojet/Projet]], pour avoir une structure html plus simple.

local p = {}

local dataAvancement = {
	default = {
		nom = 'tidak diketahui',
		couleur = '#F5F5F5; white-space:nowrap;',
		texte = 'Belum dinilai',
	},
	ap = {
		nom = 'AP',
		couleur = '#6699FF',
		texte = '[[Berkas:Symbol star FA gold.svg|30px|AP]]<br/><abbr class="abbr" title="Artikel pilihan">AP</abbr>',
	},
	ab = {
		nom = 'AB',
		couleur = '#66FF66',
		texte = '[[Berkas:Symbol star FA GA.svg|30px|AB]]<br/><abbr class="abbr" title="Artikel baik">AB</abbr>',
	},
	dp = {
		nom = 'DP',
		couleur = '#6699FF',
		texte = '[[Berkas:Symbol star FA.svg|30px|DP]]<br/><abbr class="abbr" title="Daftar pilihan">DP</abbr>',
	},
	a = {
		nom = 'A',
		couleur = '#66FFFF',
		texte = '[[Berkas:Symbol A class.svg|30px|DP]]<br/><abbr class="abbr" title="Artikel kelas A">A</abbr>',
	},
	b = {
		nom = 'B',
		couleur = '#B2FF66',
		texte = '<abbr class="abbr" title="Artikel kelas B">B</abbr>',
	},
	c = {
		nom = 'C',
		couleur = '#FFFF66',
		texte = '<abbr class="abbr" title="Artikel kelas C">C</abbr>',
	},
	awal = {
		nom = 'Awal',
		couleur = '#FFAA66',
		texte = '<abbr class="abbr" title="Artikel kelas awal">Awal</abbr>',
	},
	['rintisan'] = {
		nom = 'Rintisan',
		couleur = '#FF6666',
		texte = '<abbr class="abbr" title="Artikel kelas rintisan">Rintisan</abbr>',
	},
	['daftar'] = {
		nom = 'Daftar',
		couleur = '#AA88FF',
		texte = '<abbr class="abbr" title="Artikel kelas daftar">Daftar</abbr>',
	},
	['mendatang'] = {
		nom = 'mendatang',
		couleur = '#8A95C9',
		texte = '<abbr class="abbr" title="Artikel kelas mendatang">Mendatang</abbr>',
	},
	['buku'] = {
		nom = 'Buku',
		couleur = '#98FF98',
		texte = '<abbr class="abbr" title="Artikel kelas buku">Buku</abbr>',
	},
	['kategori'] = {
		nom = 'Kategori',
		couleur = '#FFDB58',
		texte = '<abbr class="abbr" title="Artikel kelas kategori">Kategori</abbr>',
	},
	['disambig'] = {
		nom = 'Disambig',
		couleur = '#00FA9A',
		texte = '<abbr class="abbr" title="Artikel kelas disambig">Disambig</abbr>',
	},
	mp = {
		nom = 'MP',
		couleur = '#6699FF',
		texte = '[[Berkas:Symbol star FA gold.svg|30px|MP]]<br/><abbr class="abbr" title="Media pilihan">MP</abbr>',
	},
	['berkas'] = {
		nom = 'Berkas',
		couleur = '#DDCCFF',
		texte = '<abbr class="abbr" title="Artikel kelas berkas">Berkas</abbr>',
	},
	['portal'] = {
		nom = 'Portal',
		couleur = '#CC8899',
		texte = '<abbr class="abbr" title="Artikel kelas portal">Portal</abbr>',
	},
	['proyek'] = {
		nom = 'Proyek',
		couleur = '#C0C090',
		texte = '<abbr class="abbr" title="Artikel kelas proyek">Proyek</abbr>',
	},
	['pengalihan'] = {
		nom = 'Pengalihan',
		couleur = '#C0C0C0',
		texte = '<abbr class="abbr" title="Artikel kelas pengalihan">Pengalihan</abbr>',
	},
	['templat'] = {
		nom = 'Templat',
		couleur = '#FBCEB1',
		texte = '<abbr class="abbr" title="Artikel kelas templat">Templat</abbr>',
	},
}
--dataAvancement['awal'] = dataAvancement.bd
--dataAvancement.c = dataAvancement.bd
--dataAvancement.d = dataAvancement.bd
--dataAvancement.e = dataAvancement['rintisan']

local dataImportance = {
	default = {
		nom = 'belum dinilai',
		couleur = '#F5F5F5; white-space:nowrap;',
		texte = 'Belum dinilai',
	},
	sangat = {
		nom = 'sangat penting',
		couleur = '#FF00FF',
		texte = "sangat penting",
	},
	penting = {
		nom = 'penting',
		couleur = '#FF88FF',
		texte = "penting",
	},
	cukup = {
		nom = 'cukup penting',
		couleur = '#FFCCFF',
		texte = "cukup penting",
	},
	kurang = {
		nom = 'kurang penting',
		couleur = '#FFEEFF',
		texte = "kurang penting",
	},
	na = {
		nom = 'tidak dinilai',
		couleur = '#FFFFFF',
		texte = "tidak dinilai",
	},
}

local introduction = '<div class="intro-wikiprojet" style="margin-bottom:1em"><p>Halaman ini merupakan bagian dari %s %s.</p>'
	.. '<p>[[Wikipedia:ProyekWiki|ProyekWiki]] adalah sebuah proyek kerja sama antarpengguna untuk memperbaiki dan meningkatkan kualitas artikel yang berkaitan di Wikipedia. '
	.. 'Anda boleh [%s menyunting halaman ini] '
	.. 'atau menuju ke halaman proyek jika anda ingin melakukannya. '
	.. 'Anda juga dapat menuju ke halaman pembicaraan jika Anda memiliki pertanyaan atau pendapat.</p></div>'
local lienProjet = '[[Wikipedia:ProyekWiki %s|%s]]'
local liensProjet = '<b>[[Wikipedia:ProyekWiki ¤|¤]]</b> ([[Pembicaraan Wikipedia:ProyekWiki ¤|pembicaraan]]'
	.. '\194\160• [[Wikipedia:ProyekWiki ¤/Penilaian|kriteria]])'
--local chaineCategorie = ""

local function switch( key, table )
	return table[key] or table['default']
end

local function analyseArgs( args )
	local newArgs = {
		[0] = {},  -- pour éviter une erreur sur i-1
		avancement = switch( mw.ustring.lower( args.avancement or '' ), dataAvancement ),
		nocat = ( args.nocat and args.nocat ~= '' ),
	}
	local frame = mw.getCurrentFrame()
	local i = 1
	while args[i] and mw.text.trim( args[i] ) ~= '' do
		local nomProjet = frame:expandTemplate{
			title = 'ProyekWiki/alias',
			args = { projet = args[i] }
		}
		local projet = {
			importance = switch( mw.ustring.lower( mw.text.trim( args[ i + 1 ] or '' ) ), dataImportance ),
			nom = nomProjet,
			image = frame:expandTemplate{
				title = 'ProyekWiki/gambar',
				args = { projet = nomProjet }
			},
			nomCategorie = frame:expandTemplate{
				title = 'ProyekWiki/kategorisasi',
				args = { projet = nomProjet }
			},
			extraCategorie = frame:expandTemplate{
				title = 'ProyekWiki/ekstra',
				args = { projet = nomProjet }
			},
		}
		--if projet.image == '' then
		--	local bandeau = require 'Module:InfoboxImage'
		--	local icone = bandeau.parametreEbauche( frame:newChild { args = { nomProjet, 'icone' } } )
		--	projet.image = icone or 'Emojione 2754.svg'
			--projet.extraCategorie = projet.extraCategorie .. '[[Catégorie:Page avec image Wikiprojet déduite|' .. nomProjet .. ']]'
		--end
		table.insert( newArgs, projet )
		i = i + 2
	end
	return newArgs
end

function p.introduction( args )
	local sp = 'proyek'
	
	local listeProjets = {}
	for i = 1, #args do
		local nom = args[i].nom
		table.insert( listeProjets, string.format( lienProjet, nom, nom ) )
	end
	return string.format( introduction,
		sp,
		mw.text.listToText( listeProjets ),
		mw.title.getCurrentTitle():fullUrl( 'action=edit' )
	)
end

function p.tableauAvancement( args )
	local pageName = mw.title.getCurrentTitle().text
	local titreAvancement = {
		default = '[[:Kategori:Artikel menurut skala kualitas|Skala kualitas]]',
		AP = '[[Wikipedia:Artikel pilihan|Skala kualitas]]',
		AB = '[[Wikipedia:Artikel baik|Skala kualitas]]]',
		DP = '[[Wikipedia:Daftar pilihan|Skala kualitas]]]'
	}
	local evalTable = mw.html.create( 'table' )
	evalTable
		-- le "border-collapse:separate" est pour la version mobile
		:cssText( 'background:#ededed; width:100%; border-spacing:0; padding:0.4em 0.6em calc(0.4em + 2px); border:1px solid #c0c090; border-radius:5px; border-collapse:separate;' )
		:tag( 'caption' )
			:cssText( 'padding-bottom:5px;' )
			:wikitext( '[[Berkas:Star half by Amirreza.png|20px|alt=Logo penilaian]] <b>[[Wikipedia:ProyekWiki/Penilaian|Penilaian]]</b> halaman: <b>' )
			:wikitext( mw.text.nowiki( pageName ) )
			:wikitext( '</b>' )
			:done()
		:tag( 'tr' )
			:tag( 'th' )
				:attr{ scope = 'col' }
				:cssText( 'width:6em; font-weight:normal;' )
				:wikitext( switch( args.avancement.nom, titreAvancement ) )
				:done()
			:tag( 'th' )
				:attr{ scope = 'col' }
				:cssText( 'width:6em; font-weight:normal;' )
				:wikitext( '[[:Kategori:Artikel menurut skala kepentingan|Skala kepentingan]]' )
				:done()
			:tag( 'th' )
				:attr{ scope = 'col', colspan = 2 }
				:cssText( 'text-align:left; font-weight:normal;' )
				:wikitext( '[[Berkas:WikiProject Council.svg|14px|DPW]] [[Wikipedia:ProyekWiki|ProyekWiki]]' )
		:allDone()
	local ligne = mw.html.create( 'tr' )
			:tag( 'td' )
				:attr( 'rowspan', #args )
				:cssText( 'text-align:center; font-weight:bold; background-color:' .. args.avancement.couleur )
				:wikitext( args.avancement.texte )
			:done()

	for numProjet, projet in ipairs( args ) do
		local importance = projet.importance
		if projet.importance ~= args[numProjet-1].importance then
			local span = 1
			while numProjet + span <= #args
				and args[numProjet + span].importance == projet.importance
			do
				span = span + 1
			end
			ligne
				:tag( 'td' )
					:attr( 'rowspan', span )
					:cssText( 'text-align:center; font-weight:bold; background-color:' .. projet.importance.couleur )
					:wikitext( projet.importance.texte )
					:done()
		end

		--local categories = ''
		--if not args.nocat then
		--	categories = string.format( chaineCategorie,
		--		args.avancement.nom,
		--		projet.nomCategorie,
		--		args.avancement.nom,
		--		projet.nomCategorie,
		--		projet.importance.nom,
		--		projet.extraCategorie
		--	)
		--end

		ligne
			:tag( 'td' )
				:cssText( 'text-align:center; width:40px' )
				:wikitext( string.format( '[[Berkas:%s|40x20px|%s]]', projet.image, projet.nom ) )
				:done()
			:tag( 'td' )
				:wikitext( ( string.gsub( liensProjet, '¤', projet.nom ) ) )
			--	:wikitext( categories )
				:done()

		evalTable:node( ligne )
		ligne =  mw.html.create( 'tr' )
	end
	if #args == 0 then
		evalTable:node( ligne )
	end
	return tostring( evalTable )
end


function p.projet( frame )
	local args
	if type( frame.getParent ) == 'function' then
		args = analyseArgs( frame:getParent().args )
	else
		args = analyseArgs( frame.args )  -- pour tester dans la console
	end
	if args[1] then
		return p.introduction(args) .. p.tableauAvancement( args )
	elseif args.avancement then
		return p.tableauAvancement( args )
	end
end

return p