-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
-- DO NOT USE WITHOUT PERMISSION

do

local xml = require("xml") 

local user = cred_data.mal_username
local password = cred_data.mal_pw

local BASE_URL = 'http://'..user..':'..password..'@myanimelist.net/api/anime'

local function delete_tags(str)
  str = string.gsub( str, '<br />', '')
  str = string.gsub( str, '%[i%]', '')
  str = string.gsub( str, '%[/i%]', '')
  str = string.gsub( str, '&mdash;', ' — ')
  return str
end

local makeOurDate = function(dateString)
  local pattern = "(%d+)%-(%d+)%-(%d+)"
  local year, month, day = dateString:match(pattern)
  return day..'.'..month..'.'..year
end

local function get_anime_info(anime)
  local url = BASE_URL..'/search.xml?q='..anime
  local res,code  = http.request(url)
  if code ~= 200 then return "HTTP-Fehler" end
  local result = xml.load(res)
  return result
end

local function send_anime_data(result, receiver)
  local title = xml.find(result, 'title')[1]
  local id = xml.find(result, 'id')[1]
  local mal_url = 'http://myanimelist.net/anime/'..id
  
  if xml.find(result, 'type')[1] then
    typ = ' ('..xml.find(result, 'type')[1]..')'
  else
    typ = ''
  end
  
  if xml.find(result, 'synonyms')[1] then
    syno = '\nAlternativ: '..unescape(delete_tags(xml.find(result, 'synonyms')[1]))
  else
    syno = ''
  end
  
  if xml.find(result, 'english')[1] then
    eng = '; '..unescape(delete_tags(xml.find(result, 'english')[1]))
  else
    eng = ''
  end

  if xml.find(result, 'episodes')[1] then
    episodes = '\nEpisoden: '..xml.find(result, 'episodes')[1]
  else
    episodes = ''
  end
  
  if xml.find(result, 'status')[1] then
    status = ' ('..xml.find(result, 'status')[1]..')'
  else
    status = ''
  end
  
  if xml.find(result, 'score')[1] ~= "0.00" then
    score = '\nScore: '..xml.find(result, 'score')[1]
  else
    score = ''
  end
 
  if xml.find(result, 'start_date')[1] ~= "0000-00-00" then
    startdate = '\nAusstrahlung: '..makeOurDate(xml.find(result, 'start_date')[1])
  else
    startdate = ''
  end
 
  if xml.find(result, 'end_date')[1] ~= "0000-00-00" then
    enddate = ' - '..makeOurDate(xml.find(result, 'end_date')[1])
  else
    enddate = ''
  end
  
  if xml.find(result, 'synopsis')[1] then
    desc = '\n'..unescape(delete_tags(string.sub(xml.find(result, 'synopsis')[1], 1, 200)))..'...'
  else
    desc = ''
  end
 
  local text = title..typ..syno..eng..episodes..status..score..startdate..enddate..'\n'..desc..'\n'..mal_url
  if xml.find(result, 'image') then
    local image_url = xml.find(result, 'image')[1]
    local cb_extra = {
      receiver=receiver,
      url=image_url
    }
    send_msg(receiver, text, send_photo_from_url_callback, cb_extra)
  else
    send_msg(receiver, text, ok_cb, false)
  end 
end

local function run(msg, matches)
  local anime = URL.escape(matches[1])
  local receiver = get_receiver(msg)
  local anime_info = get_anime_info(anime)
  if anime_info == "HTTP-Fehler" then
    return "Anime nicht gefunden!"
  else
    send_anime_data(anime_info, receiver)
  end
end

return {
  description = "Sendet Infos zu einem Anime.", 
  usage = "/anime [Anime]: Sendet Infos zum Anime",
  patterns = {"^/anime (.+)$",
  			"^/mal (.*)$",
  			"^/myanimelist (.*)$)",
  			"^https?://myanimelist.net/anime/[0-9]+/(.*)$"
  			},
  run = run 
}

end