// 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 and the community
rinput = bsf.lookupBean("request");
comm = rinput.getCommunity(true,"top.js.vs");

if (!(comm.canModifyProfile()))
{ // no access - sorry, dude
  logger.error("tried to call up community profile screen without access...naughty naughty!");
  vlib.output(new ErrorBox("Unauthorized","You do not have access to modify this community's profile.",
			   "comm/admin_menu.js.vs?cc=" + comm.communityID));
  vlib.done();

} // end if

rc = null;
if ("GET"==rinput.verb)
{ // create a PhotoUploader to do the photo uploading
  try
  { // create and initialize the PhotoUploader
    rc = new PhotoUploader("Upload Community Logo","comm/photo.js.vs",LinkTypes.SERVLET);
    rc.addHiddenParameter("cc",comm.communityID);
    html = vlib.queryHTMLRendering(rinput);
    rc.photoTag = html.getCommunityLogoTag(comm.getContactInfo().photoURL);
    rc.label = "New community logo";

  } // 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,
                        "comm/profile.js.vs?cc=" + comm.communityID);
    else if (etype.match("Access Error"))
      rc = new ErrorBox("Access Error",e.message,"comm/profile.js.vs?cc=" + comm.communityID);
    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("comm/profile.js.vs?cc=" + comm.communityID,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",
			     "comm/profile.js.vs?cc=" + comm.communityID));
    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.",
                             "comm/photo.js.vs?cc=" + comm.communityID));
    vlib.done();

  } // end if

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

    // set the community logo data!
    ci = comm.getContactInfo();
    ci.setPhotoData(rinput.contextPath + "/imagedata/","image/jpeg",real_pic.length,real_pic.data);
    comm.putContactInfo(ci);

    // Jump back to the profile form.
    rinput.setSessionAttribute(RequestInput.LEFT_MENU_SESSION_ATTR,null);
    rc = new Redirect("comm/profile.js.vs?cc=" + comm.communityID,LinkTypes.SERVLET);

  } // end try
  catch (e)
  { // exception while setting community logo
    etype = vlib.exceptionType(e) + "";
    if (etype.match("ServletMultipartException"))
      rc = new ErrorBox(null,"Internal Error: " + e.message,"comm/profile.js.vs?cc=" + comm.communityID);
    else if (etype.match("ImageNormalizerException"))
      rc = new ErrorBox(null,e.message,"comm/photo.js.vs?cc=" + comm.communityID);
    else if (etype.match("DataException"))
      rc = new ErrorBox("Database Error","Database error storing community logo: " + e.message,
			"comm/profile.js.vs?cc=" + comm.communityID);
    else
      rc = e;

  } // end catch

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

} // end else

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