lib_date = require('Module:Date')
local p = {}


function p.year(frame)
  if frame ~= nil then
    if frame.args[1] ~= nil then page = mw.text.trim(frame.args[1]) end
    if frame.args[2] ~= nil then start = tonumber(mw.text.trim(frame.args[2])) end
    if frame.args[3] ~= nil then ending = tonumber(mw.text.trim(frame.args[3])) end
    if frame.args[4] ~= nil then
      separator = mw.text.trim(frame.args[4])
      if separator == "grid" then
        separator = "||"
      end
    end
  end
  if start == nil or start == "" then start = 2003 end
  if ending == nil or ending == "" then ending = tonumber(os.date("%Y")) end
  if separator == nil then separator = "" end

  local YearsList = {}
  if separator == "||" then 
    table.insert(YearsList, '{| cellpadding="5" style="border:1px solid #aaaaaa; margin-top:1em; margin-bottom:0.5em" rules="all" align="center"\n|')
  elseif separator == "image" then
    table.insert(YearsList, "<ul>")
    tag1 = '<li style="float:left;margin:2px 2px 2px 8px;display:inline;">[[Image:Nuvola devices nfs mount.png|30px|link='
    tag2 = "]]<br/>"
    tag3 = "</li>"
  end

  while start <= ending do
    link = page .. "/" .. tostring(start)
    text = tostring(start)
    if separator == "image" then
      table.insert(YearsList, tag1 .. link .. tag2 .. "[[" .. link .. "|" .. text .. "]]" .. tag3)
    else
      table.insert(YearsList, "[[" .. link .. "|" .. text .. "]]")
    end
    start = start + 1
  end

  if separator == "||" then
    table.insert(YearsList, "\n|}\n")
  elseif separator == "image" then
    table.insert(YearsList, "</ul>\n")
    separator = ""
  end

  return table.concat(YearsList, separator .. " ")
end

function p.month(frame)
  return p.Period("month", frame)
end

function p.week(frame)
  return p.Period("week", frame)
end

function p.Period(unite, frame)
  if frame ~= nil then
    if frame.args[1] ~= nil then page = mw.text.trim(frame.args[1]) end
    if frame.args[2] ~= nil then
      StartYear = mw.ustring.gsub(frame.args[2], "(.+)\-(.+)", "%1")
      StartYear = tonumber(StartYear)
      StartPeriod = mw.ustring.gsub(frame.args[2], "(.+)\-(.+)", "%2")
      StartPeriod = tonumber(StartPeriod)
    end
    if frame.args[3] ~= nil then
      YearLimit = mw.ustring.gsub(frame.args[3], "(.+)\-(.+)", "%1")
      YearLimit = tonumber(YearLimit)
      EndPeriod = mw.ustring.gsub(frame.args[3], "(.+)\-(.+)", "%2")
      EndPeriod = tonumber(EndPeriod)
    end
    if frame.args[4] ~= nil then
      if mw.text.trim(frame.args[4]) == "grid" then
        separator = "||"
      else
        separator = mw.text.trim(frame.args[4])
      end
    else
      separator = ""
    end
    if frame.args[5] ~= nil then
      if mw.text.trim(frame.args[5]) == "" then
        format = "name"
      else
        format = mw.text.trim(frame.args[5])
      end
    else
      format = "name"
    end
  end
  if StartYear == nil or StartYear == "" then
    StartYear = 2003
  end
  if StartPeriod == nil or StartPeriod == "" then
    StartPeriod = 01
  end
  if YearLimit == nil or YearLimit == "" then
    YearLimit = tonumber(os.date("%Y"))
  end
  if unite == "week" then
    EndLimit = tonumber(52)
    EndPeriodTemp = tonumber(os.date("%W"))
  else
    EndLimit = tonumber(12)
    EndPeriodTemp = tonumber(os.date("%m"))
  end
  if EndPeriod == nil or EndPeriod == "" then EndPeriod = EndPeriodTemp end

  local ListePeriod = ""
  if separator == "||" then 
    ListePeriod = '{| cellpadding="5" style="border:1px solid #aaaaaa; margin-top:1em; margin-bottom:0.5em" rules="all" align="center"\n|' .. "'''" .. StartYear .. "'''||"
  elseif separator == "image" then
    ListePeriod = "<ul>"
    tag1 = '<li style="float:left;margin:2px 2px 2px 8px;display:inline;">[[Image:Nuvola devices nfs mount.png|30px|link='
    tag2 = "]]<br/>"
    tag3 = "</li>"
  end
  while StartYear <= YearLimit do
    while StartPeriod <= EndLimit do

      if unite == "week" then
        link = page .. "/week " .. StartPeriod .. " " .. tostring(StartYear)
        if separator == "||" then
          text = StartPeriod
        else
          text = StartPeriod .. " " .. tostring(StartYear)
        end

      elseif format == "name" then
        link = page .. "/" .. lib_date.monthName(StartPeriod) .. " " .. tostring(StartYear)
        if separator == "||" then
          text = lib_date.monthName(StartPeriod)
        else
          text = lib_date.monthName(StartPeriod) .. " " .. tostring(StartYear)
        end

      elseif format == "abbreviation" then
        link = page .. "/" .. lib_date.monthName(StartPeriod) .. " " .. tostring(StartYear)
        text = mw.text.truncate(lib_date.monthName(StartPeriod),3)
      else
        link = page .. "/" .. tostring(StartYear) .. "/" .. tostring(StartPeriod)
        text = tostring(StartYear) .. "/" .. tostring(StartPeriod)
      end

      if separator == "image" then
        ListePeriod = ListePeriod .. tag1 .. link .. tag2 .. "[[" .. link .. "|" .. text .. "]]" .. tag3
      else
        ListePeriod = ListePeriod .. "[[" .. link .. "|" .. text .. "]]" 
      end

      if StartYear == YearLimit and StartPeriod == EndPeriod then
        StartPeriod = EndLimit
      elseif StartPeriod < EndLimit then
        if separator ~= "image" then
          ListePeriod = ListePeriod .. separator .. " "
        end 	
      end
      StartPeriod = StartPeriod + 1
    end
    
    if StartYear < YearLimit or StartPeriod < EndPeriod then
      -- Year changing unless for the very last line
      if separator == "||" then 
        ListePeriod = ListePeriod .. "\n|-\n|'''" .. StartYear+1 .. "'''||"
      else
        ListePeriod = ListePeriod .. "\n\n"
      end
    end
    StartPeriod = 1
    StartYear = StartYear + 1
  end

  if separator == "||" then
    ListePeriod = ListePeriod .. "\n|}\n"
  elseif separator == "image" then
    ListePeriod = ListePeriod .. "</ul>\n"
  end

  return ListePeriod
end


return p