package v1

import (
	"net/http"

	"gogs.io/gogs/internal/context"
	"gogs.io/gogs/internal/database"
	"gogs.io/gogs/internal/route/api/v1/types"
)

func listIssueLabels(c *context.APIContext) {
	issue, err := database.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
	if err != nil {
		c.NotFoundOrError(err, "get issue by index")
		return
	}

	apiLabels := make([]*types.IssueLabel, len(issue.Labels))
	for i := range issue.Labels {
		apiLabels[i] = toIssueLabel(issue.Labels[i])
	}
	c.JSONSuccess(&apiLabels)
}

type issueLabelsRequest struct {
	Labels []int64 `json:"labels"`
}

func addIssueLabels(c *context.APIContext, form issueLabelsRequest) {
	issue, err := database.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
	if err != nil {
		c.NotFoundOrError(err, "get issue by index")
		return
	}

	labels, err := database.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
	if err != nil {
		c.Error(err, "get labels in repository by IDs")
		return
	}

	if err = issue.AddLabels(c.User, labels); err != nil {
		c.Error(err, "add labels")
		return
	}

	labels, err = database.GetLabelsByIssueID(issue.ID)
	if err != nil {
		c.Error(err, "get labels by issue ID")
		return
	}

	apiLabels := make([]*types.IssueLabel, len(labels))
	for i := range labels {
		apiLabels[i] = toIssueLabel(issue.Labels[i])
	}
	c.JSONSuccess(&apiLabels)
}

func deleteIssueLabel(c *context.APIContext) {
	issue, err := database.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
	if err != nil {
		c.NotFoundOrError(err, "get issue by index")
		return
	}

	label, err := database.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
	if err != nil {
		if database.IsErrLabelNotExist(err) {
			c.ErrorStatus(http.StatusUnprocessableEntity, err)
		} else {
			c.Error(err, "get label of repository by ID")
		}
		return
	}

	if err := database.DeleteIssueLabel(issue, label); err != nil {
		c.Error(err, "delete issue label")
		return
	}

	c.NoContent()
}

func replaceIssueLabels(c *context.APIContext, form issueLabelsRequest) {
	issue, err := database.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
	if err != nil {
		c.NotFoundOrError(err, "get issue by index")
		return
	}

	labels, err := database.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
	if err != nil {
		c.Error(err, "get labels in repository by IDs")
		return
	}

	if err := issue.ReplaceLabels(labels); err != nil {
		c.Error(err, "replace labels")
		return
	}

	labels, err = database.GetLabelsByIssueID(issue.ID)
	if err != nil {
		c.Error(err, "get labels by issue ID")
		return
	}

	apiLabels := make([]*types.IssueLabel, len(labels))
	for i := range labels {
		apiLabels[i] = toIssueLabel(issue.Labels[i])
	}
	c.JSONSuccess(&apiLabels)
}

func clearIssueLabels(c *context.APIContext) {
	issue, err := database.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
	if err != nil {
		c.NotFoundOrError(err, "get issue by index")
		return
	}

	if err := issue.ClearLabels(c.User); err != nil {
		c.Error(err, "clear labels")
		return
	}

	c.NoContent()
}
