/**
 * File upload
 *
 * @author ELO Digital Office GmbH
 * @version 1.03.006
 *
 */
sol.define("sol.common.forms.FileUpload", {

  /**
   * @cfg {String} buttonName
   * Button name
   */
  buttonName: undefined,

  /**
   * @cfg {String} maskName
   * Mask name
   */
  maskName: undefined,

  /**
   * @cfg {String} dialogTitle
   * Dialog title
   */
  dialogTitle: undefined,

  /**
   * @cfg {String} sordName
   * Sord name
   */
  sordName: "Element",

  /**
   * @cfg {String} extension
   * Extension
   */
  extension: "",

  /**
   * @cfg {function} onload
   * Callback
   */
  onload: undefined,

  /**
   * Initialize file upload
   * @param {Object} config Configuration
   */
  initialize: function (config) {
    var me = this;

    sol.common.forms.Utils.initializeIxSession();
    me.$super("sol.Base", "initialize", [config]);

    if (!me.buttonName) {
      throw "Button name is empty";
    }

    me.button = $var(me.buttonName);

    if (!me.button) {
      throw "Can't find button '" + me.buttonName + "'";
    }

    me.fileInputId = me.fileInputId || "file-input";

    me.fileInput = document.createElement("input");
    me.fileInput.id = me.fileInputId;
    me.fileInput.type = "file";
    me.fileInput.style = "display:none";
    me.fileInput.addEventListener("change", me.fileInputChangedInBrowser.bind(me)); // will only be called in Browser
    me.button.addEventListener("click", me.buttonClicked.bind(me));
  },

  fileInputChangedInBrowser: function (event) {
    var me = this,
        fileReader, file;

    fileReader = new FileReader();
    fileReader.onload = function () {
      me.uploadContent(fileReader.result);
    };
    if (event.target.files && (event.target.files.length > 0)) {
      file = event.target.files[0];
      if (file.name) {
        me.sordName = file.name.split(".").shift();
        me.extension = file.name.split(".").pop();
      }
      fileReader.readAsDataURL(file);
    }
  },

  isJavaFxBrowser: function () {
    var javaFxBrowser;
    javaFxBrowser = (navigator.userAgent.indexOf("JavaFX") > -1);
    return javaFxBrowser;
  },

  uploadContent: function (content) {
    var me = this,
        result = {},
        base64Content;

    try {
      ELOF.showLoadingDiv();
      if (content) {

        base64Content = content.replace(/^data:.+;base64,/, "");

        result = sol.common.IxUtils.execute("RF_sol_common_document_service_UploadFile", {
          objId: ELO_PARAMS.ELO_OBJID,
          base64Content: base64Content,
          extension: me.extension,
          cfg: {
            mask: me.maskName,
            pictureName: me.sordName
          }
        });

        if (result && result.objId && me.onload) {
          me.onload(result.objId);
        }
      }
    } catch (ex) {
      console.error("Exception: " + ex);
    } finally {
      ELOF.hideLoadingDiv();
    }
    return result;
  },

  buttonClicked: function (e) {
    var me = this, params;
    if (navigator.userAgent.indexOf("JavaFX") === -1) { // Web-Client
      if (me.fileInput) {
        me.fileInput.value = null;
        me.fileInput.click(); // this executes the actual handler for opening the file selection dialog. This trick is required, because I did not want to use the standard filepicker Button. (it would not look like an ELO Button)
      }
    } else { // Java-Client
      params = { objId: ELO_PARAMS.ELO_OBJID, dialogTitle: me.dialogTitle };
      api.communication.Parent.sendCustomMessage("workspace.showFileChooserDialog_", params, function (data, event) {
        console.info("data=" + JSON.stringify(data));
        me.sordName = data.response.name;
        me.extension = data.response.extension;
        me.uploadContent(data.response.base64);
      });
    }
  }
});