/* 
 * 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.
 */

namespace ParquetSharp.Column.Values.Dictionary
{
    using System.IO;
    using ParquetSharp.Bytes;
    using ParquetSharp.Column.Impl;
    using ParquetSharp.Column.Values.Rle;
    using ParquetSharp.External;
    using ParquetSharp.IO;
    using ParquetSharp.IO.Api;

    /**
     * Reads values that have been dictionary encoded
     *
     * @author Julien Le Dem
     *
     */
    public class DictionaryValuesReader : ValuesReader
    {
        private static readonly Log LOG = Log.getLog(typeof(DictionaryValuesReader));

        private ByteBufferInputStream @in;

        private Dictionary dictionary;

        private RunLengthBitPackingHybridDecoder decoder;

        public DictionaryValuesReader(Dictionary dictionary)
        {
            this.dictionary = dictionary;
        }

        public override void initFromPage(int valueCount, ByteBuffer page, int offset)
        {
            this.@in = new ByteBufferInputStream(page, offset, page.limit() - offset);
            if (page.limit() - offset > 0)
            {
                if (Log.DEBUG)
                    LOG.debug("init from page at offset " + offset + " for length " + (page.limit() - offset));
                int bitWidth = BytesUtils.readIntLittleEndianOnOneByte(@in);
                if (Log.DEBUG) LOG.debug("bit width " + bitWidth);
                decoder = new RunLengthBitPackingHybridDecoder(bitWidth, @in);
            }
            else
            {
                decoder = new EmptyPageDecoder(1, @in);
            }
        }

        class EmptyPageDecoder : RunLengthBitPackingHybridDecoder
        {
            public EmptyPageDecoder(int bitWidth, InputStream @in)
                : base(bitWidth, @in)
            {
            }

            public override int readInt()
            {
                throw new IOException("Attempt to read from empty page");
            }
        }

        public override int readValueDictionaryId()
        {
            try
            {
                return decoder.readInt();
            }
            catch (IOException e)
            {
                throw new ParquetDecodingException(e);
            }
        }

        public override Binary readBytes()
        {
            try
            {
                return dictionary.decodeToBinary(decoder.readInt());
            }
            catch (IOException e)
            {
                throw new ParquetDecodingException(e);
            }
        }

        public override float readFloat()
        {
            try
            {
                return dictionary.decodeToFloat(decoder.readInt());
            }
            catch (IOException e)
            {
                throw new ParquetDecodingException(e);
            }
        }

        public override double readDouble()
        {
            try
            {
                return dictionary.decodeToDouble(decoder.readInt());
            }
            catch (IOException e)
            {
                throw new ParquetDecodingException(e);
            }
        }

        public override int readInteger()
        {
            try
            {
                return dictionary.decodeToInt(decoder.readInt());
            }
            catch (IOException e)
            {
                throw new ParquetDecodingException(e);
            }
        }

        public override long readLong()
        {
            try
            {
                return dictionary.decodeToLong(decoder.readInt());
            }
            catch (IOException e)
            {
                throw new ParquetDecodingException(e);
            }
        }

        public override void skip()
        {
            try
            {
                decoder.readInt(); // Type does not matter as we are just skipping dictionary keys
            }
            catch (IOException e)
            {
                throw new ParquetDecodingException(e);
            }
        }
    }
}
