package cn.edu.seu.ise.dca.analysis.util.test;

/**
 * Licensed to OpenOnes under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * OpenOnes 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.
 */

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;

import cn.edu.seu.ise.dca.global.Global;
import cn.edu.seu.ise.dca.util.ListUtil;


public class LoCComparisionTest {
    public static void main(String[] args) throws SVNException {
        DAVRepositoryFactory.setup();

        String url = "http://web-erp.svn.sourceforge.net/svnroot/web-erp";
        long oldRev = 188;
        long newRev = 189; 
        List<String> prefixes = new ArrayList<String>();
        prefixes.add("trunk/");

        SVNURL svnurl = SVNURL.parseURIEncoded(url);
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        SVNClientManager manager = SVNClientManager.newInstance();
        manager.getDiffClient().doDiff(svnurl, SVNRevision.UNDEFINED, SVNRevision.create(oldRev), SVNRevision.create(newRev), SVNDepth.UNKNOWN, true, bao);
        System.out.println(getChangedLines(bao.toString(), prefixes, Global.POSTFIX));
        System.out.println(bao.toString());
    }
	
    private static long getChangedLines(String content, List<String> prefixes, String[] postfixes) {
		long changedLines = 0;
		String path = null;
		String postfix = null;
		boolean flag = false;
		Scanner scanner = new Scanner(content);
		while (scanner.hasNextLine()) {
		  String line = scanner.nextLine().trim();
		  if(line.startsWith("Index")) {
			  String[] lineSeps = line.split(":");
			  if(lineSeps.length > 1) {
				  path = line.split(":")[1].trim();
				  if(path.contains(".")) {
					  int index = path.lastIndexOf(".");
					  postfix = path.substring(index+1, path.length());
				  }
				  
				  if (ListUtil.isStartWith(path, prefixes) && ListUtil.isInArray(postfix, postfixes)) {
					  flag = true;
					 
				  } else {
					  flag = false;
				  }
			  }  
		  }
		  
		  if(((line.startsWith("+") && !line.startsWith("+++")) || (line.startsWith("-") && !line.startsWith("---"))) && flag == true) {
			  changedLines ++;
		  }
		}
		return changedLines;
	}
}