// The contents of this file are subject to the Mozilla Public License Version 1.1
// (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
// WARRANTY OF ANY KIND, either express or implied. See the License for the specific
// language governing rights and limitations under the License.
// 
// The Original Code is the Venice Web Communities System.
// 
// The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
// for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
// Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
// 
// Contributor(s): 

importPackage(Packages.com.silverwrist.util);
importClass(Packages.com.silverwrist.dynamo.Namespaces);
importClass(Packages.com.silverwrist.dynamo.db.ImageStore);
importPackage(Packages.com.silverwrist.dynamo.iface);
importPackage(Packages.com.silverwrist.dynamo.util);
importClass(Packages.com.silverwrist.venice.VeniceNamespaces);
importPackage(Packages.com.silverwrist.venice.content);

req = bsf.lookupBean("request");
req_help = bsf.lookupBean("request_help");
target = req_help.getParameterString("tgt");
if (target==null)
  target = "top.js.vs";
on_error = "profile.js.vs?tgt=" + stringutils.encodeURL(target);
vlib.setOnError(req,on_error);

// Check the user account.
user = vlib.getUser(req);
if (user.isAnonymous())
{ // user not logged in, must log in first - so bounce us to the login dialog
  new_target = "profile_photo.js.vs?tgt=" + stringutils.encodeURL(target);
  dynamo.scriptReturn(new Redirect("SERVLET","login.js.vs?tgt=" + stringutils.encodeURL(new_target)));

} // end if

// Make sure they can actually edit their own photo.
admin_flags = cast.toOptionSet(user.getObject(VeniceNamespaces.USER_SETTINGS_NAMESPACE,"admin.flags"));
if (admin_flags.get(0))
  dynamo.scriptReturn(new Redirect("SERVLET",on_error));

if (req_help.isVerb("GET"))
{ // on GET, display the form
  view = new VelocityView("Set User Photo","user/user_photo.vm");
  view.setParameter("target",target);
  dynamo.scriptReturn(view);

} // end if

// everything from this point on is a POST operation
if (req_help.isImageButtonClicked("cancel"))
  dynamo.scriptReturn(new Redirect("SERVLET",on_error));

// get the image provider object
imgprov = cast.queryImageStore(req_help.getRequestObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,"images"));

// figure out which image ID we have
imgid = imgprov.findImageID(VeniceNamespaces.USER_PROFILE_NAMESPACE,"user.photo",user);

selector = req_help.getParameterString("selector") + "";
if (selector=="none")
{ // take out the image property and the image
  if (PropertyUtils.hasProperty(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,"url.photo"))
    user.removeObject(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,"url.photo");
  if (imgid!=-1)
    imgprov.deleteImage(user,imgid);

} // end if
else if (selector=="set")
{ // set the image URL directly, delete the image
  s = req_help.getParameterString("url");
  if (stringutils.isEmpty(s))
    dynamo.scriptReturn(new ErrorBox(null,"No photo URL specified.",on_error));
  user.setObject(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,"url.photo",s);
  if (imgid!=-1)
    imgprov.deleteImage(user,imgid);

} // end else if
else if (selector=="upload")
{ // take the uploaded image, reformat it, and save it off, then set the image URL
  input_di = req_help.getDataItem("image");
  if (input_di==null)
    dynamo.scriptReturn(new ErrorBox(null,"No uploaded photo specified.",on_error));
  mtype = input_di.getMimeType();
  if (!(mtype.startsWith("image/")))
    dynamo.scriptReturn(new ErrorBox(null,"Uploaded data item is not an image.",on_error));
  globals = vcast.getGlobalPropertiesStore(req);
  width = cast.toInteger(globals.getObject(VeniceNamespaces.CONTENT_LAF_NAMESPACE,"user.photo.width"));
  height = cast.toInteger(globals.getObject(VeniceNamespaces.CONTENT_LAF_NAMESPACE,"user.photo.height"));
  di = imgprov.normalizeImage(input_di,width,height,"image/jpeg");
  if (imgid!=-1)
    imgprov.replaceImage(user,imgid,di);
  else
    imgid = imgprov.saveNewImage(VeniceNamespaces.USER_PROFILE_NAMESPACE,"user.photo",user,di);
  rewriter = cast.queryURLRewriter(req);
  user.setObject(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,"url.photo",
                 rewriter.rewriteURL("IMAGEDATA",imgid + ""));

} // end else if
else  // return a "No Content" and just leave the dialog box up
  dynamo.scriptReturn(new NoContent());

// All done - bounce back to the Profile dialog
dynamo.scriptOutput(new Redirect("SERVLET",on_error));
