local p = {}

function trim(s)
    return (mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1"))
end

function xtrim(s,v)
  local a = trim(s or '');
  if a and a ~= '' then return a ; else return v; end;
end

function p.open(options)
       local id = options['id']

       if id ~= nil
        	then id = ' id="'..id..'"'
    	else
    		id= ''
		end
		
       return '<table cellspacing="3px" class="infobox_v2" style="width:22em"'..id..'>'
end

function p.close(options)
	return '</table>'
end

function p.title(options)

       local colspan= tonumber( options[5] ) or  2;
       local class_td = trim(options[3] or 'entete map');
       local bg_color = trim(options[2] or 'E1E1E1');
       local txt_color = trim(options[4] or '000000');

       local res =  mw.html.create( 'tr' )
       			:tag('th')
        			:addClass(class_td)
        			:attr('scope', 'col') 
        			:css('line-height', '2em')
        			:css('background-color', '#'..bg_color)
        			:css('color', '#'..txt_color)
        			:attr('colspan', colspan)
        			:wikitext(trim(options[1]))
        			:done()
        			
        return tostring(res)
end

function p.icoTitle(options)
       local colspan= tonumber( options[5] ) or 2;
       local icon =trim(options[3] or 'Nuvola apps important.png');
       local bg_color =trim(options[2] or 'E1E1E1');
       local txt_color =trim(options[4] or '000000');
       local aright = trim(options['right'] or '');
       local ctxt1 = ''
	   local ctxt2 = ''
       
       if aright ~= ''
       		then
       			ctxt1 = '[[file:'..icon..'|45x45px|center|alt=|link=]]';
       			ctxt2 = '[[file:Pix.gif|45x45px|center|alt=|link=]]';
   			else
   				ctxt1 = '[[file:Pix.gif|45x45px|center|alt=|link=]]';
   				ctxt2 = '[[file:'..icon..'|45x45px|center|alt=|link=]]';
   			end
   			
       local res =  mw.html.create( 'tr' )
       			:tag('th')
        			:attr('colspan', colspan)
        				:tag('table')
        					:attr('cellpadding', 0)
        					:attr('cellspacing', 0)
        					:attr('width', '100%')
        					:css('text-align', 'center')
        					:tag('tr')
        						:addClass('entete defaut')
        						:css('background-color', '#'..bg_color)
        						:tag('td')        			
        								:attr('width', '10%')         			
        								:wikitext(ctxt1)
        								:done()
        						:tag('td')        			
        								:attr('width', '80%')         			
        								:css('color', '#'..txt_color)        			
        								:wikitext(trim(options[1]))
        								:done()
        						:tag('td')        			
        								:attr('width', '10%')
        					       		:wikitext(ctxt2)
        					       		:done()
        					    :done() --tr
        					:done() -- tab
        			:done() --th
        		:done() --tr
        return tostring(res)
end

function p.subTitle(options)
       local colspan=  options[5] or 2;
       local bg_color =trim(options[3] or 'E1E1E1');
       local txt_color =trim(options[4] or '000000');
       local showTitle = trim(options[2] or '');

       if showTitle ~= ''
       	then 
       		res = mw.html.create( 'tr' )
       			:tag('th')
        			:attr('scope', 'col') 
        			:css('background-color', '#'..bg_color)
        			:css('color', '#'..txt_color)
        			:css('text-align', 'center')
        			:attr('colspan', colspan)
        			:wikitext(trim(options[1]))
        			:done()
        			
        	return tostring(res)    
        else 
        	return ''
	   end
end

function p.line(options)

       local colspan= tonumber( options[6] ) or 2;
       local bg_color =xtrim(options[4] , 'F9F9F9');
       local txt_color =xtrim(options[5] , '000000');
       local showLine = trim(options[1] or '');
       local content = trim(options[2] or '');
       local textAlign = xtrim(options[3] , 'center');
       local lineHeight = tonumber( options[7] ) or 0;
       
       if content == ''
         then content = showLine ;
       end;
       	
       if showLine ~= ''
       	 then 
       		res = mw.html.create( 'tr' )
       			:tag('td')
        			:attr('scope', 'col') 
        			:css('background-color', '#'..bg_color)
        			:css('color', '#'..txt_color)
        			:css('text-align', textAlign)
        			:attr('colspan', colspan)
        			:wikitext(content)
        			:done()
        	
        	if lineHeight~=0
        	  then 
        	  		res:tag('tr'):tag('td'):css('height', lineHeight..'px')
        	  end
        	return tostring(res)    
    	else
    		return ''
		end
end

function p.mixedLine(options)
       local colspan= tonumber( options[5] ) or '0';
       local bg_color =xtrim(options[6] , 'F3F3F3');
       local txt_color =xtrim(options[7] , '000000');
       local showLine = trim(options[2] or '');
       local title = trim(options[1] or '');
       local content = trim(options[3] or '');
       local width = tonumber(options[4]) or 0;

       if content == ''
         then content = showLine ;
       end;
       	
       if showLine ~= ''
       	 then 
       		local res = mw.html.create( 'tr' )
       		local resth = mw.html.create('th')
        			:attr('scope', 'row') 
        			:css('background-color', '#'..bg_color)
        			:css('color', '#'..txt_color)
        			:css('text-align', 'right')
        			:wikitext(title)
        			:done()
        	local restd = mw.html.create( 'td')
        	--		:css('background-color', '#'..bg_color)
        	--		:css('color', '#'..txt_color)
        			:wikitext(content)
        			:done()
        	
        	if width~=0
        	  then 
        	  		resth:attr('width', width..'%')
        	  end        	
        	if colspan~=0
        	  then 
        	  		restd:attr('colspan', colspan)
        	  end
        	res:node(resth)
        	res:node(restd)
        	return tostring(res)    
    	else
    		return ''
		end
end

function p.prevNextLine(options)
       local dprev = trim(options[1] or '');
       local dnext = trim(options[2] or ''); 
       local colspan = tonumber( options[5] ) or '2';
       local bgc1 = 'transparent';
       local bgc2 = 'transparent';
       local content1 = '';
       local content2 = '';
       local link1 = trim(options[3] or '');
       local link2 = trim(options[4] or ''); 
       local arrowSize = tonumber(options[5]) or 13;
       
       if dprev == '' and dnext == '' then return ''; end;
       if dprev ~= '' 
       	then 
       			bgc1 = '#E6E6E6'; 
       			content1 = '[[File:Fleche-defaut-droite.png|'..arrowSize..'px|link='..link1..']]'
       	end;
       if dnext ~= '' 
       	then 
       			bgc2 = '#E6E6E6'; 
       			content2 = '[[File:Fleche-defaut-gauche.png|'..arrowSize..'px|link='..link2..']]'
       	end;

       res = mw.html.create( 'tr' )
       		:tag('td')
       			:attr('colspan',colspan) 
       			:attr('align','center')
       			:tag('table')
        			:css('background-color', '#eeeeee')
        			:css('margin', '0 0 0 0')
        			:css('border-collapse', 'collapse')
        			:attr('cellspacing', 0)
        			:attr('width', '100%')
        			:tag('tr')
        				:tag('td')
        					:attr('width', '50%')
        					 :tag('table')
        						:css('background-color', bgc1)
        						:css('margin', '0 0 0 0')
        						:css('border-collapse', 'collapse')
        						:attr('cellspacing', 0)
        						:attr('width', '100%')
        						:tag('tr')
        							:tag('td')
        								:attr('width', '20')
        								:attr('align', 'right')
        								:wikitext(content1)
        								:done()
        							:tag('td')
        								:css('text-align', 'right')
        								:css('font-size', '80%')
        								:wikitext(dprev)
        								:done()
        						:done()
        					:done()
        				 :done()
        				:tag('td')
        					:attr('width', '50%')
        					 :tag('table')
        						:css('background-color', bgc2)
        						:css('margin', '0 0 0 0')
        						:css('border-collapse', 'collapse')
        						:attr('cellspacing', 0)
        						:attr('width', '100%')
        						:tag('tr')
        							:tag('td')
        								:css('text-align', 'left')
        								:css('font-size', '80%')
        								:wikitext(dnext)
        								:done()
        							:tag('td')
        								:attr('width', '20')
        								:attr('align', 'left')
        								:wikitext(content2)
        								:done()
        							:done() -- tr
        						:done() --tab
        					:done()--td
        				:done() -- tr
        			:done() --tab
        		:done() -- td
      return tostring(res) 
end
   
return p