package cn.reghao.tnb.sb.multi.mybatis.service;

import cn.reghao.tnb.sb.multi.mybatis.mapper.one.DataBlockMapper;
import cn.reghao.tnb.sb.multi.mybatis.mapper.one.FileMetaMapper;
import cn.reghao.tnb.sb.multi.mybatis.mapper.two.PostItemMapper;
import cn.reghao.tnb.sb.multi.mybatis.mapper.two.VideoPostMapper;
import cn.reghao.tnb.sb.multi.mybatis.model.one.DataBlock;
import cn.reghao.tnb.sb.multi.mybatis.model.one.FileMeta;
import cn.reghao.tnb.sb.multi.mybatis.model.two.VideoPost;
import cn.reghao.jutil.jdk.converter.ByteConverter;
import cn.reghao.jutil.jdk.converter.ByteType;
import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.PostConstruct;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2024-12-19 22:34:58
 */
@Slf4j
@Service
public class MyBatisService {
    private final FileMetaMapper fileMetaMapper;
    private final DataBlockMapper dataBlockMapper;
    private final VideoPostMapper videoPostMapper;
    private final PostItemMapper postItemMapper;
    private CamService camService;

    public MyBatisService(FileMetaMapper fileMetaMapper, DataBlockMapper dataBlockMapper,
                          VideoPostMapper videoPostMapper, PostItemMapper postItemMapper,
                          CamService camService) {
        this.fileMetaMapper = fileMetaMapper;
        this.dataBlockMapper = dataBlockMapper;
        this.videoPostMapper = videoPostMapper;
        this.postItemMapper = postItemMapper;
        this.camService = camService;
    }

    @PostConstruct
    public void process() {
        //camService.startBak();
        //camService.moveCamFile();

        log.info("MyBatisService process done...");
    }

    private void countVideoFileSize1() {
        List<FileMeta> fileMetas = fileMetaMapper.findByPrefix();

        long total = fileMetas.stream().mapToLong(FileMeta::getSize).sum();
        ByteConverter byteConverter = new ByteConverter();
        String totalStr = byteConverter.convert(ByteType.Bytes, total);
        System.out.println(totalStr);
    }

    private void countVideoFileSize() {
        List<VideoPost> list = videoPostMapper.findAll();
        List<FileMeta> fileMetas = list.stream()
                .map(videoPost -> {
                    String videoFileId = videoPost.getVideoFileId();
                    return fileMetaMapper.findByObjectId(videoFileId);
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        long total = fileMetas.stream().mapToLong(FileMeta::getSize).sum();
        ByteConverter byteConverter = new ByteConverter();
        String totalStr = byteConverter.convert(ByteType.Bytes, total);
        System.out.printf("total size %s\n", totalStr);
    }

    private void deleteVideoFile() {
        videoPostMapper.updateSetDelete();
        markObjectDelete();
    }

    private void markObjectDelete() {
        List<VideoPost> list1 = videoPostMapper.findAll();
        list1.forEach(videoPost -> {
            String videoFileId = videoPost.getVideoFileId();
            FileMeta fileMeta = fileMetaMapper.findByObjectId(videoFileId);
            if (fileMeta != null) {
                String contentId = fileMeta.getContentId();
                List<FileMeta> fileMetas = fileMetaMapper.findByContentId(contentId);
                if (fileMetas.size() == 1) {
                    fileMetaMapper.updateSetDelete(contentId);
                    dataBlockMapper.updateSetDelete(contentId);
                } else {
                    fileMetaMapper.updateSetDelete(videoFileId);
                }
            }
        });
    }

    private void deleteObject() {
        dataBlockMapper.findByDeleted().forEach(dataBlock -> {
            String absolutePath = dataBlock.getAbsolutePath();
            File file = new File(absolutePath);
            if (file.exists()) {
                //FileUtils.deleteQuietly(file);
                dataBlockMapper.delete(dataBlock);
            } else {
                log.error("file {} not exist", absolutePath);
            }
        });
        fileMetaMapper.deleteFileMetas();

        log.error("#################################################################");
        log.error("delete object done");
        log.error("#################################################################");
    }

    String srcDir = "/opt/oss/disk/40b2cf6e-1501-421a-b7b3-9cbf5b206d39";
    String destDir = "/opt/oss/disk/13f654c8-af87-4710-aac9-7aa086c99aec";
    private void moveDataBlock() {
        List<DataBlock> dataBlocks = dataBlockMapper.findAll();
        for (DataBlock dataBlock : dataBlocks) {
            int id = dataBlock.getId();
            String contentId = dataBlock.getContentId();
            String absolutePath = dataBlock.getAbsolutePath();
            File srcFile = new File(absolutePath);

            String destPath = absolutePath.replace(srcDir, destDir);
            File destFile = new File(destPath);
            if (destFile.exists()) {
                log.info("dest file {} exist", destPath);
            } else {
                try {
                    //FileUtils.moveFile(srcFile, destFile);
                    dataBlockMapper.updatePath(id, destPath);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
