/**
 * Copyright © CENRA (2016)
 *
 * remi.clement@espaces-naturels.fr
 * 
 * This software is a multi-platform and portable application based on 
 * the scientifical toolbox to monitor wetlands. With this toolbox it’s 
 * possible to calculate indicators «RhoMéO» to monitor quality, 
 * fonctionalities and urban and agricultural pressures of wetlands. 
 * Indicators are calculated using naturalist data entered and/or imported,
 * and geographic data downloaded from a geographic server.
 * 
 * This software is governed by the CeCILL license under French law and
 * abiding by the rules of distribution of free software.  You can  use, 
 * modify and/ or redistribute the software under the terms of the CeCILL
 * license as circulated by CEA, CNRS and INRIA at the following URL
 * "http://www.cecill.info". 
 * 
 * As a counterpart to the access to the source code and  rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty  and the software's author,  the holder of the
 * economic rights,  and the successive licensors  have only  limited
 * liability. 
 * 
 * In this respect, the user's attention is drawn to the risks associated
 * with loading,  using,  modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean  that it is complicated to manipulate,  and  that  also
 * therefore means  that it is reserved for developers  and  experienced
 * professionals having in-depth computer knowledge. Users are therefore
 * encouraged to load and test the software's suitability as regards their
 * requirements in conditions enabling the security of their systems and/or 
 * data to be ensured and,  more generally, to use and operate it in the 
 * same conditions as regards security. 
 * 
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL license and that you accept its terms.
*/
package fr.cenra.rhomeo.core;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.regex.Pattern;
import org.apache.sis.util.Numbers;
import org.apache.sis.util.ObjectConverter;
import org.apache.sis.util.ObjectConverters;
import org.geotoolkit.util.collection.CloseableIterator;

/**
 * Read a given CSV file, trying to create a list of pojo of the given type to hold
 * its data.
 *
 * @author Alexis Manin (Geomatys)
 */
public class CSVDecoder<T> extends CSVMapper<T> {

    /**
     * At runtime, we detect doubled double-quote in escaped strings, to replace
     * them by single ones.
     */
    static final Pattern DOUBLED_DOUBLE_QUOTE = Pattern.compile("\"\"");
    static final String DOUBLE_QUOTE_REPLACEMENT = "\"";

    /**
     * Contains all cell value decoders. For each line read, we take each column
     * value, and give it to an appropriate converter which will put them into
     * the right properties of a target Pojo.
     */
    private final BiConsumer<T, String>[] propertySetters;

    /**
     * Create a new decoder whose aim is to read data hold by input file, and
     * return it as a list of pojos of the given type.
     * @param input The file to read. MUST BE READABLE
     * @param beanClass The class defining POJO object to use as data container.
     * @throws IOException If we cannot analyze input file header.
     * @throws IntrospectionException If we cannot analyze input pojo type.
     */
    public CSVDecoder(Path input, Class<T> beanClass) throws IOException, IntrospectionException {
        this(input, beanClass, null);
    }

    /**
     * Create a new decoder whose aim is to read data hold by input file, and
     * return it as a list of pojos of the given type.
     * @param input The file to read. MUST BE READABLE
     * @param beanClass The class defining POJO object to use as data container.
     * to read.
     * @param fileEncoding Encoding to use for reading.
     * @throws IOException If we cannot analyze input file header.
     * @throws IntrospectionException If we cannot analyze input pojo type.
     */
    public CSVDecoder(Path input, Class<T> beanClass, Charset fileEncoding) throws IOException, IntrospectionException {
        this(input, beanClass, fileEncoding, null);
    }

    public CSVDecoder(Path input, Class<T> beanClass, Charset fileEncoding, Set<String> ignorableProperties) throws IOException, IntrospectionException {
        this(input, beanClass, null, fileEncoding, ignorableProperties);
    }

    public CSVDecoder(Path input, Class<T> beanClass, final Character separator, final Charset fileEncoding, final Set<String> ignorableProperties) throws IOException, IntrospectionException {
        super(input, beanClass, separator, fileEncoding, ignorableProperties);

        if (!Files.isReadable(input)) {
            throw new IOException("Given file is not readable : ".concat(input.toString()));
        }

        /* Prepare data conversion. For each property present in input bean type,
         * we ensure that a read method is available. Then, we analyze its return
         * type. We try to find an appropriate converter to get a string
         * representation writable in CSV file.
         */
        final PropertyDescriptor[] pDescriptors = beanInfo.getPropertyDescriptors();
        propertySetters = new BiConsumer[pDescriptors.length];
        for (int i = 0; i < pDescriptors.length; i++) {
            final PropertyDescriptor p = pDescriptors[i];
            final Method writeMethod = p.getWriteMethod();
            if (writeMethod == null)
                throw new IllegalArgumentException(
                        new StringBuilder("Property ")
                        .append(p.getName())
                        .append(" from class ")
                        .append(beanClass.getCanonicalName())
                        .append("is not writable.")
                        .toString());

            writeMethod.setAccessible(true);
            final Class propertyType = p.getPropertyType();
            final Function<String, ?> conv = createPropertyConverter(propertyType);

            final BiConsumer<T, Object> setter;
            setter = (pojo, value) -> {
                try {
                    writeMethod.invoke(pojo, value);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RhomeoRuntimeException(e);
                }
            };

            if (conv == null) {
                propertySetters[i] = (BiConsumer) setter;
            } else {
                propertySetters[i] = (pojo, strValue) -> setter.accept(pojo, conv.apply(strValue));
            }
        }
    }

