/*
 *	Copyright (c) 2024 Scott Duensing, scott@kangaroopunch.com
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *	SOFTWARE.
 */


#include "oc_object.h"
#include "interpreter.h"
#include "state.h"
#include "memory.h"
#include "story.h"
#include "portme.h"
#include "messages.h"
#include "object.h"
#include "variable.h"


void opcodes_clear_attr(void) {
	uint32_t ptr    = objectPointerGet(__state.operands[0]);
	uint16_t attrId = __state.operands[1];

	// Zork 1 will trigger this on "go X" where "x" isn't a direction, so ignore it.
	if (ptr == 0) return;

	if (storyVersion() <= 3) {
		ptr += (attrId / 8);
		ZPOKE(ptr, ZPEEK(ptr) & ~(0x80 >> (attrId & 7)));
	} else {
		portDie(MSG_UNIMPLEMENTED);
	}
}


void opcodes_get_child(void) {
	uint16_t result = objectRelationGet(__state.operands[0], 6);
	variableStore(ZPEEK(__state.pc++), result);
	interpreterDoBranch(result != 0 ? 1 : 0);
}


void opcodes_get_next_prop(void) {
	uint16_t objId     = __state.operands[0];
	bool     firstProp = (__state.operands[1] == 0);
	uint16_t result    = 0;
	uint8_t  size      = 0;
	uint32_t ptr       = objectPropertyGet(objId, firstProp ? 0xFFFFFFFF : __state.operands[1], &size);

	if (ptr == 0) {
		portDie(MSG_OP_OBJ_MISSING_PROPERTY);
	}

	if (storyVersion() <= 3) {
		// 5 bits for the prop id.
		result = ZPEEK(ptr + (firstProp ? -1 : (int8_t)size)) & 0x1f;
	} else {
		portDie(MSG_UNIMPLEMENTED);
	}

	variableStore(ZPEEK(__state.pc++), result);
}


void opcodes_get_parent(void) {
	uint16_t result = objectRelationGet(__state.operands[0], 4);
	variableStore(ZPEEK(__state.pc++), result);
}


void opcodes_get_prop(void) {
	uint16_t objId  = __state.operands[0];
	uint16_t propId = __state.operands[1];
	uint16_t result = 0;
	uint8_t  size   = 0;
	uint32_t ptr    = objectPropertyGet(objId, propId, &size);

	if (ptr == 0) {
		result = objectPropertyDefaultGet(propId);
	} else if (size == 1) {
		result = ZPEEK(ptr);
	} else {
		result = ZPEEKW(ptr);
	}

	variableStore(ZPEEK(__state.pc++), result);
}


void opcodes_get_prop_addr(void) {
	uint16_t objId  = __state.operands[0];
	uint16_t propId = __state.operands[1];

	variableStore(ZPEEK(__state.pc++), objectPropertyGet(objId, propId, 0));
}


void opcodes_get_prop_len(void) {
	uint16_t result = 0;
	uint8_t  info;

	// This must return 0 if OP0 is zero, to avoid a bug in older Infocom games.
	if (__state.operands[0] != 0) {
		if (storyVersion() <= 3) {
			info = ZPEEK(__state.operands[0] - 1);
			result = ((info >> 5) & 0x7) + 1;  // 3 bits for property size.
		} else {
			portDie(MSG_UNIMPLEMENTED);
		}
	}

	variableStore(ZPEEK(__state.pc++), result);
}


void opcodes_get_sibling(void) {
	uint16_t result = objectRelationGet(__state.operands[0], 5);
	variableStore(ZPEEK(__state.pc++), result);
	interpreterDoBranch(result != 0 ? 1 : 0);
}


void opcodes_insert_obj(void) {
	uint32_t objPtr = objectPointerGet(__state.operands[0]);
	uint32_t dstPtr = objectPointerGet(__state.operands[1]);

	if (storyVersion() <= 3) {
		// Take object out of tree.
		objectUnparent(__state.operands[0]);
		// Reinsert in right spot.
		ZPOKE(objPtr + 4, __state.operands[1]);
		ZPOKE(objPtr + 5, ZPEEK(dstPtr + 6));
		ZPOKE(dstPtr + 6, __state.operands[0]);
	} else {
		portDie(MSG_UNIMPLEMENTED);
	}
}


void opcodes_put_prop(void) {
	uint8_t  size = 0;
	uint32_t ptr  = objectPropertyGet(__state.operands[0], __state.operands[1], &size);

	if (ptr == 0) portDie(MSG_OP_OBJ_MISSING_PROPERTY);

	if (size == 1) {
		ZPOKE(ptr, (__state.operands[2] & 0xff));
	} else {
		ZPOKEW(ptr, __state.operands[2]);
	}
}


void opcodes_remove_obj(void) {
	uint16_t objId  = __state.operands[0];
	uint32_t objPtr = objectPointerGet(objId);

	if (storyVersion() <= 3) {
		// Remove object from tree.
		objectUnparent(objId);
		// Clear out object's relationships.
		ZPOKE(objPtr + 4, 0);  // Parent.
		ZPOKE(objPtr + 5, 0);  // Sibling.
	} else {
		portDie(MSG_UNIMPLEMENTED);
	}
}


void opcodes_set_attr(void) {
	uint32_t ptr    = objectPointerGet(__state.operands[0]);
	uint16_t attrId = __state.operands[1];

	if (storyVersion() <= 3) {
		ptr += (attrId / 8);
		ZPOKE(ptr, ZPEEK(ptr) | 0x80 >> (attrId & 7));
	} else {
		portDie(MSG_UNIMPLEMENTED);
	}
}


void opcodes_test_attr(void) {
	uint32_t ptr    = objectPointerGet(__state.operands[0]);
	uint16_t attrId = __state.operands[1];

	if (storyVersion() <= 3) {
		ptr += (attrId / 8);
		interpreterDoBranch((ZPEEK(ptr) & (0x80 >> (attrId & 7))) ? 1 : 0);
	} else {
		portDie(MSG_UNIMPLEMENTED);
	}
}
