package org.kne.opencl64;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout.OfLong;
import java.util.Arrays;
import java.util.HashMap;

import club.doki7.ffm.ptr.LongPtr;
import club.doki7.ffm.ptr.PointerPtr;
import club.doki7.opencl.CLConstants;

public class OpenCLProperties extends HashMap<Long, Object> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected PointerPtr toPtrProperties() {
		//cl_context_properties ret=new cl_context_properties();
		PointerPtr ret=PointerPtr.allocate(Arena.ofAuto(),(size()+1)*2 );
		long off=0;
		for (Entry<Long, Object> entry : entrySet()) {
			long key = entry.getKey();
			Object val = entry.getValue();
			if(key==CLConstants.CONTEXT_PLATFORM) {
				ret.segment().setAtIndex(OfLong.JAVA_LONG, off, key);
				ret.write(off+1, ((OpenCLPlatform)val).getCl_platform_id());
				//ret.addProperty(key, ((OpenCLPlatform)val).getCl_platform_id());
			}
			off+=2;
		}
		//System.out.println(Arrays.toString( ret.segment().toArray(OfLong.JAVA_LONG)));
		return ret;
	}
	
	protected LongPtr toLongProperties() {
		//cl_context_properties ret=new cl_context_properties();
		LongPtr ret=LongPtr.allocate(Arena.ofAuto(),(size()+1)*2 );
		long off=0;
		for (Entry<Long, Object> entry : entrySet()) {
			long key = entry.getKey();
			Object val = entry.getValue();
			if(key==CLConstants.CONTEXT_PLATFORM) {
				ret.write( off+0, key);
				ret.write( off+1, ((OpenCLPlatform)val).getCl_platform_id().segment().address());
				//ret.addProperty(key, ((OpenCLPlatform)val).getCl_platform_id());
			}
			off+=2;
		}
		return ret;
	}
}
