-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
-- DO NOT USE WITHOUT PERMISSION
-- Modified by Akamaru [https://ponywave.de]

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/manga'

local function delete_tags(str)
  str = string.gsub( str, '<br />', '')
  str = string.gsub( str, '%[i%]', '')
  str = string.gsub( str, '%[/i%]', '')
  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_manga_info(manga)
  local url = BASE_URL..'/search.xml?q='..manga
  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_manga_data(result, receiver)
  local title = xml.find(result, 'title')[1]
  local id = xml.find(result, 'id')[1]
  local mal_url = 'http://myanimelist.net/manga/'..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(string.sub(xml.find(result, 'synonyms')[1], 1, 200)))
  else
    syno = ''
  end

  if xml.find(result, 'chapters')[1] then
    chapters = '\nChapters: '..xml.find(result, 'chapters')[1]
  else
    chapters = ''
  end
  
  if xml.find(result, 'status')[1] then
    status = ' ('..xml.find(result, 'status')[1]..')'
  else
    status = ''
  end

  if xml.find(result, 'volumes')[1] then
    volumes = '\nVolumes: '..xml.find(result, 'volumes')[1]
  else
    volumes = ''
  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 = '\nVeröffentlichung: '..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..chapters..status..volumes..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 manga = URL.escape(matches[1])
  local receiver = get_receiver(msg)
  local manga_info = get_manga_info(manga)
  if manga_info == "HTTP-Fehler" then
    return "manga nicht gefunden!"
  else
    send_manga_data(manga_info, receiver)
  end
end

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

end