// Copyright 2015 - Present, The Gogs Authors. All rights reserved.
// Copyright 2018 - Present, Gitote. All rights reserved.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

package org

import (
	"gitote/gitote/models"
	"gitote/gitote/models/errors"
	"gitote/gitote/pkg/context"
	"gitote/gitote/pkg/form"
	"path"

	raven "github.com/getsentry/raven-go"
	"gitlab.com/gitote/com"
	log "gopkg.in/clog.v1"
)

const (
	// TeamsTPL page template
	TeamsTPL = "org/team/teams"

	// TeamNewTPL page template
	TeamNewTPL = "org/team/new"

	// TeamMembersTPL page template
	TeamMembersTPL = "org/team/members"

	// TeamRepositoriesTPL page template
	TeamRepositoriesTPL = "org/team/repositories"
)

// Teams shows team page
func Teams(c *context.Context) {
	org := c.Org.Organization
	c.Data["Title"] = org.FullName
	c.Data["PageIsOrgTeams"] = true

	for _, t := range org.Teams {
		if err := t.GetMembers(); err != nil {
			c.Handle(500, "GetMembers", err)
			return
		}
	}
	c.Data["Teams"] = org.Teams

	c.HTML(200, TeamsTPL)
}

// TeamsAction handle the user action
// Like : join, leave, add or remove a team
func TeamsAction(c *context.Context) {
	uid := com.StrTo(c.Query("uid")).MustInt64()
	if uid == 0 {
		c.Redirect(c.Org.OrgLink + "/teams")
		return
	}

	page := c.Query("page")
	var err error
	switch c.Params(":action") {
	// Join a team
	case "join":
		if !c.Org.IsOwner {
			c.Error(404)
			return
		}
		err = c.Org.Team.AddMember(c.User.ID)
	// Leave a team
	case "leave":
		err = c.Org.Team.RemoveMember(c.User.ID)
	// Remove a team
	case "remove":
		if !c.Org.IsOwner {
			c.Error(404)
			return
		}
		err = c.Org.Team.RemoveMember(uid)
		page = "team"
	// Add a team
	case "add":
		if !c.Org.IsOwner {
			c.Error(404)
			return
		}
		uname := c.Query("uname")
		var u *models.User
		u, err = models.GetUserByName(uname)
		if err != nil {
			if errors.IsUserNotExist(err) {
				c.Flash.Error(c.Tr("form.user_not_exist"))
				c.Redirect(c.Org.OrgLink + "/teams/" + c.Org.Team.LowerName)
			} else {
				c.Handle(500, " GetUserByName", err)
			}
			return
		}

		err = c.Org.Team.AddMember(u.ID)
		page = "team"
	}

	if err != nil {
		if models.IsErrLastOrgOwner(err) {
			c.Flash.Error(c.Tr("form.last_org_owner"))
		} else {
			raven.CaptureErrorAndWait(err, nil)
			log.Error(3, "Action(%s): %v", c.Params(":action"), err)
			c.JSON(200, map[string]interface{}{
				"ok":  false,
				"err": err.Error(),
			})
			return
		}
	}

	switch page {
	case "team":
		c.Redirect(c.Org.OrgLink + "/teams/" + c.Org.Team.LowerName)
	default:
		c.Redirect(c.Org.OrgLink + "/teams")
	}
}

// TeamsRepoAction handle user actions
// Like: add, remove a repository
func TeamsRepoAction(c *context.Context) {
	if !c.Org.IsOwner {
		c.Error(404)
		return
	}

	var err error
	switch c.Params(":action") {
	case "add":
		repoName := path.Base(c.Query("repo_name"))
		var repo *models.Repository
		repo, err = models.GetRepositoryByName(c.Org.Organization.ID, repoName)
		if err != nil {
			if errors.IsRepoNotExist(err) {
				c.Flash.Error(c.Tr("org.teams.add_nonexistent_repo"))
				c.Redirect(c.Org.OrgLink + "/teams/" + c.Org.Team.LowerName + "/repositories")
				return
			}
			c.Handle(500, "GetRepositoryByName", err)
			return
		}
		err = c.Org.Team.AddRepository(repo)
	case "remove":
		err = c.Org.Team.RemoveRepository(com.StrTo(c.Query("repoid")).MustInt64())
	}

	if err != nil {
		raven.CaptureErrorAndWait(err, nil)
		log.Error(3, "Action(%s): '%s' %v", c.Params(":action"), c.Org.Team.Name, err)
		c.Handle(500, "TeamsRepoAction", err)
		return
	}
	c.Redirect(c.Org.OrgLink + "/teams/" + c.Org.Team.LowerName + "/repositories")
}

