import Options from "options";
import Markdown from "markdown";
import flashBadge from "badge";

var resultContainer = document.getElementById("result-markdown");

function copyToClipboard(text, okCallback) {
  resultContainer.value = text;
  resultContainer.select();
  document.execCommand('Copy');
  resultContainer.value = "";

  okCallback();
}

function extractTabsList(tabs) {
  let links = [];

  for (let i in tabs) {
    let tab = tabs[i];
    links.push({
      title: tab.title,
      url: tab.url
    });
  }

  return links;
}

var CopyAsMarkdown = new (function() {
  // load options
  this.options = {};

  Options.load(function(options) {
    this.options = options;
  }.bind(this));

  Options.onChange(function(changes) {
    for (key in changes) {
      this.options[key] = changes[key];
    }
  }.bind(this))

  this.copyLink = function(title, url, options) {
    var options = options || { needEscape: true };
    var escape = (options.needEscape && this.options.escape);
    var text = Markdown.linkTo(title, url, { escape });

    copyToClipboard(text, function() {
      flashBadge("success", "1");
    });
  };

  this.copyListOfLinks = function(links, options) {
    var options = options || { needEscape: true };
    var escape = (options.needEscape && this.options.escape);
    var md_list = [];

    for(var i in links) {
      var md = Markdown.linkTo(links[i].title, links[i].url, { escape });
      md_list.push("* " + md);
    }

    var text = md_list.join("\n");

    copyToClipboard(text, function() {
      flashBadge("success", md_list.length.toString());
    });
  };

  this.copyImage = function(title, url) {
    copyToClipboard(Markdown.imageFor(title, url), function() {
      flashBadge("success", "1");
    });
  };

  this.copyCurrentTab = function(options) {
    let query = {
      windowId: chrome.windows.WINDOW_ID_CURRENT,
      active: true
    };

    chrome.tabs.query(query, function(tabs) {
      tab = tabs[0];

      // XXX: Bad namespacing! (CoffeeScript's binding can resolve this issue)
      CopyAsMarkdown.copyLink(tab.title, tab.url, options);
    });
  };

  this.copyAllTabs = function(options) {
    let query = { currentWindow: true };

    chrome.tabs.query(query, function(tabs) {
      var links = extractTabsList(tabs);

      // XXX: Bad namespacing! (CoffeeScript's binding can resolve this issue)
      CopyAsMarkdown.copyListOfLinks(links, options);
    });
  };

  this.copyHighlightedTabs = function(options) {
    let query = { currentWindow: true, highlighted: true };

    chrome.tabs.query(query, function(tabs) {
      var links = extractTabsList(tabs);

      // XXX: Bad namespacing! (CoffeeScript's binding can resolve this issue)
      CopyAsMarkdown.copyListOfLinks(links, options);
    });
  };
})();

export default CopyAsMarkdown;

@@ -155,16 +155,40 @@
 kdown%22);
+%0Avar globalOptions = %7B%7D;
 %0A%0Afuncti
@@ -563,89 +563,24 @@
 %0A%7D%0A%0A
-var CopyAsMarkdown = new (function() %7B%0A  // load options%0A  this.options = %7B%7D;%0A%0A  
+// load options%0A
 Opti
@@ -589,33 +589,36 @@
 s.load(function(
-o
+newO
 ptions) %7B%0A    th
@@ -613,24 +613,23 @@
 ns) %7B%0A  
-  this.o
+globalO
 ptions =
@@ -633,17 +633,20 @@
 s = 
-o
+newO
 ptions;%0A
   %7D.
@@ -645,28 +645,13 @@
 ns;%0A
-  %7D.bind(this)
+%7D
 );%0A%0A
-  
 Opti
@@ -687,17 +687,19 @@
 ) %7B%0A
-  
   for (
+let 
 key 
@@ -720,16 +720,15 @@
     
-  this.o
+globalO
 ptio
@@ -757,28 +757,53 @@
 ;%0A  
-  %7D%0A  %7D.bind(this))%0A
+%7D%0A%7D);%0A%0Avar CopyAsMarkdown = new (function() %7B
 %0A  t
@@ -932,38 +932,39 @@
 s.needEscape && 
-this.o
+globalO
 ptions.escape);%0A
@@ -1254,14 +1254,15 @@
  && 
-this.o
+globalO
 ptio

Refactor Options