package cn.reghao.autodop.dmaster.app.controller.page;

import cn.reghao.autodop.dmaster.app.db.query.config.RepoAuthConfigQuery;
import cn.reghao.autodop.dmaster.app.model.constant.build.RepoAuthType;
import cn.reghao.autodop.dmaster.app.model.constant.build.RepoType;
import cn.reghao.autodop.dmaster.app.model.po.config.build.RepoAuthConfig;
import cn.reghao.autodop.dmaster.app.model.vo.KeyValue;
import cn.reghao.autodop.dmaster.util.db.PageList;
import cn.reghao.autodop.dmaster.util.db.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("/app/config/build")
public class RepoAuthPageController {
    private RepoAuthConfigQuery repoAuthConfigQuery;

    public RepoAuthPageController(RepoAuthConfigQuery repoAuthConfigQuery) {
        this.repoAuthConfigQuery = repoAuthConfigQuery;
    }

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

        model.addAttribute("page", page);
        model.addAttribute("list", pageList.getList());
        return "/app/config/repoauth/index";
    }

    @GetMapping("/repoauth/add")
    public String addRepoAuthPage(Model model) {
        setRepoAuthModel(model);
        return "/app/config/repoauth/add";
    }

    @GetMapping("/repoauth/edit/{id}")
    public String editRepoAuthPage(@PathVariable("id") RepoAuthConfig repoAuth, Model model) {
        setRepoAuthModel(model);
        model.addAttribute("repoAuth", repoAuth);
        return "/app/config/repoauth/add";
    }

    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) {
        model.addAttribute("repoAuth", repoAuth);
        return "/app/config/repoauth/detail";
    }
}