// NewTeam shows the team creation page
func NewTeam(c *context.Context) {
	c.Data["Title"] = c.Org.Organization.FullName
	c.Data["PageIsOrgTeams"] = true
	c.Data["PageIsOrgTeamsNew"] = true
	c.Data["Team"] = &models.Team{}
	c.HTML(200, TeamNewTPL)
}

// NewTeamPost creates an organization with the body fields
func NewTeamPost(c *context.Context, f form.CreateTeam) {
	c.Data["Title"] = c.Org.Organization.FullName
	c.Data["PageIsOrgTeams"] = true
	c.Data["PageIsOrgTeamsNew"] = true

	t := &models.Team{
		OrgID:       c.Org.Organization.ID,
		Name:        f.TeamName,
		Description: f.Description,
		Authorize:   models.ParseAccessMode(f.Permission),
	}
	c.Data["Team"] = t

	if c.HasError() {
		c.HTML(200, TeamNewTPL)
		return
	}

	// Create a new team
	if err := models.NewTeam(t); err != nil {
		c.Data["Err_TeamName"] = true
		switch {
		case models.IsErrTeamAlreadyExist(err):
			c.RenderWithErr(c.Tr("form.team_name_been_taken"), TeamNewTPL, &f)
		case models.IsErrNameReserved(err):
			c.RenderWithErr(c.Tr("org.form.team_name_reserved", err.(models.ErrNameReserved).Name), TeamNewTPL, &f)
		default:
			c.Handle(500, "NewTeam", err)
		}
		return
	}
	log.Trace("Team created: %s/%s", c.Org.Organization.Name, t.Name)
	c.Redirect(c.Org.OrgLink + "/teams/" + t.LowerName)
}

// TeamMembers shows team members page
func TeamMembers(c *context.Context) {
	c.Data["Title"] = c.Org.Team.Name
	c.Data["PageIsOrgTeams"] = true
	if err := c.Org.Team.GetMembers(); err != nil {
		c.Handle(500, "GetMembers", err)
		return
	}
	c.HTML(200, TeamMembersTPL)
}

// TeamRepositories shows team repositoies page
func TeamRepositories(c *context.Context) {
	c.Data["Title"] = c.Org.Team.Name
	c.Data["PageIsOrgTeams"] = true
	if err := c.Org.Team.GetRepositories(); err != nil {
		c.Handle(500, "GetRepositories", err)
		return
	}
	c.HTML(200, TeamRepositoriesTPL)
}

// EditTeam shows edit team page
func EditTeam(c *context.Context) {
	c.Data["Title"] = c.Org.Organization.FullName
	c.Data["PageIsOrgTeams"] = true
	c.Data["team_name"] = c.Org.Team.Name
	c.Data["desc"] = c.Org.Team.Description
	c.HTML(200, TeamNewTPL)
}

// EditTeamPost edit team with the body fields
func EditTeamPost(c *context.Context, f form.CreateTeam) {
	t := c.Org.Team
	c.Data["Title"] = c.Org.Organization.FullName
	c.Data["PageIsOrgTeams"] = true
	c.Data["Team"] = t

	if c.HasError() {
		c.HTML(200, TeamNewTPL)
		return
	}

	isAuthChanged := false
	if !t.IsOwnerTeam() {
		// Validate permission level.
		var auth models.AccessMode
		switch f.Permission {
		case "read":
			auth = models.AccessModeRead
		case "write":
			auth = models.AccessModeWrite
		case "admin":
			auth = models.AccessModeAdmin
		default:
			c.Error(401)
			return
		}

		t.Name = f.TeamName
		if t.Authorize != auth {
			isAuthChanged = true
			t.Authorize = auth
		}
	}
	t.Description = f.Description
	// Edit team
	if err := models.UpdateTeam(t, isAuthChanged); err != nil {
		c.Data["Err_TeamName"] = true
		switch {
		case models.IsErrTeamAlreadyExist(err):
			c.RenderWithErr(c.Tr("form.team_name_been_taken"), TeamNewTPL, &f)
		default:
			c.Handle(500, "UpdateTeam", err)
		}
		return
	}
	c.Redirect(c.Org.OrgLink + "/teams/" + t.LowerName)
}

// DeleteTeam deletes a team
func DeleteTeam(c *context.Context) {
	// Delete team
	if err := models.DeleteTeam(c.Org.Team); err != nil {
		c.Flash.Error("DeleteTeam: " + err.Error())
	} else {
		c.Flash.Success(c.Tr("org.teams.delete_team_success"))
	}

	c.JSON(200, map[string]interface{}{
		"redirect": c.Org.OrgLink + "/teams",
	})
}
