import java.io.IOException;

import org.netbeans.lib.cvsclient.Client;
import org.netbeans.lib.cvsclient.admin.StandardAdminHandler;
import org.netbeans.lib.cvsclient.command.CommandAbortedException;
import org.netbeans.lib.cvsclient.command.CommandException;
import org.netbeans.lib.cvsclient.command.DefaultFileInfoContainer;
import org.netbeans.lib.cvsclient.command.FileInfoContainer;
import org.netbeans.lib.cvsclient.command.GlobalOptions;
import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;
import org.netbeans.lib.cvsclient.command.log.LogInformation;
import org.netbeans.lib.cvsclient.command.log.LogInformation.Revision;
import org.netbeans.lib.cvsclient.command.log.RlogCommand;
import org.netbeans.lib.cvsclient.connection.AuthenticationException;
import org.netbeans.lib.cvsclient.connection.PServerConnection;
import org.netbeans.lib.cvsclient.connection.StandardScrambler;
import org.netbeans.lib.cvsclient.event.BinaryMessageEvent;
import org.netbeans.lib.cvsclient.event.CVSAdapter;
import org.netbeans.lib.cvsclient.event.CVSListener;
import org.netbeans.lib.cvsclient.event.FileAddedEvent;
import org.netbeans.lib.cvsclient.event.FileInfoEvent;
import org.netbeans.lib.cvsclient.event.FileRemovedEvent;
import org.netbeans.lib.cvsclient.event.FileToRemoveEvent;
import org.netbeans.lib.cvsclient.event.FileUpdatedEvent;
import org.netbeans.lib.cvsclient.event.MessageEvent;
import org.netbeans.lib.cvsclient.event.ModuleExpansionEvent;
import org.netbeans.lib.cvsclient.event.TerminationEvent;

public class Main {

	public static void main(String[] args) {
		// PServerとの接続
		PServerConnection c = new PServerConnection();
//		c.setHostName("jedit.cvs.sourceforge.net");		// PServerのホスト名またはIP(jEdit)
//		c.setRepository("/cvsroot/jedit");				// 接続するするリポジトリ名(jEdit)
		c.setHostName("jhotdraw.cvs.sourceforge.net");	// PServerのホスト名またはIP(JHotDraw)
		c.setRepository("/cvsroot/jhotdraw");			// 接続するするリポジトリ名(JHotDraw)
		Client client = openConnection(c);
		client.setLocalPath("./tmp");
		
		CVSAdapter adapter = new CVSAdapter() {
			@Override
			public void commandTerminated(TerminationEvent arg0) {
				super.commandTerminated(arg0);
			}
			@Override
			public void fileAdded(FileAddedEvent arg0) {
				super.fileAdded(arg0);
			}
			@Override
			public void fileInfoGenerated(FileInfoEvent fileInfoEvent) {
				FileInfoContainer fileInfoContainer = fileInfoEvent.getInfoContainer();
				if (fileInfoContainer instanceof DefaultFileInfoContainer) {
					
				} else if (fileInfoContainer instanceof LogInformation) {
					LogInformation logInfo = (LogInformation) fileInfoContainer;
					int length = Integer.parseInt(logInfo.getTotalRevisions());
					for (int i = 0; i < length; i++) {
						Revision rev = (Revision) logInfo.getRevisionList().get(i);
						System.out.println("---------------------");
						System.out.println("File:" + logInfo.getRepositoryFilename());
						System.out.println("Author:" + rev.getAuthor());
						System.out.println("CommitID:" + rev.getCommitID());
						System.out.println("Number:" + rev.getNumber());
						System.out.println("Message:" + rev.getMessage());
						System.out.println("Lines:" + rev.getLines());
						System.out.println("Date:" + rev.getDateString());
					}
				}
			}
			@Override
			public void fileRemoved(FileRemovedEvent arg0) {
				super.fileRemoved(arg0);
			}
			@Override
			public void fileToRemove(FileToRemoveEvent arg0) {
				super.fileToRemove(arg0);
			}
			@Override
			public void fileUpdated(FileUpdatedEvent arg0) {
				super.fileUpdated(arg0);
			}
			@Override
			public void messageSent(MessageEvent arg0) {
				super.messageSent(arg0);
			}
			@Override
			public void messageSent(BinaryMessageEvent arg0) {
				super.messageSent(arg0);
			}
			@Override
			public void moduleExpanded(ModuleExpansionEvent arg0) {
				super.moduleExpanded(arg0);
			}
		};
		
		// チェックアウト
//		CheckoutCommand checkoutCommand = new CheckoutCommand();
////		checkoutCommand.setModule("plugins");		// モジュール名(jEdit)
//		checkoutCommand.setModule("JHotDraw");			// モジュール名(JHotDraw)
//		try {
//			client.getEventManager().addCVSListener(adapter);
//			client.executeCommand(checkoutCommand, new GlobalOptions());
//		} catch (CommandException | AuthenticationException e) {
//			e.printStackTrace();
//		}
		
		// ログ
		RlogCommand rlogCommand = new RlogCommand();
//		rlogCommand.setModule("plugins");		// モジュール名(jEdit)
		rlogCommand.setModule("JHotDraw");		// モジュール名(JHotDraw)
		try {
			client.getEventManager().addCVSListener(adapter);
			client.executeCommand(rlogCommand, new GlobalOptions());
		} catch (CommandException | AuthenticationException e) {
			e.printStackTrace();
		}
		
		closeConnection(c);
	}

	private static Client openConnection(PServerConnection c) {
		c.setUserName("anonymous");
		c.setEncodedPassword(StandardScrambler.getInstance().scramble(""));
		try {
			c.open();
			return new Client(c, new StandardAdminHandler());
		} catch (CommandAbortedException | AuthenticationException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static void closeConnection(PServerConnection c) {
		try {
			c.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
