/*global window, rJS, UriTemplate */
/*jslint indent: 2, maxerr: 3, nomen: true */
(function (window, rJS, UriTemplate) {
  "use strict";
  function download_linux(context, evt) {
    var link = document.createElement('a');
    link.download = "ebulk-v1.0.0.tar.gz";
    link.href = window.location.origin + "/erp5/data_stream_module/embulk_download_script/getData";
    link.click();
  }
  function download_rpm(context, evt) {
    var link = document.createElement('a');
    link.download = "ebulk-v1.0.0-0.noarch.rpm";
    link.href = window.location.origin + "/erp5/data_stream_module/embulk_download_rpm/getData";
    link.click();
  }
  function download_win(context, evt) {
    var link = document.createElement('a');
    link.download = "ebulk-v1.0.0.zip";
    link.href = window.location.origin + "/erp5/data_stream_module/embulk_download_script_win/getData";
    link.click();
  }

  rJS(window)
    /////////////////////////////////////////////////////////////////
    // handle acquisition
    /////////////////////////////////////////////////////////////////
    .declareAcquiredMethod("redirect", "redirect")
    .declareAcquiredMethod("getUrlFor", "getUrlFor")
    .declareAcquiredMethod("jio_getAttachment", "jio_getAttachment")
    .declareAcquiredMethod("translateHtml", "translateHtml")
    .declareAcquiredMethod("updateHeader", "updateHeader")

    /////////////////////////////////////////////////////////////////
    // declared methods
    /////////////////////////////////////////////////////////////////
    .declareJob('download_linux', function (evt) {
      return download_linux(this, evt);
    })
    .declareJob('download_rpm', function (evt) {
      return download_rpm(this, evt);
    })
    .declareJob('download_win', function (evt) {
      return download_win(this, evt);
    })
     .declareMethod("render", function () {
      var gadget = this;
      return gadget.updateHeader({page_title: 'Download'});
    })
    .onEvent('submit', function (evt) {
      if (evt.target.name === 'download-linux') {
        return this.download_linux(evt);
      } else if (evt.target.name === 'download-win') {
        return this.download_win(evt);
      } else if (evt.target.name === 'download-rpm') {
        return this.download_rpm(evt);
      } else {
        throw new Error('Unknown form');
      }
    });

}(window, rJS, UriTemplate));