package com.atlassian.stash.internal.web.admin;

import com.atlassian.stash.internal.web.soy.StashSoyResponseBuilder;
import com.atlassian.bitbucket.user.UserAdminService;
import com.atlassian.bitbucket.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/admin/groups/view")
public class GroupEditController {

    private static final String GROUP_VIEW = "bitbucket.internal.admin.groups.edit";

    private final UserService userService;
    private final UserAdminService userAdminService;

    @Autowired
    public GroupEditController(UserService userService, UserAdminService userAdminService) {
        this.userService = userService;
        this.userAdminService = userAdminService;
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView showGroup(@RequestParam String name) {
        return new StashSoyResponseBuilder(GROUP_VIEW)
                .put("group", name)
                .put("exists", userService.existsGroup(name))
                .put("canUpdateGroups", userAdminService.canUpdateGroups())
                .put("canDeleteGroups", userAdminService.canDeleteGroups())
                .build();
    }
}
