package model;

import java.util.ArrayList;
import java.util.List;

public class HitboxRectangle extends Hitbox {
	private List<Point> listPointsIso = new ArrayList<Point>();
	private List<Point> listPointsCartesian = new ArrayList<Point>();
	private float widthHitbox = 0,heightHitbox = 0; // Ширина и высота хитбокса в декартовых координатах.
	
	public HitboxRectangle(String shape, Entity owner){
		super(shape);
		setOwnerEntity(owner);
	}
	public HitboxRectangle(String shape,List<Point> listPointsIso,List<Point> listPointsCartesian){
		super(shape);
		this.listPointsIso = listPointsIso;
		this.listPointsCartesian = listPointsCartesian;
		initListsPoints();
	}
	
	//informationHitbox -  default="Rectangle 0 0 96 98"
	public HitboxRectangle(String[] dataHitbox, Entity owner){
		super(dataHitbox[0]);
		setOwnerEntity(owner);
		initListsPoints();
	    //в 0-м индексе всегда идет название фигуры.Так сделан наш xml.
	    parseStringToRectangleHitbox(dataHitbox);
	    createCartesianPointsFromWidthAndHeigh();
	    convertCartesianPointsToIso();
	    printToConsole();
	}
	
	public void initListsPoints() {
		if(listPointsIso.size()<1) {
			for(int i=0;i<4;i++) { 
				listPointsIso.add(new Point(0,0));
			}	
		}
		if(listPointsCartesian.size()<1) {
			for(int i=0;i<4;i++) { 
				listPointsCartesian.add(new Point(0,0));
			}	
		}
	}
	
	private void parseStringToRectangleHitbox(String[] dataHitbox) {
		referencePoint.x = Float.parseFloat(dataHitbox[1]);
		referencePoint.y = Float.parseFloat(dataHitbox[2]);
		widthHitbox  = Float.parseFloat(dataHitbox[3]);
		heightHitbox = Float.parseFloat(dataHitbox[4]);
		
	}
	//тут не правильно реализована функция, т.к. до сих пор нигде не используется. Надо переделать будет.
	public void convertIsoPointsToCartesian() {
		Point isoPoint;
		for (int i = 0 ; i<4;i++) {
			isoPoint = listPointsIso.get(i);
			cartesianToIsometric(isoPoint.x,isoPoint.y,listPointsCartesian.get(i));
		}
	}


	//+++++++
	public void convertCartesianPointsToIso() {
		Point cartesianPoint;
		for (int i = 0 ; i<4;i++) {
			cartesianPoint = listPointsCartesian.get(i);
			cartesianToIsometric(cartesianPoint.x,cartesianPoint.y,listPointsIso.get(i));
		}
	}
	//+++++++
	private void createCartesianPointsFromWidthAndHeigh(){
		Point refIsoPoint = new Point(owner.getWidth()/2+referencePoint.x,owner.getHeight()+referencePoint.y);
		Point refCartesianPoint = isometricToCartesian(refIsoPoint.x,refIsoPoint.y,new Point(0,0));
		listPointsCartesian.clear();
		listPointsCartesian.add(new Point(refCartesianPoint.x-widthHitbox,refCartesianPoint.y));
		listPointsCartesian.add(new Point(refCartesianPoint.x,refCartesianPoint.y));
		listPointsCartesian.add(new Point(refCartesianPoint.x,refCartesianPoint.y-heightHitbox));
		listPointsCartesian.add(new Point(refCartesianPoint.x-widthHitbox,refCartesianPoint.y-heightHitbox));
	}
	
	public List<Point> getListPointsIso() {
		return listPointsIso;
	}
	public List<Point> getListPointsCartesian() {
		return listPointsCartesian;
	}
	@Override
	public String listPointsToString() {
		System.out.println("Парсинг данных");
		parseCartesianListPointsToWidthHeight();
		calculationReferencePoint();
		return "Rectangle "+(int)referencePoint.x+" "+(int)referencePoint.y+" "+(int)widthHitbox+" "+(int)heightHitbox;
	}

	public void parseCartesianListPointsToWidthHeight() {
		widthHitbox = Math.abs(listPointsCartesian.get(0).x-listPointsCartesian.get(1).x);
		heightHitbox = Math.abs(listPointsCartesian.get(1).y-listPointsCartesian.get(2).y);
	}
	
	@Override
	public void calculationReferencePoint() {
		Point bottomPoint = new Point(0,0);
		for(Point currentPoint: listPointsIso) {
			if(bottomPoint.y<currentPoint.y) {
				bottomPoint.x=currentPoint.x;
				bottomPoint.y=currentPoint.y;
			}
		}
		bottomPoint = isometricToCartesian(bottomPoint.x,bottomPoint.y,bottomPoint);
		Point cartesianCenterImagePoint= new Point(0,0);
		cartesianCenterImagePoint = isometricToCartesian(owner.getImage().getWidth()/2,owner.getImage().getHeight(),cartesianCenterImagePoint);
		referencePoint.x = cartesianCenterImagePoint.x-bottomPoint.x;
		referencePoint.y = cartesianCenterImagePoint.y-bottomPoint.y;
	}
}
