$(function() {
    $.extend(Base, {
        AwsUpload: {

        config: function(){
        AWS.config.update({
        accessKeyId : app.aws.accessKey,
        secretAccessKey :  app.aws.secretKey,
        correctClockSkew: true,
        });
        AWS.config.region =  app.aws.region;
        },

       /* options:{
        directory:String,
        file:File,
        callBack: function(data){}
        }*/
            upload: function (options) {

               Base.AwsUpload.config();

                var upFile=options.file;
                var bucket = new AWS.S3({params: {Bucket: app.aws.buket}});

                  var ext = "";
                  if ((upFile.name).split('.').length > 1) {
                    ext = "." + (upFile.name).split('.')[(upFile.name).split('.').length - 1];
                  }

                  var directory = options.directory ? options.directory : "private";

                  var fileName= directory+"/" + crypto.randomUUID() + ext;

                var uploadParams = {Key:fileName, ContentType: upFile.type, Body: upFile};


                bucket.upload(uploadParams, function(err, data) {
                     if(err) {
                          alert('Error subiendo Ficheros a amazon');
                          console.log("error subiendo ficheros a amazon", err);
                            Base.Loading.hide();
                          return false;
                      }

                      if(options.callBack != undefined && options.callBack != null && $.isFunction(options.callBack)) {

                        var url = data.Location;
                        var fileName = url.split('com')[url.split('com').length - 1];

                        return options.callBack(fileName);
                      }

                     console.log('Successfully Uploaded!');

                 }).on('httpUploadProgress', function(progress) {
                  var uploaded = parseInt((progress.loaded * 100) / progress.total);
                   $("progress").attr('value', uploaded);
                 });
            },

            _qxAwsUpload: function (box) {

                return {

                    ver: function () {

                        console.log("Base.AwsUpload 1.0");
                        console.log("Requiere: Base 1.0");
                    }
                }
            }
        }
    });

    $.fn.QxAwsUpload = function () {

        var awsUpload = this;

        return Base.Box._qxAwsUpload(awsUpload);
    }

});