/*global window, rJS, RSVP, URI */
/*jslint nomen: true, indent: 2, maxerr: 3 */
(function (window, document, RSVP, rJS,
           XMLHttpRequest, location, console) {
  "use strict";
  function download(context, evt) {
    var link = document.createElement('a');
    link.download = "ebulk_tool_unix.zip";
    link.href = window.location.origin + "/erp5/data_stream_module/embulk_download_script/getData";
    link.click();
  }
  function download_win(context, evt) {
    var link = document.createElement('a');
    link.download = "ebulk_tool_win.zip";
    link.href = window.location.origin + "/erp5/data_stream_module/embulk_download_script_win/getData";
    link.click();
  }

  rJS(window)
    .allowPublicAcquisition('setFillStyle', function () {
      return {
        height: '100%',
        width: '100%'
      };
    })
    .declareJob('download', function (evt) {
      return download(this, evt);
    })
    .declareJob('download_win', function (evt) {
      return download_win(this, evt);
    })
    .declareMethod("render", function (reference) {
      var html = "pull <i>" + reference + "</i>";
      if (reference === undefined)
        html = "push <i>my-data-set</i>";
      return this.changeState({"dataset_reference" : html});
    })
    .declareService(function () {
      document.getElementById("dataset_reference").innerHTML = this.state.dataset_reference;
      document.getElementById("dataset_reference_win").innerHTML = this.state.dataset_reference;
    })
    .onEvent('submit', function (evt) {
      if (evt.target.name === 'download') {
        return this.download(evt);
      } else if (evt.target.name === 'download-win') {
        return this.download_win(evt);
      } else {
        throw new Error('Unknown form');
      }
    });
}(window, document, RSVP, rJS,
  XMLHttpRequest, location, console));