/* ###
 * IP: GHIDRA
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ghidra.app.util.bin.format.golang.rtti.types;

import java.io.IOException;
import java.util.Set;

import ghidra.app.util.bin.format.golang.rtti.GoTypeManager;
import ghidra.app.util.bin.format.golang.structmapping.*;
import ghidra.app.util.viewer.field.AddressAnnotatedStringHandler;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.PointerDataType;

/**
 * {@link GoType} structure that defines a pointer.
 */
@StructureMapping(structureName = {"runtime.ptrtype", "internal/abi.PtrType"})
public class GoPointerType extends GoType {
	@FieldMapping
	@MarkupReference("getElement")
	private long elem;

	public GoPointerType() {
		// empty
	}

	/**
	 * {@return a reference to the element's type}
	 * @throws IOException if error reading data
	 */
	@Markup
	public GoType getElement() throws IOException {
		return programContext.getGoTypes().getType(elem);
	}

	@Override
	public DataType recoverDataType() throws IOException {
		GoTypeManager goTypes = programContext.getGoTypes();
		DataType elementDT = goTypes.getDataType(getElement());
		DataType self = goTypes.getDataType(this, DataType.class, true);
		if (self != null) {
			return self;
		}
		return new PointerDataType(elementDT, goTypes.getDTM());
	}

	@Override
	public boolean discoverGoTypes(Set<Long> discoveredTypes) throws IOException {
		if (!super.discoverGoTypes(discoveredTypes)) {
			return false;
		}
		GoType element = getElement();
		if (element != null) {
			element.discoverGoTypes(discoveredTypes);
		}
		return true;
	}
	
	@Override
	public String getPackagePathString() {
		String ppStr = super.getPackagePathString();
		if ( ppStr == null || ppStr.isEmpty() ) {
			try {
				GoType elemType = getElement();
				if ( elemType != null ) {
					ppStr = elemType.getPackagePathString();
				}
			} catch (IOException e) {
				// fall thru
			}
		}
		return ppStr;
	}

	@Override
	public String getStructureNamespace() throws IOException {
		GoType elementType = getElement();
		return elementType != null
				? elementType.getStructureNamespace()
				: super.getStructureNamespace();
	}

	@Override
	protected String getTypeDeclString() throws IOException {
		// type PointerTypeName *elementType
		String selfName = getName();
		String elemName = programContext.getGoTypes().getTypeName(elem);
		String defStr = "*" + elemName;
		String defStrWithLinks =
			"*" + AddressAnnotatedStringHandler.createAddressAnnotationString(elem, elemName);
		boolean hasName = !defStr.equals(selfName);
		return "type %s%s".formatted(hasName ? selfName + " " : "", defStrWithLinks);
	}

	@Override
	public boolean isValid() {
		return super.isValid() && isValidSize();
	}

	private boolean isValidSize() {
		long ptrBytesSize = typ.getPtrBytes();
		// Future: ptrbytessize will occasionally be 0 instead of ptrSize.  Need to nail down why
		return typ.getSize() == programContext.getPtrSize() &&
			(ptrBytesSize == 0 || ptrBytesSize == programContext.getPtrSize());
	}

}
