// 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) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
// 
// Contributor(s): 

importClass(java.awt.Dimension);
importPackage(Packages.com.silverwrist.util);
importPackage(Packages.com.silverwrist.util.image);
importPackage(Packages.com.silverwrist.venice.core);
importPackage(Packages.com.silverwrist.venice.except);
importPackage(Packages.com.silverwrist.venice.ui);
importPackage(Packages.com.silverwrist.venice.ui.helpers);
importPackage(Packages.com.silverwrist.venice.ui.view);

// Get the request object.
rinput = bsf.lookupBean("request");

if (!(rinput.user.hasAdminAccess()))
{ // no admin access - we can't do this
  vlib.output(new ErrorBox("Access Error","You do not have permission to administer the system.",null));
  vlib.done();

} // end if

uid = rinput.getParameterInt("uid",0);
if (uid==0)
{ // user ID not found
  vlib.output(new ErrorBox(null,"User ID parameter not found.","sysadmin/find_user.js.vs"));
  vlib.done();

} // end if

rc = null;
if ("GET"==rinput.verb)
{ // create a PhotoUploader to do the photo uploading
  try
  { // create and initialize the PhotoUploader
    adm = rinput.user.adminInterface;
    admuser = adm.getUserContext(uid);
    rc = new PhotoUploader("Upload User Photo","sysadmin/modify_photo.js.vs",LinkTypes.SERVLET);
    rc.addHiddenParameter("uid",uid);
    html = vlib.queryHTMLRendering(rinput);
    rc.photoTag = html.getUserPhotoTag(admuser.getContactInfo().photoURL);
    rc.label = "New user photo";

  } // end try
  catch (e)
  { // an exception in the output...
    etype = vlib.exceptionType(e) + "";
    if (etype.match("DataException"))
      rc = new ErrorBox("Database Error","Database error generating display: " + e.message,
                        "sysadmin/find_user.js.vs");
    else if (etype.match("AccessError"))
      rc = new ErrorBox(null,"User ID parameter not found.","sysadmin/find_user.js.vs");
    else
      rc = e;

  } // end catch

  vlib.output(rc);
  vlib.done();

} // end if

// If the cancel button was clicked, bounce out of here.
if (rinput.isImageButtonClicked("cancel"))
{ // user elected to cancel upload - bounce us out of here
  vlib.output(new Redirect("sysadmin/modify_user.js.vs?uid=" + uid,LinkTypes.SERVLET));
  vlib.done();

} // end if

if (rinput.isImageButtonClicked("upload"))
{ // do some shorts-checking in the parameters
  if (!(rinput.isFileParam("thepic")))
  { // bogus file parameter
    logger.error("Internal Error: 'thepic' should be a file param");
    vlib.output(new ErrorBox(null,"Internal Error: 'thepic' should be a file param",
			     "sysadmin/modify_user.js.vs?uid=" + uid));
    vlib.done();

  } // end if

  mimetype = rinput.getParameterType("thepic") + "";
  if (!(mimetype.match("image/")))
  { // not an image file type that got uploaded
    logger.error("Error: 'thepic' not an image type");
    vlib.output(new ErrorBox(null,"You did not upload an image file.  Try again.",
                             "sysadmin/modify_photo.js.vs?uid=" + uid));
    vlib.done();

  } // end if

  try
  { // normalize the photo to 100x100 pixels
    real_pic = ImageNormalizer.normalizeImage(rinput.getParameterDataStream("thepic"),
                                              rinput.engine.getUserPhotoSize(),"jpeg");

    // set the user photo data!
    adm = rinput.user.adminInterface;
    admuser = adm.getUserContext(uid);
    ci = admuser.getContactInfo();
    ci.setPhotoData(rinput.contextPath + "/imagedata/","image/jpeg",real_pic.length,real_pic.data);
    admuser.putContactInfo(ci);

    // Jump back to the profile form.
    rc = new Redirect("sysadmin/modify_user.js.vs?uid=" + uid,LinkTypes.SERVLET);

  } // end try
  catch (e)
  { // exception while setting user photo
    etype = vlib.exceptionType(e) + "";
    if (etype.match("ServletMultipartException"))
      rc = new ErrorBox(null,"Internal Error: " + e.message,"sysadmin/modify_user.js.vs?uid=" + uid);
    else if (etype.match("ImageNormalizerException"))
      rc = new ErrorBox(null,e.message,"sysadmin/modify_photo.js.vs?uid=" + uid);
    else if (etype.match("DataException"))
      rc = new ErrorBox("Database Error","Database error storing user photo: " + e.message,
			"sysadmin/modify_user.js.vs?uid=" + uid);
    else if (etype.match("EmailException"))
      rc = new ErrorBox(null,"Internal Error: " + e.message,"sysadmin/modify_user.js.vs?uid=" + uid);
    else if (etype.match("AccessError"))
      rc = new ErrorBox("Access Error","You do not have permission to administer the system.",null);
    else
      rc = e;

  } // end catch

} // end if
else
{ // don't know the command button!
  logger.error("no known button click on POST to sysadmin/modify_photo.js");
  rc = new ErrorBox("Internal Error","Unknown command button pressed",
                    "sysadmin/modify_user.js.vs?uid=" + uid);

} // end else

vlib.output(rc);   // all done!
