package com.eactive.eai.transformer.transform;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.eactive.eai.transformer.dao.LoadingUnit;
import com.eactive.eai.transformer.layout.Layout;

public class Transform  extends LoadingUnit {
	private static final long serialVersionUID = 1L;
	public static final String TARGET_LAYOUT = "TGT_LAYOUT";
    public static final String SOURCE_LAYOUT = "SRC_LAYOUT";
    private String name;
    private String description;
    private String lastUpdateDate;
    private String eAISevrDstcd;
    private String modfiMgtStusDstcd;
    private String useYn;
    private Layout targetLayout;
    private Map<String, Layout> sourceLayoutMap;
    private List<Layout> sourceLayoutList;
    private List<TransformItem> transformItemList;

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

    public Transform(String name) {
        this.name = name;
        this.sourceLayoutMap = new HashMap<>();
        this.sourceLayoutList = new ArrayList<>();
        this.transformItemList = new ArrayList<>();
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return this.description;
    }
    
    public String getLastUpdateDate() {
    	return this.lastUpdateDate;
    }
    
    public void setLastUpdateDate(String lastUpdateDate) {
    	this.lastUpdateDate = lastUpdateDate;
    }

    public String getEAISevrDstcd() {
		return eAISevrDstcd;
	}

	public void setEAISevrDstcd(String eAISevrDstcd) {
		this.eAISevrDstcd = eAISevrDstcd;
	}

	public String getModfiMgtStusDstcd() {
		return modfiMgtStusDstcd;
	}

	public void setModfiMgtStusDstcd(String modfiMgtStusDstcd) {
		this.modfiMgtStusDstcd = modfiMgtStusDstcd;
	}

	public String getUseYn() {
		return useYn;
	}

	public void setUseYn(String useYn) {
		this.useYn = useYn;
	}

	public void setTargetLayout(Layout targetLayout) {
        this.targetLayout = targetLayout;
    }

    public Layout getTargetLayout() {
        return this.targetLayout;
    }

    public void addSourceLayout(Layout layout) {
        this.sourceLayoutMap.put(layout.getName(), layout);
        this.sourceLayoutList.add(layout);
    }
    
    public Iterator<Layout> getSourceLayouts() {
    	return sourceLayoutMap.values().iterator();
    }

    public Layout getSourceLayout(String name) {
        return this.sourceLayoutMap.get(name);
    }

    public Layout getSourceLayout(int idx) {
        return this.sourceLayoutList.get(idx);
    }

    public int getSourceLayoutCount() {
        return this.sourceLayoutMap.size();
    }

    public Iterator<String> getSourceLayoutNames() {
        return this.sourceLayoutMap.keySet().iterator();
    }

    public void addTransformItem(TransformItem item) {
        this.transformItemList.add(item);
    }

    public List<TransformItem> getTransformItemList() {
        return this.transformItemList;
    }

    public TransformItem getTransformItem(int idx) {
        return this.transformItemList.get(idx);
    }

    public int getTransformItemCount() {
        return this.transformItemList.size();
    }
    
    public String summary() {
        StringBuilder sb = new StringBuilder();

        sb.append("Transform (").append(name).append(") from (");
        for (int i = 0; i < sourceLayoutList.size(); i++) {
            sb.append("," + sourceLayoutList.get(i).getName());
        }
        sb.append(") to (");

        if (targetLayout != null) {
            sb.append(targetLayout.getName());
        }

        sb.append(")");

        for (int i = 0; i < transformItemList.size(); i++) {
            TransformItem transformItem = transformItemList.get(i);
            sb.append("\n   Transform Rule Expression : " + transformItem.getResultItemPath() + " = " + transformItem.getInstruction());
        }

        return sb.toString();
    }

    public String toString(String tab) {
    	StringBuilder sb = new StringBuilder();
        sb.append(tab);
        sb.append("+ Transform[name=").append(name);
        sb.append(", description=").append(description);

        for (int i = 0; i < sourceLayoutList.size(); i++) {
            sb.append(", source[" + i + "]=" + sourceLayoutList.get(i).getName());
        }

        if (targetLayout != null) {
            sb.append(", target=" + targetLayout.getName());
        }

        sb.append("]");

        for (int i = 0; i < transformItemList.size(); i++) {
            sb.append("\n    - " + transformItemList.get(i).toString(tab + tab));
        }

        return sb.toString();
    }

    public String toString() {
        return toString("");
    }
}
