import collections
from datetime import datetime, date

from merc import util
from merc import feature
from merc import message

INFO_TEMPLATE="""
   _ __ ___   ___ _ __ ___
  | '_ ` _ \ / _ \ '__/ __|
  | | | | | |  __/ | | (__ _
  |_| |_| |_|\___|_|  \___(_)

  The Modern Ethereal Relay Chat daemon, version {version}.
  Copyright (C) {year}, #merc-devel

  This software comes with no warranties: see the LICENSE file in the
  source root for details and usage terms.

  Visit us: http://merc-devel.com
  Get the merc source code at: https://github.com/merc-devel/merc

  The following people have contributed significantly to merc,
  in nickname-alphabetic order:
  - rfw <press@rfw.name>
  - Shiz <hi@shiz.me>

  This merc instance has been online since {online_since},
  meaning it has been up for {online_for}!
"""


class InfoFeature(feature.Feature):
  NAME = __name__


install = InfoFeature.install


class InfoReply(message.Reply):
  NAME = "371"
  FORCE_TRAILING = True

  def __init__(self, line):
    self.line = line

  def as_reply_params(self, user):
    return [self.line]

class EndOfInfo(message.Reply):
  NAME = "374"

  def as_reply_params(self, user):
    return ["End of /INFO list"]


@InfoFeature.register_command
class Info(message.Command):
  NAME = "INFO"
  MIN_ARITY = 0

  @message.Command.requires_registration
  def handle_for(self, user, prefix):
    version = util.get_version()
    year = date.today().year
    online_since = user.server.creation_time.strftime("%c")
    online_for = friendly_timespan(datetime.now() - user.server.creation_time)

    lines = INFO_TEMPLATE.format(
        version=version,
        year=year,
        online_since=online_since,
        online_for=online_for)

    for line in lines.splitlines():
      user.send_reply(InfoReply(line))
    user.server.run_hooks("after_info", user)
    user.send_reply(EndOfInfo())

def friendly_timespan(diff, range=3):
  UNITS = collections.OrderedDict([
    ('year',   31536000),
    ('month',  2592000),
    ('week',   604800),
    ('day',    86400),
    ('hour',   3600),
    ('minute', 60),
    ('second', 1)
  ])
  seconds = round(diff.total_seconds())
  indications = []

  for unit, amount in UNITS.items():
    n, seconds = divmod(seconds, amount)
    if n == 0:
      continue
    elif n > 1:
      unit += "s"

    indications.append('{} {}'.format(n, unit))
    if range is not None:
      range -= 1
      if range == 0:
        break

  if len(indications) > 0:
    if len(indications) > 1:
      return ", ".join(indications[:-1]) + " and " + indications[-1]
    return indications[0]
  else:
    return "a small while"

@@ -122,16 +122,17 @@
 essage%0A%0A
+%0A
 INFO_TEM
@@ -140,130 +140,119 @@
 LATE
-=
+ = 
 %22%22%22%0A   
+ 
-_ __ 
+ ____%0A  __/ / /
 ___ 
+_
   ___ _
- 
 __
- 
+__
 ___%0A 
- %7C '_ %60 _
+/_  . __/  '
  %5C
- 
 / 
-_ %5C '
+-_) 
 __/ __
-%7C%0A  %7C %7C %7C %7C %7C %7C  __/ %7C %7C (__ _%0A  %7C_%7C %7C_%7C %7C_%7C%5C___%7C_%7C  %5C___(_)%0A%0A  
+/%0A/_    __/_/_/_/%5C__/_/  %5C__/%0A /_/_/%0A%0A
 The 
@@ -305,18 +305,16 @@
 rsion%7D.%0A
-  
 Copyrigh
@@ -340,18 +340,16 @@
 -devel%0A%0A
-  
 This sof
@@ -358,39 +358,57 @@
 are 
-comes with no warranties: see t
+is licensed under the terms of the MIT license. T
 he L
@@ -422,17 +422,15 @@
 file
- 
+%0A
 in the
-%0A 
  sou
@@ -438,19 +438,29 @@
 ce root 
-for
+contains full
  details
@@ -478,18 +478,16 @@
 terms.%0A%0A
-  
 Visit us
@@ -510,18 +510,61 @@
 vel.com%0A
-  
+Visit us on IRC: #merc @ irc.merc-devel.com%0A%0A
 Get the 
@@ -620,18 +620,16 @@
 l/merc%0A%0A
-  
 The foll
@@ -684,14 +684,12 @@
 erc,
-%0A 
  in
- 
+%0A
 nick
@@ -707,16 +707,18 @@
 etic
+al
  order:%0A
   - 
@@ -717,22 +717,30 @@
 er:%0A
-  - rfw %3Cpress
+%0Arfw, Tony Young %3Ctony
 @rfw
@@ -750,12 +750,8 @@
 me%3E%0A
-  - 
 Shiz
@@ -765,18 +765,16 @@
 iz.me%3E%0A%0A
-  
 This mer
@@ -821,18 +821,16 @@
 _since%7D,
-%0A 
  meaning
@@ -844,17 +844,17 @@
  been up
- 
+%0A
 for %7Bonl

change INFO template a little