/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.sysds.runtime.transform.decode;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.sysds.common.Types.ValueType;
import org.apache.sysds.runtime.frame.data.FrameBlock;
import org.apache.sysds.runtime.matrix.data.MatrixBlock;

/**
 * Simple atomic decoder for passing through numeric columns to the output.
 * This is required for block-wise decoding. 
 *  
 */
public class DecoderPassThrough extends Decoder
{
	private static final long serialVersionUID = -8525203889417422598L;
	
	private int[] _dcCols = null;
	private int[] _srcCols = null;

	protected DecoderPassThrough(ValueType[] schema, int[] ptCols, int[] dcCols) {
		this(schema, ptCols, dcCols, null);
	}

	protected DecoderPassThrough(ValueType[] schema, int[] ptCols, int[] dcCols, int[] hashCols) {
		super(schema, ptCols);
		_dcCols = dcCols;
		_dcHashCols = hashCols;
	}

	public DecoderPassThrough() { super(null, null); }

	@Override
	public FrameBlock decode(MatrixBlock in, FrameBlock out) {
		out.ensureAllocatedColumns(in.getNumRows());
		decode(in, out, 0, in.getNumRows());
		return out;
	}
	
	@Override
	public void decode(MatrixBlock in, FrameBlock out, int rl, int ru) {
		int clen = Math.min(_colList.length, out.getNumColumns());
		for(int i = rl; i < ru; i++) {
			for(int j = 0; j < clen; j++) {
				int srcColID = _srcCols[j] - 1;
				int tgtColID = _colList[j] - 1;
				double val = in.get(i, srcColID);
				out.getColumn(tgtColID).set(i, val);
			}
		}
	}

	@Override
	public Decoder subRangeDecoder(int colStart, int colEnd, int dummycodedOffset) {
		List<Integer> colList = new ArrayList<>();
		List<Integer> dcList = new ArrayList<>();
		List<Integer> srcList = new ArrayList<>();
		
		for (int i = 0; i < _colList.length; i++) {
			int colID = _colList[i];
			if (colID >= colStart && colID < colEnd) {
				colList.add(colID - (colStart - 1));
				srcList.add(_srcCols[i] - dummycodedOffset);
			}
		}
		
		Arrays.stream(_dcCols)
			.filter(c -> c >= colStart && c < colEnd)
			.forEach(c -> dcList.add(c));
		
		if (colList.isEmpty())
			// empty decoder -> return null
			return null;
		
		DecoderPassThrough decoder = new DecoderPassThrough(Arrays.copyOfRange(_schema, colStart - 1, colEnd - 1),
			colList.stream().mapToInt(i -> i).toArray(),
			dcList.stream().mapToInt(i -> i).toArray());
		decoder._srcCols = srcList.stream().mapToInt(i -> i).toArray();
		return decoder;
	}
	
	@Override
	public void initMetaData(FrameBlock meta) {
		_srcCols = buildSrcCols(meta, _dcCols);
	}

	@Override
	public void writeExternal(ObjectOutput os)
		throws IOException
	{
		super.writeExternal(os);
		os.writeInt(_srcCols.length);
		for(int i = 0; i < _srcCols.length; i++)
			os.writeInt(_srcCols[i]);

		os.writeInt(_dcCols == null ? 0 : _dcCols.length);
		for(int i = 0; _dcCols != null && i < _dcCols.length; i++)
			os.writeInt(_dcCols[i]);
	}

	@Override
	public void readExternal(ObjectInput in)
		throws IOException
	{
		super.readExternal(in);
		_srcCols = new int[in.readInt()];
		for(int i = 0; i < _srcCols.length; i++)
			_srcCols[i] = in.readInt();

		_dcCols = new int[in.readInt()];
		for(int i = 0; i < _dcCols.length; i++)
			_dcCols[i] = in.readInt();
	}
}
