package com.lz.modules.performance.controller;

import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.LogCommand;
import org.eclipse.jgit.api.PullResult;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
import org.seimicrawler.xpath.JXDocument;
import org.seimicrawler.xpath.JXNode;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: djc
 * @Desc:
 * @Date: 2020/12/17 17:06
 */
public class GitController {


    /*public static void main(String[] args) throws Exception{


        //设置远程服务器上的用户名和密码
        UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
                UsernamePasswordCredentialsProvider("dujianchao31","Du1262707462");
        //克隆代码库命令
        CloneCommand cloneCommand = Git.cloneRepository();

        Git git= cloneCommand.setURI("http://gitlab.ldxinyong.com/enterpriseManagement/lz_management/") //设置远程URI
                .setBranch("version_performance_2.0") //设置clone下来的分支
                .setDirectory(new File("D:\\test\\")) //设置下载存放路径
                .setCredentialsProvider(usernamePasswordCredentialsProvider) //设置权限验证
                .call();
    }*/

    public static void main(String[] args) throws Exception{
        UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
                UsernamePasswordCredentialsProvider("dujianchao31","Du1262707462");
      /*  //git仓库地址
        Git git = new Git(new FileRepository("D:\\test\\"+"/.git"));
        PullResult call = git.pull().setRemoteBranchName("version_performance_2.0").
                setCredentialsProvider(usernamePasswordCredentialsProvider).call();*/

        //git仓库地址
        Git git = new Git(new FileRepository("D:\\test\\"+"/.git"));


    }

    public static AbstractTreeIterator prepareTreeParser(RevCommit commit){
        System.out.println(commit.getId());
        try (RevWalk walk = new RevWalk(new FileRepository("D:\\test\\"+"/.git"))) {
            System.out.println(commit.getTree().getId());
            RevTree tree = walk.parseTree(commit.getTree().getId());

            CanonicalTreeParser oldTreeParser = new CanonicalTreeParser();
            try (ObjectReader oldReader = new FileRepository("D:\\test\\"+"/.git").newObjectReader()) {
                oldTreeParser.reset(oldReader, tree.getId());
            }

            walk.dispose();

            return oldTreeParser;
        }catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }


    //日志信息

    public static void commit(Git git) throws Exception{
        List<String> versions = new ArrayList<>();
        Iterable<RevCommit> commits = git.log().all().call();
        int count = 0;
        for (RevCommit commit : commits) {
            System.out.println("LogCommit: " + commit);
            System.out.println("提交信息：" + commit.getFullMessage());
            String version = commit.getName(); //版本号,用来查询详细信息
            versions.add(version);
            System.out.println("版本号：" + commit.getName());
            PersonIdent authorIdent = commit.getAuthorIdent();
            System.out.println("作者：" + authorIdent.getName());
            System.out.println("邮箱：" + authorIdent.getEmailAddress());
            System.out.println("时间：" + authorIdent.getWhen());
            count++;
        }
        System.out.println("=========");
        System.out.println("总计" + count);
    }


    //文件变动
    public void diff(Git git) throws Exception{
        List<RevCommit> commitsList = new ArrayList<>();
        Iterable<RevCommit> commits = git.log().all().call();
        for(RevCommit commit:commits){
            commitsList.add(commit);
        }

        AbstractTreeIterator newTree = prepareTreeParser(commitsList.get(0));
        AbstractTreeIterator oldTree = prepareTreeParser(commitsList.get(1));

        List<DiffEntry> diffEntries = git.diff().setNewTree(newTree).setOldTree(oldTree).call();
        DiffEntry.ChangeType changeType;
        int add =0;
        int copy =0;
        int delete =0;
        int modify =0;
        int rename =0;
        for(DiffEntry entry : diffEntries){
            changeType = entry.getChangeType();
            System.out.println("=====" + changeType);
            switch (changeType) {
                case ADD:
                    System.out.println(++add);
                    break;
                case COPY:
                    System.out.println(++copy);
                    break;
                case DELETE:
                    System.out.println(++delete);
                    break;
                case MODIFY:
                    System.out.println(++modify);
                    break;
                case RENAME:
                    System.out.println(rename++);
                    break;
            }
        }

        System.out.println("=========");
    }

    public void webTest(){
        //域名
        String domain = "";
        //项目名
        String projectName = "";
        //分支名
        String branchName = "";
        //session
        String session = "";
        //分页 ?limit=40&offset=120

        try {
            URLConnection urlConnection = new URL("http://gitlab.ldxinyong.com/enterpriseManagement/lz_management/commits/version_performance_2.0").openConnection();
            //URLConnection urlConnection = new URL("http://gitlab.ldxinyong.com/enterpriseManagement/lz_management/graphs/master").openConnection();
            HttpURLConnection connection = (HttpURLConnection) urlConnection;
            connection.setRequestMethod("GET");
            connection.addRequestProperty("Host", "gitlab.ldxinyong.com");

            connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0");

            connection.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            connection.addRequestProperty("Accept-Encoding", "gzip, deflate");
            connection.addRequestProperty("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
            connection.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.160 Safari/537.22");

            //connection.addRequestProperty("Referer", "http://gitlab.ldxinyong.com/enterpriseManagement/lz_management/commits/master");

            //connection.addRequestProperty("Connection", "keep-alive");

            //connection.addRequestProperty("Upgrade-Insecure-Requests", "1");

            //connection.addRequestProperty("If-None-Match", "W/\"352c90856827b53f3735109727c9f3a1\"");

            connection.addRequestProperty("Cookie", "_gitlab_session=4e14c53a8cb378a65fda996b0bc15021; sidebar_collapsed=false");
            connection.connect();// 连接会话
            // 获取输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {// 循环读取流
                sb.append(line);
            }
            br.close();// 关闭流
            connection.disconnect();// 断开连接
            System.out.println(sb.toString());
            JXDocument document = JXDocument.create(sb.toString());
            JXNode jxNode = document.selNOne("//*[@id='commits-list']");
            List<JXNode> sel = jxNode.sel("//*[@class='commits-row']");
            for(JXNode jx:sel){
                List<JXNode> sel2 = jx.sel("//*[@class='commit flex-row js-toggle-container']");
                for(JXNode j:sel2){
                    String avatar = j.sel("//img[@class='avatar s36 hidden-xs has-tooltip']/@src").get(0).toString();
                    System.out.println("头像 " + avatar);

                    String commit = j.sel("//a[@class='commit-row-message item-title']/text()").get(0).toString();
                    System.out.println("摘要 " + commit);

                    String name = j.sel("//a[@class='commit-author-link has-tooltip']/text()").get(0).toString();
                    System.out.println("姓名 " + name);

                    String time = j.sel("//time[@class='js-timeago']/@datetime").get(0).toString();
                    System.out.println("时间 " + time);


                    String version = j.sel("//div[@class='label label-monospace']/text()").get(0).toString();
                    System.out.println("版本 " + version);

                }



            }
            System.out.println();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
