package cn.reghao.devops.web.mgr.builds.controller.page;

import cn.reghao.devops.web.mgr.builds.db.repository.RepoAuthConfigRepository;
import cn.reghao.devops.web.mgr.builds.model.po.RepoAuthConfig;
import cn.reghao.devops.web.mgr.app.model.vo.KeyValue;
import cn.reghao.devops.web.mgr.build.model.constant.RepoAuthType;
import cn.reghao.devops.web.mgr.build.model.constant.RepoType;
import cn.reghao.devops.web.util.PageSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author reghao
 * @date 2021-09-17 10:31:15
 */
@Slf4j
@Api(tags = "仓库认证配置页面")
@Controller
@RequestMapping("/build")
public class RepoAuthPageController {
    private final RepoAuthConfigRepository repoAuthConfigRepository;

    public RepoAuthPageController(RepoAuthConfigRepository repoAuthConfigRepository) {
        this.repoAuthConfigRepository = repoAuthConfigRepository;
    }

    @ApiOperation(value = "仓库认证列表页面")
    @GetMapping("/repoauth")
    public String repoAuthPage(Model model) {
        PageRequest pageRequest = PageSort.pageRequest();
        Page<RepoAuthConfig> page = repoAuthConfigRepository.findAll(pageRequest);

        model.addAttribute("page", page);
        model.addAttribute("list", page.getContent());
        return "/devops/build/repoauth/index";
    }

    @ApiOperation(value = "仓库认证添加页面")
    @GetMapping("/repoauth/add")
    public String addRepoAuthPage(Model model) {
        setRepoAuthModel(model);
        return "/devops/build/repoauth/add";
    }

    @ApiOperation(value = "仓库认证编辑页面")
    @GetMapping("/repoauth/edit/{id}")
    public String editRepoAuthPage(@PathVariable("id") RepoAuthConfig repoAuth, Model model) {
        repoAuth.setNull();
        model.addAttribute("repoAuth", repoAuth);
        return "/devops/build/repoauth/edit";
    }

    private void setRepoAuthModel(Model model) {
        List<KeyValue> repos = new ArrayList<>();
        for (RepoType repoType : RepoType.values()) {
            repos.add(new KeyValue(repoType.name(), repoType.name()));
        }

        List<KeyValue> auths = new ArrayList<>();
        for (RepoAuthType authType : RepoAuthType.values()) {
            auths.add(new KeyValue(authType.name(), authType.name()));
        }

        model.addAttribute("repos", repos);
        model.addAttribute("auths", auths);
    }

    @GetMapping("/repoauth/detail/{id}")
    public String repoAuthDetailPage(@PathVariable("id") RepoAuthConfig repoAuth, Model model) {
        repoAuth.setPassword("******");
        model.addAttribute("repoAuth", repoAuth);
        return "/devops/build/repoauth/detail";
    }
}
