package com.uwsoft.editor.renderer.data;

import com.badlogic.ashley.core.Entity;
import com.uwsoft.editor.renderer.components.CompositeTransformComponent;
import com.uwsoft.editor.renderer.components.DimensionsComponent;
import com.uwsoft.editor.renderer.components.MainItemComponent;
import com.uwsoft.editor.renderer.utils.ComponentRetriever;

import java.util.ArrayList;

public class CompositeItemVO extends MainItemVO {

	public CompositeVO composite;
	
	public float scissorX; 
	public float scissorY;
	public float scissorWidth; 
	public float scissorHeight;

	public float width;
	public float height;
	public boolean automaticResize;
	
	public CompositeItemVO() {
		composite = new CompositeVO();
	}
	
	public CompositeItemVO(CompositeVO vo) {
		composite = new CompositeVO(vo);
	}
	
	public CompositeItemVO(CompositeItemVO vo) {
		super(vo);
		composite = new CompositeVO(vo.composite);
	}
	
	public void update(CompositeItemVO vo) {
		composite = new CompositeVO(vo.composite);
	}
	
	public CompositeItemVO clone() {
		CompositeItemVO tmp = new CompositeItemVO();
		tmp.composite = composite;
        tmp.itemName = itemName;
        tmp.layerName = layerName;
        tmp.rotation = rotation;
        tmp.tint = tint;
        tmp.x = x;
        tmp.y = y;
        tmp.zIndex = zIndex;

        tmp.scissorX = scissorX;
        tmp.scissorY = scissorY;
        tmp.scissorWidth = scissorWidth;
        tmp.scissorHeight = scissorHeight;

		tmp.width = width;
		tmp.height = height;
		
		return tmp;
	}

	@Override
	public void loadFromEntity(Entity entity) {
		super.loadFromEntity(entity);
		//scissorsX
		//scissorsY
		composite = new CompositeVO();
		composite.loadFromEntity(entity);

		DimensionsComponent dimensionsComponent = ComponentRetriever.get(entity, DimensionsComponent.class);
		CompositeTransformComponent compositeTransformComponent = ComponentRetriever.get(entity, CompositeTransformComponent.class);

		width = dimensionsComponent.width;
		height = dimensionsComponent.height;
		automaticResize = compositeTransformComponent.automaticResize;
	}

	public void cleanIds() {
		uniqueId = -1;
		ArrayList<MainItemVO> items = composite.getAllItems();
		for(MainItemVO subItem: items) {
			subItem.uniqueId = -1;
		}
	}
}
