package repo

import (
	"fmt"
	"math"
	"net/http"
	"strconv"

	"code.gitea.io/gitea/models/repo"
	user "code.gitea.io/gitea/models/user"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/context"
	"code.gitea.io/gitea/modules/convert"
	"code.gitea.io/gitea/modules/git"
	"code.gitea.io/gitea/modules/setting"
	api "code.gitea.io/gitea/modules/structs"
	"code.gitea.io/gitea/routers/api/v1/utils"
	"code.gitea.io/gitea/services/gitdiff"
	hat_convert "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert"
	hat_git "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/git"
)

func GetAllCommitsSliceByTime(ctx *context.APIContext) {
	if ctx.Repo.Repository.IsEmpty {
		ctx.JSON(http.StatusConflict, api.APIError{
			Message: "Git Repository is empty.",
			URL:     setting.API.SwaggerURL,
		})
		return
	}

	gitRepo, err := git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath())
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
		return
	}

	defer gitRepo.Close()

	listOptions := utils.GetListOptions(ctx)
	if listOptions.Page <= 0 {
		listOptions.Page = 1
	}

	if listOptions.PageSize > setting.Git.CommitsRangeSize {
		listOptions.PageSize = setting.Git.CommitsRangeSize
	}

	sha := ctx.Params("sha")
	var baseCommit *git.Commit
	if len(sha) == 0 {
		head, err := gitRepo.GetHEADBranch()
		if err != nil {
			ctx.Error(http.StatusInternalServerError, "GetHEADBranch", err)
			return
		}

		baseCommit, err = gitRepo.GetBranchCommit(head.Name)
		if err != nil {
			ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err)
			return
		}

	} else {
		baseCommit, err = gitRepo.GetCommit(sha)
		if err != nil {
			ctx.Error(http.StatusInternalServerError, "GetCommit", err)
			return
		}
	}

	commitsCountTotal, err := baseCommit.CommitsCount()
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "CommitsCount", err)
		return
	}

	pageCount := int(math.Ceil(float64(commitsCountTotal)))

	commits, err := baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize)
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "CommitsByRange", err)
		return
	}

	userCache := make(map[string]*user.User)

	apiCommits := make([]*responseCommit, len(commits))
	apiCommitsList := []responseCommit{}
	for i, commitPoniter := range commits {
		apiCommits[i], err = toResponseCommit(ctx.Repo.Repository, gitRepo, commitPoniter, userCache)
		if err != nil {
			ctx.Error(http.StatusInternalServerError, "ToCommit", err)
			return
		}
		apiCommitsList = append(apiCommitsList, *apiCommits[i])
	}

	ctx.RespHeader().Set("X-Page", strconv.Itoa(listOptions.Page))
	ctx.RespHeader().Set("X-PerPage", strconv.Itoa(listOptions.PageSize))
	ctx.RespHeader().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10))
	ctx.RespHeader().Set("X-PageCount", strconv.Itoa(pageCount))
	ctx.RespHeader().Set("X-HasMore", strconv.FormatBool(listOptions.Page < pageCount))

	ctx.SetLinkHeader(int(commitsCountTotal), listOptions.PageSize)
	ctx.RespHeader().Set("X-Total-Count", fmt.Sprintf("%d", commitsCountTotal))
	ctx.RespHeader().Set("Access-Control-Expose-Headers", "X-Total-Count, X-PerPage, X-Total, X-PageCount, X-HasMore, Link")

	ctx.JSON(http.StatusOK, apiCommitsList)
}

type responseCommit struct {
	*api.Commit
	CommitDate string `json:"commit_date"`
}

func toResponseCommit(repo *repo.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user.User) (*responseCommit, error) {
	apiCommit, err := convert.ToCommit(repo, gitRepo, commit, userCache, true)
	if err != nil {
		return nil, err
	}

	return &responseCommit{
		Commit:     apiCommit,
		CommitDate: apiCommit.Created.Format("2006-01-02"),
	}, nil
}

