/*
 * Copyright 2018 Viritin.
 *
 * Licensed 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.vaadin.firitin;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.html.Paragraph;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import org.apache.commons.lang3.mutable.MutableInt;
import org.vaadin.firitin.components.button.VButton;
import org.vaadin.firitin.components.upload.UploadFileHandler;

import java.io.IOException;
import java.time.Instant;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mstahv
 */
@Route
public class UploadFileHandlerChunked extends VerticalLayout {

    private static final String DELIMITER = ";";
    private Instant lastUpdate;

    public UploadFileHandlerChunked() {

        add("""
        UploadFileHandler can chunk large files into smaller chunks
        and process them one by one, which is useful to bypass e.g. front-proxy
        size limits. Here configured chunk size is 100 KiB only for example/testing.
        1 MB (default) is a more reasonable size for chunking & probably fits into most
        max size defaults.
        """);

        Paragraph liveLogger = new Paragraph("...");
        UI ui = UI.getCurrent();

        MutableInt lineCount = new MutableInt(0);

        UploadFileHandler multiUploadFileHandler = new UploadFileHandler( (content, metadata) ->{
                    try {
                        int b = 0;
                        int count = 0;
                        while ((b = content.read()) != -1) {
                            if (b == "\n".getBytes()[0]) {
                                count++;
                                System.out.println("Found a line break in file " + metadata.fileName() + " at " + Instant.now());
                            }
                        }
                        lineCount.add(count);
                        String msg = "Counted " + lineCount + "lines. Last file name " + metadata.fileName() + " folderpath: " + metadata.folderPath();
                        lineCount.setValue(0);
                        return () -> Notification.show(msg);
                    } catch (IOException ex) {
                        Logger.getLogger(UploadFileHandlerChunked.class.getName()).log(Level.SEVERE, null, ex);
                        return () -> Notification.show(ex.getMessage());
                    }
                })
                .allowMultiple()
                .withChunkSize(100*1024); // 100 KiB chunks
        add(multiUploadFileHandler);
    }

}
