package com.celements.web.plugin.cmd;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xwiki.context.Execution;
import org.xwiki.model.reference.DocumentReference;

import com.celements.inheritor.FieldInheritor;
import com.celements.inheritor.InheritorFactory;
import com.celements.web.service.IWebUtilsService;
import com.celements.web.utils.WebUtils;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.web.Utils;

public class SkinConfigObjCommand {

  private static Log LOGGER = LogFactory.getFactory().getInstance(
      SkinConfigObjCommand.class);

  IWebUtilsService injected_webService;

  public FieldInheritor getSkinConfigFieldInheritor(String fallbackClassName) {
    XWikiDocument doc = getContext().getDoc();
    try {
      XWiki xwiki = getContext().getWiki();
      XWikiDocument skinDoc = xwiki.getDocument(getWebService().resolveDocumentReference(
          xwiki.getSpacePreference("skin", getContext())), getContext());
      String className = skinDoc.getXObject(getSkinsClassRef()).getStringValue(
          "skin_config_class_name");
      return new InheritorFactory().getConfigDocFieldInheritor(
          className, getWebService().getRefLocalSerializer().serialize(
              doc.getDocumentReference()), getContext());
    } catch(XWikiException exp){
      LOGGER.error("Failed to get skin-config object.", exp);
    }
    return null;
  }

  public BaseObject getSkinConfigObj() {
    return getSkinConfigObj("");
  }

  public BaseObject getSkinConfigObj(String fallbackClassName) {
    XWikiDocument doc = getContext().getDoc();
    try {
      XWiki xwiki = getContext().getWiki();
      XWikiDocument skinDoc = xwiki.getDocument(getWebService().resolveDocumentReference(
          xwiki.getSpacePreference("skin", getContext())), getContext());
      BaseObject skinObj = skinDoc.getXObject(getSkinsClassRef());
      if (skinObj != null) {
        String className = skinObj.getStringValue("skin_config_class_name");
        if ((className == null) || "".equals(className)) {
          className = fallbackClassName;
        }
        if ((className != null) && !"".equals(className)) {
          BaseObject configObj = WebUtils.getInstance().getConfigDocByInheritance(doc,
              className, getContext()).getObject(className);
          return configObj;
        }
      } else {
        LOGGER.warn("getSkinConfigObj failed because no 'XWiki.XWikiSkins' object found"
            + " on skinDoc [" + skinDoc.getDocumentReference() + "].");
      }
    } catch(XWikiException e){
      LOGGER.error(e);
    }
    return null;
  }

  public DocumentReference getSkinsClassRef() {
    return getWebService().resolveDocumentReference("XWiki.XWikiSkins");
  }

  private IWebUtilsService getWebService() {
    if (injected_webService != null) {
      return injected_webService;
    }
    return Utils.getComponent(IWebUtilsService.class);
  }

  private Execution getExecution() {
    return Utils.getComponent(Execution.class);
  }

  private XWikiContext getContext() {
    return (XWikiContext)getExecution().getContext().getProperty("xwikicontext");
  }

}

@@ -936,232 +936,49 @@
     
-XWiki xwiki = getContext().getWiki();%0A      XWikiDocument skinDoc = xwiki.getDocument(getWebService().resolveDocumentReference(%0A          xwiki.getSpacePreference(%22skin%22, getContext())), getContext());%0A      String c
+String className = getSkinConfigC
 lassName
  = s
@@ -977,100 +977,37 @@
 Name
- = s
+FromS
 kinDoc
-.getXObject(getSkinsClassRef()).getStringValue(%0A          %22skin_config_c
+(fallbackC
 lass
-_n
+N
 ame
-%22
 );%0A 
@@ -1521,24 +1521,526 @@
     try %7B%0A  
+    String className = getSkinConfigClassNameFromSkinDoc(fallbackClassName);%0A      if ((className != null) && !%22%22.equals(className)) %7B%0A        BaseObject configObj = WebUtils.getInstance().getConfigDocByInheritance(doc,%0A            className, getContext()).getObject(className);%0A        return configObj;%0A      %7D%0A    %7D catch(XWikiException e)%7B%0A      LOGGER.error(e);%0A    %7D%0A    return null;%0A  %7D%0A%0A  private String getSkinConfigClassNameFromSkinDoc(String fallbackClassName)%0A      throws XWikiException %7B%0A
     XWiki xw
@@ -2069,18 +2069,16 @@
 Wiki();%0A
-  
     XWik
@@ -2165,18 +2165,16 @@
         
-  
 xwiki.ge
@@ -2225,26 +2225,24 @@
 Context());%0A
-  
     BaseObje
@@ -2292,24 +2292,64 @@
 ssRef());%0A  
+  String className = fallbackClassName;%0A
     if (skin
@@ -2365,34 +2365,42 @@
 ll) %7B%0A      
-  
 String 
-c
+skinConfigC
 lassName = s
@@ -2458,595 +2458,128 @@
     
-  
 if ((
-className == null) %7C%7C %22%22.equals(className)) %7B%0A          className = fallbackClassName;%0A        %7D%0A        if ((className != null) && !%22%22.equals(className)) %7B%0A          BaseObject configObj = WebUtils.getInstance().getConfigDocByInheritance(doc,%0A              className, getContext()).getObject(className);%0A          return configObj;%0A        %7D%0A      %7D else %7B%0A        LOGGER.warn(%22getSkinConfigObj failed because no 'XWiki.XWikiSkins' object found%22%0A            + %22 on skinDoc %5B%22 + skinDoc.getDocumentReference() + %22%5D.%22);%0A      %7D%0A    %7D catch(XWikiException e)%7B%0A      LOGGER.error(e);
+skinConfigClassName != null) && !%22%22.equals(skinConfigClassName)) %7B%0A        className = skinConfigClassName;%0A      %7D
 %0A   
@@ -2584,36 +2584,41 @@
    %7D%0A    return 
-null
+className
 ;%0A  %7D%0A%0A  public 

fix SkinConfigObjCommand if cellSkin is used