package com.codvision.monitor.controller.manager;

import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.monitor.entity.GitVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * @author KX
 * @date 2023/6/28 18:09
 */
@Slf4j
@RestController
@Api(tags = "平台管理端-版本信息管理模块")
public class ManagerVersionController {

    @ApiOperation(value = "获取前后端版本信息", notes = "获取前后端版本信息接口")
    @GetMapping(value = "/version")
    public ResponseEntity<GitVersion> versionInformation() {
        ResponseEntity<GitVersion> responseEntity = new ResponseEntity<>();
        JSONObject jsonObject = JSONUtil.parseObj(readGitProperties());
        GitVersion gitVersion = GitVersion.builder().tags(jsonObject.getStr("git.tags")).closestTagName(jsonObject.getStr("git.closest.tag.name"))
                .branch(jsonObject.getStr("git.branch")).buildHost(jsonObject.getStr("git.build.host"))
                .buildUserName(jsonObject.getStr("git.build.user.name")).buildUserEmail(jsonObject.getStr("git.build.user.email"))
                .buildTime(jsonObject.getStr("git.build.time")).buildVersion(jsonObject.getStr("git.build.version"))
                .commitIdAbbrev(jsonObject.getStr("git.commit.id.abbrev")).commitId(jsonObject.getStr("git.commit.id"))
                .commitUserName(jsonObject.getStr("git.commit.user.name"))
                .commitUserEmail(jsonObject.getStr("git.commit.user.email")).commitTime(jsonObject.getStr("git.commit.time"))
                .commitMessageFull(jsonObject.getStr("git.commit.message.full")).totalCommitCount(jsonObject.getStr("git.total.commit.count"))
                .remoteOriginUrl(jsonObject.getStr("git.remote.origin.url")).build();
        log.info(jsonObject.toString());
        responseEntity.setData(gitVersion);
        return responseEntity;
    }

    private String readGitProperties() {
        ClassLoader classLoader = getClass().getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("git.json");
        try {
            return readFromInputStream(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            return "Version information could not be retrieved.";
        }
    }

    private String readFromInputStream(InputStream inputStream)
            throws IOException {
        StringBuilder result = new StringBuilder();
        try (
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8.name()))
        ) {
            String line;
            while ((line = br.readLine()) != null) {
                result.append(line).append(System.lineSeparator());
            }
        }
        return result.toString();
    }


    @PostMapping("/appVersion")
    @ApiOperation(value = "获取移动端版本信息", notes = "获取移动端版本信息")
    public ResponseEntity<String> appVersion(@ApiParam(value = "移动端文件路径", required = true) @RequestParam(value = "path") String path) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        try {
            FileReader fileReader = new FileReader(path);
            responseEntity.setData(fileReader.readString());
        } catch (IORuntimeException e) {
            responseEntity.setMessage("文件路径未找到移动端版本信息！如有需要请联系管理员配置");
        }
        return responseEntity;
    }
}

