/// <reference path="typings/jquery/jquery.d.ts"/>
'use strict';

function getFirstChildInObject(obj) {
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      return obj[key];
    }
  }
}

function getMediaWikiTimestamp(date){
  console.log("" + date.toISOString());
  return date.toISOString();
  //return "" + date.getUTCFullYear()+ (date.getUTCMonth() + 1) + date.getUTCDate() + date.getUTCHours() + date.getUTCMinutes() + date.getUTCSeconds();
}

function port(tab){
var TARGET_WIKI_API_URL_BASE = "http://lotr.huiji.wiki/api.php";
  var tempIndex = tab.url.indexOf("/wiki/");
  if (tempIndex < 0){
    console.log("Not a valid wiki url");
    return;
  }
  var title = tab.url.substring(tempIndex + 6);
  console.log(title);
  var requestUrl = tab.url.replace("/wiki/", "/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=");
  console.log(requestUrl);
  $.ajax({
    url: requestUrl
  }).done(function (params) {
    var pagesNode = params.query.pages;
    var pageNode = getFirstChildInObject(pagesNode);
    var wikitext = getFirstChildInObject(pageNode.revisions[0]);
    console.log(wikitext);
    $.ajax({
      url: TARGET_WIKI_API_URL_BASE + "?action=query&meta=tokens&format=json",
    }).done(function(tokensParam){
      console.log(tokensParam);
      var timestamp = getMediaWikiTimestamp(new Date());
      console.log(timestamp);
      var token = tokensParam.query.tokens.csrftoken;
      console.log(token);
      var editRequestUrl = TARGET_WIKI_API_URL_BASE + "?action=edit&format=json&summary=port&title=" + title + "&starttimestamp=" + timestamp;
      //var editRequestUrl = "http://lotr.huiji.wiki/api.php?action=edit&title=Test&summary=test%20summary&text=article%20content&basetimestamp=20070824123454";
      var postBody = {
        "token" : token,
        "text" : wikitext
      }
      console.log(editRequestUrl);
      $.ajax({
        url: editRequestUrl,
        type: "POST",
        data: postBody,
      }).done(function (params2){
        console.log(params2);
      }).error(function( jqXHR, textStatus, errorThrown){
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown);
      });
    });
  });
}

// Listener for when the user clicks on the Wikimapper button
chrome.browserAction.onClicked.addListener(function() {
 // chrome.tabs.create({'url': chrome.extension.getURL('html/index.html')}, function() {
 // });
  chrome.tabs.getSelected(null, function (tab){
    port(tab);
  });
});

@@ -197,270 +197,8 @@
 %0A%7D%0A%0A
-function getMediaWikiTimestamp(date)%7B%0A  console.log(%22%22 + date.toISOString());%0A  return date.toISOString();%0A  //return %22%22 + date.getUTCFullYear()+ (date.getUTCMonth() + 1) + date.getUTCDate() + date.getUTCHours() + date.getUTCMinutes() + date.getUTCSeconds();%0A%7D%0A%0A
 func
@@ -1029,95 +1029,8 @@
 m);%0A
-      var timestamp = getMediaWikiTimestamp(new Date());%0A      console.log(timestamp);%0A
     
@@ -1185,16 +1185,27 @@
 at=json&
+createonly&
 summary=
@@ -1228,202 +1228,8 @@
 itle
- + %22&starttimestamp=%22 + timestamp;%0A      //var editRequestUrl = %22http://lotr.huiji.wiki/api.php?action=edit&title=Test&summary=test%2520summary&text=article%2520content&basetimestamp=20070824123454%22
 ;%0A  

create only