func GetFileAllCommits(ctx *context.APIContext) {
	if ctx.Repo.Repository.IsEmpty {
		ctx.JSON(http.StatusConflict, api.APIError{
			Message: "Git Repository is empty",
			URL:     setting.API.SwaggerURL,
		})
		return
	}
	listOptions := utils.GetListOptions(ctx)
	if listOptions.Page <= 0 {
		listOptions.Page = 1
	}
	if listOptions.PageSize > setting.Git.CommitsRangeSize {
		listOptions.PageSize = setting.Git.CommitsRangeSize
	}

	ref := ctx.FormString("sha")
	if ref == "" {
		ref = ctx.FormString("ref")
	}

	treePath := ctx.Params("*")
	var baseCommit *git.Commit
	var commitsCountTotal int64
	var err error
	if len(ref) == 0 {
		head, err := ctx.Repo.GitRepo.GetHEADBranch()
		if err != nil {
			ctx.Error(http.StatusInternalServerError, "GetHeadBranch", err)
			return
		}
		baseCommit, err = ctx.Repo.GitRepo.GetBranchCommit(head.Name)
		if err != nil {
			ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err)
			return
		}
		commitsCountTotal, err = git.CommitsCountFiles(ctx, ctx.Repo.Repository.RepoPath(), []string{head.Name}, []string{treePath})
		if err != nil {
			ctx.Error(http.StatusInternalServerError, "CommitsCountFiles", err)
			return
		}

	} else {
		baseCommit, err = ctx.Repo.GitRepo.GetCommit(ref)
		if err != nil {
			ctx.Error(http.StatusInternalServerError, "GetCommit", err)
			return
		}
		commitsCountTotal, err = git.CommitsCountFiles(ctx, ctx.Repo.GitRepo.Path, []string{ref}, []string{treePath})
		if err != nil {
			ctx.Error(http.StatusInternalServerError, "CommitsCountFiles", err)
			return
		}

	}
	pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(listOptions.PageSize)))
	commits, err := hat_git.CommitsByFileAndRange(ctx.Repo.GitRepo, baseCommit.ID.String(), treePath, listOptions.Page, listOptions.PageSize)
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "CommitsByRange", err)
		return
	}

	userCache := make(map[string]*user_model.User)
	apiCommits := make([]*api.Commit, len(commits))
	for i, commit := range commits {
		apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, ctx.Repo.GitRepo, commit, userCache, true)
		if err != nil {
			ctx.Error(http.StatusInternalServerError, "ToCommit", err)
			return
		}
	}

	ctx.Resp.Header().Set("X-Page", strconv.Itoa(listOptions.Page))
	ctx.Resp.Header().Set("X-PerPage", strconv.Itoa(listOptions.PageSize))
	ctx.Resp.Header().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10))
	ctx.Resp.Header().Set("X-PageCount", strconv.Itoa(pageCount))
	ctx.Resp.Header().Set("X-HasMore", strconv.FormatBool(listOptions.Page < pageCount))

	ctx.SetLinkHeader(int(commitsCountTotal), listOptions.PageSize)
	ctx.Resp.Header().Set("X-Total-Count", fmt.Sprintf("%d", commitsCountTotal))

	ctx.JSON(http.StatusOK, apiCommits)
}

func GetSingleCommit(ctx *context.APIContext) {
	sha := ctx.Params(":sha")
	if !git.IsValidRefPattern(sha) {
		ctx.Error(http.StatusUnprocessableEntity, "no valid ref or sha", fmt.Sprintf("no valid ref or sha: %s", sha))
		return
	}

	getCommit(ctx, sha)
}

func getCommit(ctx *context.APIContext, indentifier string) {
	commit, err := ctx.Repo.GitRepo.GetCommit(indentifier)
	if err != nil {
		if git.IsErrNotExist(err) {
			ctx.NotFound(indentifier)
			return
		}
		ctx.Error(http.StatusInternalServerError, "gitRepo.GetCommit", err)
		return
	}

	json, err := hat_convert.ToCommit(ctx.Repo.Repository, ctx.Repo.GitRepo, commit, nil, true)
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "toCommit", err)
		return
	}
	ctx.JSON(http.StatusOK, json)
}

func GetCommitDiff(ctx *context.APIContext) {
	commitID := ctx.Params(":sha")
	gitRepo := ctx.Repo.GitRepo

	commit, err := gitRepo.GetCommit(commitID)
	if err != nil {
		if git.IsErrNotExist(err) {
			ctx.NotFound(commitID)
			return
		}
		ctx.Error(http.StatusInternalServerError, "gitRepo.GetCommit", err)
		return
	}

	if len(commitID) != 40 {
		commitID = commit.ID.String()
	}

	diff, err := gitdiff.GetDiff(gitRepo, &gitdiff.DiffOptions{
		AfterCommitID:     commitID,
		MaxLines:          setting.Git.MaxGitDiffLines,
		MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
		MaxFiles:          setting.Git.MaxGitDiffFiles,
	})
	if err != nil {
		ctx.NotFound("GetDIff", err)
		return
	}
	ctx.JSON(http.StatusOK, &diff)
}
