#include "tl_tui_bc_error_msg.h"
#include "TZ_Vendor_debug_tl.h"

#include "TuiRotation.h"
#include "TuiPng.h"
#include "TuiScreen.h"
#include "TuiScreenProperty.h"
#include "TuiControl.h"
#include "TuiState.h"
#include "TuiLayout.h"

static RotationType gRotationType = NONE_ROTATION_TYPE;

void setRotationType(RotationType type) {
	gRotationType = type;
	DBG_LOG("%s rotation type : %d", LOG_TAG, type);
}

RotationType getRotationType() {
	if (getTuiMode() == PINPAD_TUI_MODE && !isTuiPinMode()) {
		return NONE_ROTATION_TYPE;
	}
	return gRotationType;
}

void rotationCoordinates(uint32_t* x_out, uint32_t* y_out, uint8_t* pngData) {
	if (getRotationType() == NONE_ROTATION_TYPE && getScreenType() != SCREEN_TYPE_Q2_MAIN)
		return;

	uint32_t width=0, height=0;

	getPngWidthAndHeight(pngData, &width, &height); // includes rotation logic.

	uint32_t xTemp;
	switch(getRotationType()) {
		case COUNTER_CLOCKWISE_ROTATION_TYPE:
		    xTemp = *x_out;
		    *x_out = *y_out;
			*y_out = getScreenHeight() - width - xTemp;
		break;
		case CLOCKWISE_ROTATION_TYPE:
			xTemp = getScreenWidth() - *y_out;
			*y_out = *x_out;
			*x_out = xTemp - height;
		break;
		case NONE_ROTATION_TYPE:
			if (getScreenType() == SCREEN_TYPE_Q2_MAIN) {
                *y_out = getScreenHeight() - height - *y_out;
                *x_out = getScreenWidth() - *x_out - width;
        	}
			break;
		default:
			TTY_LOG("%s unknown rotation type : %d", LOG_TAG, gRotationType);
			break;
	}
	return;

}
	