    /**
     * Read given string (must be a line in the csv, not header) and map its
     * data into a POJO.
     *
     * @param line A line of the CSV file.
     * @param lineNumber Number of the read line. It serves to build error messages.
     * @return A Pojo containing line data. If input line is blank, a null value is returned.
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws IOException
     */
    private T decodeLine(final String line, int lineNumber) throws InstantiationException, IllegalAccessException, IOException {
        final List<CharBuffer> groups = splitByProperty(line);
        if (groups.isEmpty())
            return null;
        final T newInstance = dataType.newInstance();
        CharBuffer buffer;
        for (int i = 0; i < groups.size(); i++) {
            buffer = groups.get(i);
            try {
                if (buffer.hasRemaining() && propertyOrder[i] >= 0) {
                    propertySetters[propertyOrder[i]].accept(newInstance, unescape(buffer));
                }
            } catch (Exception e) {
                throwWithDescription(lineNumber, i, buffer, e);
            }
        }

        return newInstance;
    }

    /**
     *
     * @return A list of POJO, each one containing data of a line. Pojos are
     * sorted according to line number in the source file.
     *
     * @throws IOException Reading fails.
     * @throws InstantiationException Cannot create a new pojo (Mostly no default builder found).
     * @throws IllegalAccessException A property of the pojo type cannot be used by the decoder.
     */
    public List<T> decode() throws IOException, InstantiationException, IllegalAccessException {
        try (final BufferedReader reader = Files.newBufferedReader(target, charset)) {
            // No data ...
            String line = reader.readLine();
            if (line == null) {
                return new ArrayList<>();
            } else {
                updatePropertyOrder(line);
            }

            // Do not check header content. It has been done at built.
            final List<T> result = new ArrayList<>();
            int lineCount = 1;
            T read;
            while ((line = reader.readLine()) != null) {
                read = decodeLine(line, lineCount++);
                if (read != null)
                    result.add(read);
            }
            return result;
        }
    }

    /**
     * /!\ You MUST CLOSE the iterator once you're done with it.
     * @return An iterator which will decode lines of the source file as we iterate
     * on them.
     */
    public CloseableIterator<T> decodeLazy() {
        return new CloseableIterator<T>() {

            BufferedReader fileReader;
            T next;
            int lineCount = 0;

            @Override
            public boolean hasNext() {
                if (next == null) {
                    try {
                        // first time, check header.
                        if (fileReader == null) {
                            fileReader = Files.newBufferedReader(target, charset);
                            final String header = fileReader.readLine();
                            lineCount++;
                            // check header
                            if (header == null)
                                return false;
                            else
                                updatePropertyOrder(header);
                        }

                        // read next line
                        String line;
                        while (next == null) {
                            line = fileReader.readLine();
                            if (line == null)
                                break;
                            next = decodeLine(line, ++lineCount);
                        }
                    } catch (IOException e) {
                        throw new RhomeoRuntimeException(e.getMessage(), e);
                    } catch (InstantiationException | IllegalAccessException ex) {
                        RhomeoCore.LOGGER.log(Level.WARNING, "Cannot set output properties.", ex);
                    }
                }

                return next != null;
            }

            @Override
            public T next() {
                if (next == null) {
                    throw new IllegalStateException("No more element available !");
                }

                try {
                    return next;
                } finally {
                    next = null;
                }
            }

            @Override
            public void close() {
                if (fileReader != null) {
                    try {
                        fileReader.close();
                    } catch (IOException ex) {
                        RhomeoCore.LOGGER.log(Level.WARNING, "A reader cannot be closed on file ".concat(target.toString()), ex);
                    }
                }
            }

            @Override
            protected void finalize() throws Throwable {
                super.finalize();
                close();
            }
        };
    }

