
var photoHelper = function () {

  //private menbers
  var photoData;
  var maxHeight = 600;

  //private methods
  function init() {
    console.log('photoHelper is loaded.');
  }

  function loadFile(_input,_callback) {
    return new Promise((reslove) => {
      // var file = document.querySelector('input[type=file]').files[0];
      var file = $(_input)[0].files[0];

      removePicExif(file).then(res => {
        console.log(res)
        file = res;
        ImageHelper.resizeAndRotateImage(file,maxHeight,function(resizeImageObj){
          // var newImage = new Image();
          // newImage.src = resizeImageObj;
          _callback(resizeImageObj);
          reslove(resizeImageObj)
        });
      });
    })
    
  }

  function removePicExif(file) {
    return new Promise((reslove) => {
      if (file) {
        let reader = new FileReader();
        let image = new Image();
        reader.onload = function (ev) {
          image.src = ev.target.result;
          image.onload = function () {
            const ctxWidth = this.width;
            const ctxHeight = this.height;
            const canvas = document.createElement("canvas");
            const ctx = canvas.getContext("2d");
            canvas.width = ctxWidth;
            canvas.height = ctxHeight;
            ctx.drawImage(this, 0, 0, ctxWidth, ctxHeight);
            canvas.toBlob((blob) => {
              const newFile = new File([blob], file.name, {
                type: "image/jpeg",
                lastModified: Date.now(),
              });
              reslove(newFile);
            });
          };
        };
        reader.readAsDataURL(file);
      }
    });
  }

  var ImageHelper = {
    resizeAndRotateImage: function (inImageSource, inMaxLength, inSuccessCallback) {
      var reader = new FileReader();
      reader.readAsDataURL(inImageSource);
      reader.onload = function (e) {
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext("2d");
        var img = new Image();
        img.onload = function () {
          //設定長邊上限值
          var max_Length = inMaxLength;

          var imgWidth = img.width;
          var imgHeight = img.height;

          if (imgWidth > imgHeight) {
            if (imgHeight > max_Length) {
              imgWidth = Math.round(imgWidth *= max_Length / imgHeight);
              imgHeight = max_Length;
            }
          } else {
            if (imgWidth > max_Length) {
              imgHeight = Math.round(imgHeight *= max_Length / imgWidth);
              imgWidth = max_Length;
            }
          }

          canvas.width = imgWidth;
          canvas.height = imgHeight;

          var that = this;
          EXIF.getData(img, function () {
            var orientation = EXIF.getTag(that, 'Orientation');
            // alert(orientation);

            if (orientation == 6 || orientation == 8 || orientation == 3) {
              var rotateAngle = 0;

              switch (orientation) {
                case 3:
                  rotateAngle = 180;
                  break;
                case 6:
                  rotateAngle = 90;
                  canvas.width = imgHeight;
                  canvas.height = imgWidth;
                  break;
                case 8:
                  rotateAngle = -90;
                  canvas.width = imgHeight;
                  canvas.height = imgWidth;
                  break;
              }

              var x = canvas.width / 2;
              var y = canvas.height / 2;

              ctx.translate(x, y);
              ctx.rotate(rotateAngle * Math.PI / 180);

              ctx.drawImage(img, (-imgWidth / 2), (-imgHeight / 2), imgWidth, imgHeight);
              console.log(imgWidth + ', ' + imgHeight);
            }
            else {
              ctx.drawImage(img, 0, 0, imgWidth, imgHeight);
              console.log(imgWidth + ', ' + imgHeight);
            }
          });


          var res = canvas.toDataURL("image/jpeg", 0.9);

          inSuccessCallback(res);
        };

        img.src = e.target.result;
      };
    }
  };

  //constructor

  {
    $(document).ready(function () {
      init();
    });
  }

  //public

  return {
    loadFile: function (_input,_callback) {
      loadFile(_input,_callback);
    }
  };
};


