package com.eactive.eai.transformer.layout;

import java.time.LocalDateTime;

import com.eactive.eai.transformer.dao.LoadingUnit;
import com.eactive.eai.transformer.message.Message;
import com.eactive.eai.transformer.message.MessageException;
import com.eactive.eai.transformer.util.Constants;
import com.eactive.eai.transformer.util.Logger;
import com.eactive.eai.transformer.util.MemoryLogger;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Layout extends LoadingUnit {
	private static final long serialVersionUID = 5818439279232001470L;

	private static Logger logger = Logger.getLogger("transformer");

	private Item rootItem;
	private LayoutType layoutType;

	private String name;
	private String description;
	private LocalDateTime lastUpdateDate;
	private String bzwkDstcd;
	private String applicationName;
	private String systemInterface;
	private String eaiSvrDstcd;
	private String modifImgTstusDstcd;
	private String useYn;

    public Layout() {
        this("");
    }

    public Layout(String name) {
        this.name = name;
    }
   
	public void setRootItem(Item rootItem) {
		if (!getName().equals(rootItem.getName())) {
			throw new MessageException("root item's name (" + rootItem.getName() + ") is not same to layout name (" + getName() + ")");
		}
        this.rootItem = rootItem;
    }

    public void init() {
        Message root = (Message) getRootItem();
        
        if (root == null) {
	    	logger.warn("cannot init layout - layout '" + name + "' does not have root item");
	    	MemoryLogger.error("cannot init layout - layout '" + name + "' does not have root item");

	    	return;
        }
        
        root.createObject();

        try {
        	root.initInclude();
	    } catch(Throwable t) {
	    	logger.warn("cannot init layout - init/reload faild when initInclude() - " + name, t);
	    	MemoryLogger.error("<WARN> cannot init layout - init/reload faild when initInclude() - " + name, t);
	    }
	    
        try {
        	root.initDepth(1);
	    } catch(Throwable t) {
	    	logger.warn("cannot init layout - init/reload faild when initDepth() - " + name, t);
	    	MemoryLogger.error("<WARN> cannot init layout - init/reload faild when initDepth() - " + name, t);
	    }
	    
        try {
        	root.initDefaultValue(); 
	    } catch(Throwable t) {
	    	logger.warn("cannot init layout - init/reload faild when initDefaultValue() - " + name, t);
	    	MemoryLogger.error("<WARN> cannot init layout - init/reload faild when initDefaultValue() - " + name, t);
	    }
	    
//        try {
        	//------------------------------------------------------
			// TEST 메모리 문제로 임시로 MessageFactory에서 처리하도록함
			// Layout.init() 에서 주석처리함
			// 2009.07.06
//        	 root.initOccMin();			            
//	    } catch(Throwable t) {
//	    	logger.warn("cannot init layout - init/reload faild when initOccMin() - " + name, t);
//	    	MemoryLogger.error("<WARN> cannot init layout - init/reload faild when initOccMin() - " + name, t);
//	    }
    }
    
    public String toString(String tab) {
        StringBuilder sb = new StringBuilder();
        sb.append("\n" + tab + " | " + "layout name = " + name);
        sb.append("\n" + tab + " | " + "layout type = " + layoutType);
    	sb.append("\n" + tab + " | " + "description = " + description);
        return sb.toString();
    }

    public String toString() {
        return toString(Constants.TAB);
    }
}
