#include "Vendor_Interface.h"
#include "TZ_Vendor_tl.h"
#include "TZ_Vendor_debug_tl.h"
#include "tl_tui_bc_error_msg.h"

#include "TuiControl.h"
#include "TuiImageView.h"

#include <string.h>

Control* initImageView(Control* control, uint32_t id, Location loc, uint32_t width, uint32_t height, uint8_t* clearImg, uint32_t clearImgLen, uint8_t* drawImg, uint32_t drawImgLen) {
	control->id = id;
	control->location = loc;
	control->width = width;
	control->height = height;
	control->eventResource = clearImg;
	control->eventResourceSize = clearImgLen;
	control->originResource = drawImg;
	control->originResourceSize = drawImgLen;
	control->extraResource = NULL;
	control->extraResourceSize = 0;
	control->isDrawingWhenAdded = 1;
	control->isTouchable = 0;
	
	return control;
}

void addImageView(Control *control) {
	control->kindOfControl = IMAGE_VIEW_CONTROL_TYPE;
	control->execute = NULL;
	
	if (control->isDrawingWhenAdded == 1) {
		control->state = ENABLE_CONTROL_STATE;
		drawImageView(control);
	} else {
		control->state = INVISIBLE_CONTROL_STATE;
	}
	addControl(control);

	return;
}

void drawImageView(Control* control) {
	DBG_LOG("drawImageView()");

	switch (control->state) {
	case ENABLE_CONTROL_STATE:
		if (control->originResource != NULL) {
			DBG_LOG("originResourceSize() : %d", control->originResourceSize);
			drawImage(control->location.x1, control->location.y1, control->originResource, control->originResourceSize);
		} else {
			TTY_LOG("DrawResource for ImageView is NULL");
		}
		break;
	case INVISIBLE_CONTROL_STATE:
		clearImageView(control);
		break;
	default:
		TTY_LOG("%s drawImageView unknown state! : %d - control:%d", LOG_TAG, control->state, control->kindOfControl);
		return;
	}
}

void clearImageView(Control* control) {
	DBG_LOG("clearImageView()");

	/*
	if (control->state == INVISIBLE_CONTROL_STATE) {
		DBG_LOG("Control state is already INVISIBLE_CONTROL_STATE");
		return;
	}
	*/

	if (control->eventResource != NULL) {
		DBG_LOG("eventResourceSize() : %d", control->eventResourceSize);
		drawImage(control->location.x1, control->location.y1, control->eventResource, control->eventResourceSize);
		control->state = INVISIBLE_CONTROL_STATE;
	} else {
		TTY_LOG("ClearResource for ImageView is NULL");
	}
}

void updateImageViewControl(Control* dest, Control* src) {
	DBG_LOG("updateImageViewControl");
	clearImageView(dest);

	dest->location = src->location;
	dest->width = src->width;
	dest->height = src->height;
	if (src->eventResource != NULL) {
		if (dest->eventResource != NULL) {
			TZ_free(dest->eventResource);
		}
		dest->eventResource = TZ_malloc(src->eventResourceSize);
		memcpy(dest->eventResource, src->eventResource, src->eventResourceSize);
		dest->eventResourceSize = src->eventResourceSize;
	}
	if (src->originResource != NULL) {
		if (dest->originResource != NULL) {
			TZ_free(dest->originResource);
		}
		dest->originResource = TZ_malloc(src->originResourceSize);
		memcpy(dest->originResource, src->originResource, src->originResourceSize);
		dest->originResourceSize = src->originResourceSize;
	}
	dest->state = ENABLE_CONTROL_STATE;
	drawImageView(dest);
}
