 KONFIG = require('koding-config-manager').load("main.#{argv.c}")
 
 request        = require 'request'
-_ = require "underscore"
-
-getNextApiURL = (proxyUrl, callback)->
-
-  return callback null, KONFIG.socialapi.proxyUrl  unless proxyUrl
-
-  return callback null, "#{KONFIG.socialapi.customDomain.local}#{proxyUrl}"
+_               = require "underscore"
+
+socialProxyUrl  = "/api/social"
+webhookProxyUrl = "/api/integration"
+localDomain     = KONFIG.socialapi.customDomain.local
 
 wrapCallback = (callback)->
   (err, response, body) ->

  # topic fetch is crashing so we forced for a limit here
  data.limit = 15

  url = "/channel"
  get url, data, callback

fetchMessage = (data, callback)->
  if not data.id
    return callback { message: "Message id is not set"}

  url = "/message/#{data.id}"
  get url, data, callback

postToChannel = (data, callback)->
  if not data.channelId or not data.accountId or not data.body
    return callback { message: "Request is not valid for posting message"}

  url = "/channel/#{data.channelId}/message"
  post url, data, callback

editMessage = (data, callback)->
  if not data.body or not data.id
    return callback { message: "Request is not valid for editing a message"}

  url = "/message/#{data.id}"
  post url, data, callback

deleteMessage = (data, callback)->
  unless data.id
    return callback { message: "Request is not valid for deleting message"}
  url =  "/message/#{data.id}"
  deleteReq url, data, callback

likeMessage = (data, callback)->
  unless data.id
    return callback { message: "Request is not valid for liking a message"}

  url = "/message/#{data.id}/interaction/like/add"
  delete data.id
  post url, data, callback


  getXml url, {}, callback

updateChannel = (data, callback) ->
  url = "#{socialProxyUrl}/channel/#{data.id}/update"
  post url, data, callback

createChannel = (data, callback) ->
  url = "#{socialProxyUrl}/channel"
  post url, data, callback

deleteChannel = (data, callback) ->
  url = "#{socialProxyUrl}/channel/#{data.channelId}/delete"
  post url, data, callback

checkOwnership = (data, callback) ->
  url = "#{socialProxyUrl}/account/#{data.accountId}/owns"
  get url, data, callback

expireSubscription = (accountId, callback) ->
  url = "#{socialProxyUrl}/payments/customers/#{accountId}/expire"
  post url, {}, callback

fetchBotChannel = (data, callback) ->
  url = "#{webhookProxyUrl}/botchannel"
  get url, data, callback

post = (url, data, callback)->
  reqOptions =
    url    : "#{localDomain}#{url}"
    json   : true
    method : 'POST'

  {reqOptions, data} = setCookieIfRequired reqOptions, data
  {reqOptions, data} = setHeaderIfRequired reqOptions, data

  reqOptions.body = data

  request reqOptions, wrapCallback callback

deleteReq = (url, data, callback)->
  [data, callback] = [callback, null] unless callback

  reqOptions =
    url    : "#{localDomain}#{url}"
    json   : true
    method : 'DELETE'

  {reqOptions, data} = setCookieIfRequired reqOptions, data

  request reqOptions, wrapCallback callback

getXml = (url, data, callback)->
  reqOptions =
    url    : "#{localDomain}#{url}"
    method : 'GET'

  {reqOptions, data} = setCookieIfRequired reqOptions, data

  request reqOptions, wrapCallback callback

get = (url, data, callback)->
  reqOptions =
    url    : "#{localDomain}#{url}"
    json   : true
    method : 'GET'

  {reqOptions, data} = setCookieIfRequired reqOptions, data

  # finally set query string
  reqOptions.qs = data

  request reqOptions, wrapCallback callback

setCookieIfRequired = (reqOptions, data)->
  # inject clientId cookie if exists
