import _User from './_User.js';
import _File from './_File.js';
import isUndefined from './util/isUndefined.js';

var _Image = {};

_Image.placeholder_img_180_path = './images/placeholder-180x180.png';
_Image.placeholder_img_960_path = './images/placeholder-960x720.png';

// Make a temporary Image instance (for use before it's uploaded); it cannot and should not be attached to item.image_ids. Once it's uploaded, the server returns a proper Image record, which can be attached to item.image_ids.
_Image.newFromFile = function(file){
  var image = {};
  image.original_filename = file.name;  // TODO: extract only filename without the path
  return _File.readAsBase64(file)
    .then(function(src_base64){
      image.src_base64 = src_base64;
      return image;
      });
  };

/*
_Image.upload = function(image){
  return m.request({
    method: "POST",
    url: "/cgi/newimage",
    body: {
      session_hash: _User.session_hash,
      image_base64: image.src_base64,
      original_filename: image.original_filename
      }
    });
  };
*/

_Image.addToItem = function(image, item){
  return _Item.addImage(item, image);
  };

_Image.thumbnailPath = function(image, size){
  var thumbnail_id = image['thumbnail_'+size+'_id'];
  if(isUndefined(thumbnail_id)){
    return _Image['placeholder_img_'+size+'_path']
    }
  else{
    return '/thumbnailsdb/'+thumbnail_id+'.png';
    }
  };

export default _Image;