    /**
     * Analyze a line of input CSV file, and split its character by column. To
     * find columns, we search for separators which are not enclosed in an
     * escaped character sequence.
     *
     * NOTE : return char buffers are already trimmed, but are still escaped (if any).
     *
     * @param line input line to find property indices for.
     * @return A list of buffer. Each represents a part of the original string.
     * They're sorted by order of appearance in the input string.
     */
    private List<CharBuffer> splitByProperty(final String line) {
        final CharBuffer cLine = CharBuffer.wrap(line);
        final ArrayList<CharBuffer> groups = new ArrayList<>();
        int lastGroup = cLine.position();
        boolean escapeMode = false;
        char c;
        for (int i = cLine.position(); i < cLine.limit(); i++) {
            c = cLine.get(i);
            if (c == '"') {
                escapeMode = !escapeMode;// If two double quotes follows, escape mode is reactivated.
            } else if (!escapeMode) {
                if (c == separator) {
                    groups.add(cLine.subSequence(lastGroup, i));
                    lastGroup = i + 1;
                } else if (Character.isWhitespace(c) && i == lastGroup) {
                    lastGroup++; // remove trailing spaces
                } else if (c == COMMENT_CHAR) {
                    break; // A comment have been found.
                }
            }
        }

        // last value on the line
        if (lastGroup < cLine.limit()) {
            groups.add(cLine.subSequence(lastGroup, cLine.remaining()));
        }

        return groups;
    }

    /**
     *
     * @param input A char buffer representing one cell data.
     * @return A string which is the cell data, from which we remove trailing
     * double-quotes and doubled ones if needed.
     */
    private String unescape(final CharBuffer input) {
        if (!input.hasRemaining()) {
            return "";
        }

        if (input.get(input.position()) == '"' && input.get(input.limit() -1) == '"') {
            if (input.remaining() < 3) // Empty escaped sequence.
                return "";
            return DOUBLED_DOUBLE_QUOTE.matcher(input.subSequence(1, input.remaining() -1)).replaceAll(DOUBLE_QUOTE_REPLACEMENT);
        }

        return input.toString();
    }

    /**
     * Search for an appropriate converter between given type and string.
     *
     * Note : for numbers and primitives, we use direct conversion using wrapping
     * class utilities. We make a distinction between primitive and wrapping class
     * case, as the first one cannot hold null value information.
     *
     * @param inputType Type of object to convert.
     * @return A ready-to-use converter, or null if we do not need any.
     */
    private Function<String, ?> createPropertyConverter(final Class inputType) {
        final Function<String, ?> result;
        if (CharSequence.class.isAssignableFrom(inputType)) {
            return null;
            // boolean
        } else if (Boolean.class.isAssignableFrom(inputType)) {
            result = input -> Boolean.valueOf(input);
        } else if (Boolean.TYPE.isAssignableFrom(inputType)) {
            result = input -> Boolean.parseBoolean(input);
            // character
        } else if (Character.class.isAssignableFrom(inputType)) {
            result = input -> input == null || input.isEmpty()? null : input.charAt(0);
        } else if (Character.TYPE.isAssignableFrom(inputType)) {
            result = input -> input == null || input.isEmpty()? '?' : input.charAt(0);
            // bytes
        } else if (Byte.class.isAssignableFrom(inputType)) {
            result = input -> Byte.valueOf(input);
        } else if (Byte.TYPE.isAssignableFrom(inputType)) {
            result = input -> input == null || input.isEmpty()? 0 : Byte.parseByte(input);
            // short
        } else if (Short.class.isAssignableFrom(inputType)) {
            result = input -> Short.valueOf(input);
        } else if (Short.TYPE.isAssignableFrom(inputType)) {
            result = input -> input == null || input.isEmpty()? 0 : Short.parseShort(input);
            // integer
        } else if (Integer.class.isAssignableFrom(inputType)) {
            result = input -> Integer.valueOf(input);
        } else if (Integer.TYPE.isAssignableFrom(inputType)) {
            result = input -> input == null || input.isEmpty()? 0 : Integer.parseInt(input);
            // long
        } else if (Long.class.isAssignableFrom(inputType)) {
            result = input -> Long.valueOf(input);
        } else if (Long.TYPE.isAssignableFrom(inputType)) {
            result = input -> input == null || input.isEmpty()? 0 : Long.parseLong(input);
            // float
        } else if (Float.class.isAssignableFrom(inputType)) {
            result = input -> Float.valueOf(input);
        } else if (Float.TYPE.isAssignableFrom(inputType)) {
            result = input -> input == null || input.isEmpty()? Float.NaN : Float.parseFloat(input);
            // double
        } else if (Double.class.isAssignableFrom(inputType)) {
            result = input -> Double.valueOf(input);
        } else if (Double.TYPE.isAssignableFrom(inputType)) {
            result = input -> input == null || input.isEmpty()? Double.NaN : Double.parseDouble(input);
        } else {
            final Class tmpClass = Numbers.primitiveToWrapper(inputType);
            final ObjectConverter<? super String, ?> sisConverter = ObjectConverters.find(String.class, tmpClass);
            result = input -> (input == null || input.isEmpty())? null : sisConverter.apply(input);
        }

        return result;
    }
}
