/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.celements.web.pagetype;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;

public class PageTypeCommand {

  public static final String PAGE_TYPE_CLASS_DOC = "PageType";
  public static final String PAGE_TYPE_CLASS_SPACE = "Celements2";
  public static final String PAGE_TYPE_CLASSNAME = PAGE_TYPE_CLASS_SPACE + "."
       + PAGE_TYPE_CLASS_DOC;

  private static PageTypeCommand pageTypeInstance;
  
  private static Log mLogger = LogFactory.getFactory().getInstance(PageTypeCommand.class);
  
  PageTypeCommand() {
  }
  
  public static IPageType getApiInstance(XWikiContext context) throws XWikiException {
    Object obj = context.get("pageTypeApi");
    if ((obj == null) || !(obj instanceof IPageType)) {
      context.put("pageTypeApi", new PageTypeApi(context.getDoc().getFullName(), context));
    }
    return ((IPageType)context.get("pageTypeApi"));
  }
  
  public static PageTypeCommand getInstance() {
    if (pageTypeInstance == null) {
      pageTypeInstance = new PageTypeCommand();
    }
    return pageTypeInstance;
  }

  public BaseObject getPageTypeObject(XWikiDocument doc, XWikiContext context){
    if((doc != null) && doc.isNew()) {
      doc = getTemplateDoc(doc, context);
    }
    if ((doc != null) && (doc.getObjects(PAGE_TYPE_CLASSNAME) != null)
        && (doc.getObjects(PAGE_TYPE_CLASSNAME).size() > 0)) {
      return doc.getObject(PAGE_TYPE_CLASSNAME);
    }
    return null;
  }

  public String getPageType(XWikiDocument doc, XWikiContext context){
    //TODO get default PageType from WebPreferences
    return getPageTypeWithDefault(doc, "RichText", context);
  }

  public String getPageTypeWithDefault(XWikiDocument doc, String defaultPageType,
      XWikiContext context) {
    String pageType = "";
    if (doc != null) {
      BaseObject pageTypeObj = getPageTypeObject(doc, context);
      if(pageTypeObj != null) {
        pageType = pageTypeObj.getStringValue("page_type");
      }
    }
    if ((pageType == null) || "".equals(pageType.trim())) {
      return defaultPageType;
    }
    return pageType.trim();
  }

  XWikiDocument getTemplateDoc(XWikiDocument doc, XWikiContext context) {
    String templName = context.getRequest().get("template");
    if((templName != null) && !"".equals(templName.trim())
        && context.getWiki().exists(templName, context)) {
      try {
        doc = context.getWiki().getDocument(templName, context);
      } catch (XWikiException e) {
        mLogger.error("Exception while getting template doc '" + templName + "'", e);
      }
    }
    return doc;
  }

  public String getPageTypeDocFN(XWikiDocument doc, XWikiContext context) {
    return completePageTypeDocName(getPageType(doc, context));
  }

  private String completePageTypeDocName(String pageTypeName) {
    if (pageTypeName.indexOf('.') > 0) {
      return pageTypeName;
    } else {
      return "PageTypes." + pageTypeName;
    }
  }

  public PageType getPageTypeObj(XWikiDocument currentDoc, XWikiContext context) {
    return new PageType(getPageTypeDocFN(currentDoc, context));
  }

  public PageType getPageTypeWithDefaultObj(XWikiDocument currentDoc,
      String defaultPageType, XWikiContext context) {
    String pageTypeDocName = getPageTypeWithDefault(currentDoc, defaultPageType, context);
    if ((pageTypeDocName != null) && !"".equals(pageTypeDocName)) {
      return new PageType(completePageTypeDocName(pageTypeDocName));
    }
    return null;
  }

  public PageType getPageTypeObj(String pageTypeFN, XWikiContext context) {
    return new PageType(pageTypeFN);
  }

  public String getPrettyName(XWikiDocument doc, XWikiContext context) {
    return getPageTypeObj(doc, context).getPageTypeProperties(context).getStringValue(
        "type_name");
  }

  public boolean isVisible(XWikiDocument doc, XWikiContext context) {
    return getPageTypeObj(doc, context).getPageTypeProperties(context).getIntValue(
        "visible", 0) > 0;
  }

}

@@ -1440,59 +1440,8 @@
 C;%0A%0A
-  private static PageTypeCommand pageTypeInstance;%0A
   %0A 
@@ -1460,23 +1460,22 @@
 tic Log 
-mLogger
+LOGGER
  = LogFa
@@ -1536,37 +1536,8 @@
 %0A  %0A
-  PageTypeCommand() %7B%0A  %7D%0A  %0A
   pu
@@ -1879,29 +1879,36 @@
  %7D%0A  %0A  
-public static
+/**%0A   * @return new
  PageTyp
@@ -1920,113 +1920,148 @@
 and 
-getI
+i
 nstance
-() %7B%0A    if (pageTypeInstance == null) %7B%0A      pageTypeInstance = new PageTypeCommand();%0A    %7D
+%0A   * %0A   * @deprecated instead please use new PageTypeCommand()%0A   */%0A  @Deprecated%0A  public static PageTypeCommand getInstance() %7B
 %0A   
@@ -2072,24 +2072,29 @@
 urn 
-p
+new P
 ageType
-Instance
+Command()
 ;%0A  
@@ -3499,15 +3499,14 @@
     
-mLogger
+LOGGER
 .err
@@ -3754,22 +3754,21 @@
   %7D%0A%0A  p
-rivate
+ublic
  String 

rename mLogger to LOGGER remove not needed singleton programming