package nl.nikhef.sfp.ddmi;

import java.util.logging.Logger;

import nl.nikhef.sfp.SFPManager;
import nl.nikhef.tools.Utils;

public class PageDataSource extends DataSource {

	private static final Logger LOG = Logger.getLogger(PageDataSource.class.getSimpleName());
	
	
	private final DataSource _parent;
	private final int        _pageNo;
	
	public PageDataSource(DataSource parent, int pageNo, int start, int end) 
	{
		super(start, end);
		assert(parent != null);
		_parent = parent;
		_pageNo = pageNo;
		
	}
	
	public boolean isValid(DDMIContext ctx) 
	{
		return _parent.isValid(ctx);
	}

	
	
	private void selectPage(DDMIContext ctx) 
	{
		LOG.info("Selecting page " + _pageNo);
		if (_parent.getPageSelect() == -1)
			throw new RuntimeException("Parent has no page select!");
		
		byte[] pageData = new byte[] { (byte)_pageNo };
		
		_parent.writeMedia(ctx, _parent.getPageSelect(), pageData);
		
	}
	
	@Override
	byte[] readMedia(DDMIContext ctx, int off, int len) 
	{
		selectPage(ctx);
		byte[] dta = _parent.readMedia(ctx, off, len);
		// Utils.dumpHex("Page: " + _pageNo + ", Reading " + off + ":" + len, dta);
		return dta;
	}

	@Override
	void writeMedia(DDMIContext ctx, int off, byte[] data) 
	{
		selectPage(ctx);
		Utils.dumpHex("Page: " + _pageNo + ", Writign " + off + ":" + data.length, data);
		_parent.writeMedia(ctx, off, data);
	}

	
	@Override
	public String getPath() {
		return _parent.getPath() + "_page" + _pageNo;
	}
}
