package com.lanyuanxiaoyao.service.hudi.controller;

import com.lanyuanxiaoyao.service.configuration.entity.hudi.HPath;
import com.lanyuanxiaoyao.service.hudi.service.HdfsService;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.collections.api.list.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * hudi目标库的hdfs操作
 *
 * @author lanyuanxiaoyao
 * @date 2024-01-24
 */
@RestController
@RequestMapping("hdfs")
public class HdfsController {
    private static final Logger logger = LoggerFactory.getLogger(HdfsController.class);

    private final HdfsService hdfsService;

    public HdfsController(HdfsService hdfsService) {
        this.hdfsService = hdfsService;
    }

    @GetMapping("exists_hudi_table_by_flink_job_id_alias")
    public Boolean existsHudiTable(
            @RequestParam("flink_job_id") Long flinkJobId,
            @RequestParam("alias") String alias
    ) {
        return hdfsService.existsHudiTable(flinkJobId, alias);
    }

    @GetMapping("exists_hudi_table_by_hdfs")
    public Boolean existsHudiTable(@RequestParam("hdfs") String hdfs) {
        return hdfsService.existsHudiTable(hdfs);
    }

    @GetMapping("exists_path")
    public Boolean existsPath(@RequestParam("hdfs") String hdfs) {
        return hdfsService.existsPath(hdfs);
    }

    @GetMapping("get")
    public HPath get(@RequestParam("root") String root) throws IOException {
        return hdfsService.get(root);
    }

    @GetMapping("list")
    public ImmutableList<HPath> list(@RequestParam("root") String root) throws IOException {
        return hdfsService.list(root);
    }

    @GetMapping("read")
    public String read(@RequestParam("root") String root) throws IOException {
        return hdfsService.read(root);
    }

    @PostMapping("write")
    public void write(
            @RequestParam("root") String root,
            @RequestParam(value = "overwrite", defaultValue = "false") Boolean overwrite,
            @RequestBody String text
    ) throws IOException {
        hdfsService.write(root, text, overwrite);
    }

    @GetMapping("download")
    public void download(@RequestParam("root") String root, HttpServletResponse response) throws IOException {
        hdfsService.download(root, response.getOutputStream());
    }

    @GetMapping("size")
    public Long size(@RequestParam("root") String root) throws IOException {
        return hdfsService.size(root);
    }

    @GetMapping("count")
    public Long count(@RequestParam("root") String root) throws IOException {
        return hdfsService.count(root);
    }

    @GetMapping("file_count")
    public Long fileCount(@RequestParam("root") String root) throws IOException {
        return hdfsService.countFiles(root);
    }

    @GetMapping("directory_count")
    public Long DirectoryCount(@RequestParam("root") String root) throws IOException {
        return hdfsService.countDirectories(root);
    }
}
