/*******************************************************************************
 * Copyright (c) 2015, 2018 Red Hat.
 * 
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Red Hat - Initial Contribution
 *******************************************************************************/
package org.eclipse.linuxtools.internal.docker.ui.wizards;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.wizard.Wizard;

public class ImageBuild extends Wizard {

	private ImageBuildPage mainPage;
	private String imageName;
	private IPath directory;
	private int lines;

	public ImageBuild() {
		super();
		setWindowTitle(WizardMessages.getString("ImageBuild.title")); //$NON-NLS-1$
	}

	public String getImageName() {
		return imageName;
	}

	public IPath getDirectory() {
		return directory;
	}

	public int getNumberOfLines() {
		return lines;
	}

	@Override
	public void addPages() {
		mainPage = new ImageBuildPage();
		addPage(mainPage);
	}

	@Override
	public boolean canFinish() {
		return mainPage.isPageComplete();
	}

	private int numberOfLines() throws IOException {
		String fileName = directory.append("Dockerfile").toString(); //$NON-NLS-1$
		int count = 0;
		boolean empty = false;
		try (InputStream is = new BufferedInputStream(
				new FileInputStream(fileName))) {
			byte[] c = new byte[1024];
			int readChars = 0;
			while ((readChars = is.read(c)) != -1) {
				empty = false;
				for (int i = 0; i < readChars; ++i) {
					if (c[i] == '\n') {
						++count;
					}
				}
			}
		}
		return (count == 0 && !empty) ? 1 : count;
	}

	@Override
	public boolean performFinish() {
		imageName = mainPage.getImageName();
		directory = new Path(mainPage.getDirectory());

		try {
			lines = numberOfLines();
		} catch (IOException e) {
			// do nothing
		}

		return true;
	}

}
