#pragma once

#include "Stamp.h"
#include "ScreenManager.h"

class StampCompare
{
public:
	bool operator() ( Stamp* tile1, Stamp* tile2 ) const {
		int screenOffset = screenManager->getYTextureOffset();
		int tile1Y = 0;
		int tile2Y = 0;

		RECT* clip1 = tile1->getClippingRect();
		RECT* clip2 = tile2->getClippingRect();

		// Figure out tile1's lower y
		if ( tile1->getStampType() == Stamp::TILE ) {
			TileStamp* aTile = ( TileStamp* ) tile1;

            // Figure top y of tile
			int offset = aTile->getTileYOffset() / 2;
			tile1Y = offset * TILE_HEIGHT;
            if ( aTile->getTileYOffset() % 2 == 1 ) {
                tile1Y += TILE_HEIGHT / 2;
            }

            // Add on out tile height to get to bottom of tile
            tile1Y += ( TILE_HEIGHT / 2 );
		} else if ( tile1->getStampType() == Stamp::SPRITE ) {
			SpriteStamp* aSprite = ( SpriteStamp* ) tile1;
			D3DXVECTOR3* position = aSprite->getPosition();
			RECT* boundingBox = aSprite->getBoundingBox();

			tile1Y = ( int ) position->y;
			if ( boundingBox != NULL ) {
				tile1Y += boundingBox->bottom;
			} else {
				if ( clip1 != NULL ) {
					tile1Y += clip1->bottom - clip1->top;
				} else {
					tile1Y += tile1->getTotalHeight();
				}			
			}
		} else {
			GameError( "SpriteCompare Encountered Unknown Sprite Type" );
			return false;
		}

		// Figure out tile2's lower y
		if ( tile2->getStampType() == Stamp::TILE ) {
			TileStamp* aTile = ( TileStamp* ) tile2;

			int offset = aTile->getTileYOffset() / 2;
			tile2Y = offset * TILE_HEIGHT;
            if ( aTile->getTileYOffset() % 2 == 1 ) {
                tile2Y += TILE_HEIGHT / 2;
            }

             // Add on out tile height to get to bottom of tile
            tile2Y += ( TILE_HEIGHT / 2 );
		} else if ( tile2->getStampType() == Stamp::SPRITE ) {
			SpriteStamp* aSprite = ( SpriteStamp* ) tile2;
			D3DXVECTOR3* position = aSprite->getPosition();
			RECT* boundingBox = aSprite->getBoundingBox();

			tile2Y = ( int ) position->y;
			if ( boundingBox != NULL ) {
				tile2Y += boundingBox->bottom;
			} else {
				if ( clip2 != NULL ) {
					tile2Y += clip2->bottom - clip2->top;
				} else {
					tile2Y += tile2->getTotalHeight();
				}
			}
		} else {
			GameError( "SpriteCompare Encountered Unknown Sprite Type" );
			return false;
		}

		return tile1Y <= tile2Y;
	}
    //  bool operator() ( Stamp* tile1, Stamp* tile2 ) const {
	//	int screenOffset = screenManager->getYTextureOffset();
	//	int tile1Y = 0;
	//	int tile2Y = 0;
	//	RECT* clip1 = tile1->getClippingRect();
	//	RECT* clip2 = tile2->getClippingRect();

	//	// Figure out tile1's lower y
	//	if ( tile1->getStampType() == Stamp::TILE ) {
	//		TileStamp* aTile = ( TileStamp* ) tile1;

	//		int offset = ( aTile->getTileYOffset() - screenOffset ) / 2;
	//		tile1Y = offset * TILE_HEIGHT;
	//		if ( clip1 != NULL ) {
	//			tile1Y += clip1->bottom - clip1->top;
	//		} else {
	//			tile1Y += TILE_HEIGHT;
	//		}
	//		if ( aTile->getTileYOffset() % 2 == 1 ) {
	//			tile1Y -= ( TILE_HEIGHT / 2 );
	//		}
	//	} else if ( tile1->getStampType() == Stamp::SPRITE ) {
	//		SpriteStamp* aSprite = ( SpriteStamp* ) tile1;
	//		D3DXVECTOR3* position = aSprite->getPosition();

	//		tile1Y = ( int ) position->y;
	//		if ( clip1 != NULL ) {
	//			tile1Y += clip1->bottom - clip1->top;
	//		} else {
	//			tile1Y += tile1->getTotalHeight();
	//		}
	//	} else {
	//		GameError( "SpriteCompare Encountered Unknown Sprite Type" );
	//		return false;
	//	}

	//	// Figure out tile2's lower y
	//	if ( tile2->getStampType() == Stamp::TILE ) {
	//		TileStamp* aTile = ( TileStamp* ) tile2;

	//		int offset = ( aTile->getTileYOffset() - screenOffset ) / 2;
	//		tile2Y = offset * TILE_HEIGHT;
	//		if ( clip2 != NULL ) {
	//			tile2Y += clip2->bottom - clip2->top;
	//		} else {
	//			tile2Y += TILE_HEIGHT;
	//		}
	//		if ( aTile->getTileYOffset() % 2 == 1 ) {
	//			tile2Y -= ( TILE_HEIGHT / 2 );
	//		}
	//	} else if ( tile2->getStampType() == Stamp::SPRITE ) {
	//		SpriteStamp* aSprite = ( SpriteStamp* ) tile2;
	//		D3DXVECTOR3* position = aSprite->getPosition();

	//		tile2Y = ( int ) position->y;
	//		if ( clip2 != NULL ) {
	//			tile2Y += clip2->bottom - clip2->top;
	//		} else {
	//			tile2Y += tile2->getTotalHeight();
	//		}
	//	} else {
	//		GameError( "SpriteCompare Encountered Unknown Sprite Type" );
	//		return false;
	//	}

	//	return tile1Y <= tile2Y;
	//}
	void setScreen( ScreenManager* newScreen ) {
		screenManager = newScreen;
	}
private:
	ScreenManager* screenManager;
};
