package cn.reghao.tnb.file.app.rpc;

import cn.reghao.file.api.dto.JobInfo;
import cn.reghao.file.api.iface.FileService;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.tnb.file.app.zjob.db.JobDetailMapper;
import cn.reghao.tnb.file.app.zjob.model.constant.JobStatus;
import cn.reghao.tnb.file.app.zjob.model.po.JobDetail;
import cn.reghao.tnb.file.app.util.QRCode;
import cn.reghao.tnb.file.app.model.constant.OssType;
import org.apache.commons.io.FileUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2024-04-23 15:13:55
 */
@DubboService
@Service
public class FileServiceImpl implements FileService {
    private final int ossType = 1;
    private final JobDetailMapper jobDetailMapper;
    private final String baseDir;

    private FileServiceImpl(JobDetailMapper jobDetailMapper) {
        this.jobDetailMapper = jobDetailMapper;
        this.baseDir = System.getProperty("java.io.tmpdir");
    }

    @Deprecated
    public String getAccountAvatar(long userId) {
        String siteAddr = "";
        int avatarChannelCode = 0;
        String text = String.format("%s/user/%s", siteAddr, userId);
        int width = 128;
        int height = 128;
        String filePath = String.format("%s/%s.jpg", baseDir, userId);
        String avatarUrl = "";
        try {
            ByteArrayOutputStream baos = QRCode.createQRCode(text, width, height);
            File file = new File(filePath);
            FileUtils.writeByteArrayToFile(file, baos.toByteArray());
            if (ossType == OssType.localOss.getCode()) {
                avatarUrl = getImageUrl(avatarChannelCode, file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            FileUtils.deleteQuietly(new File(filePath));
        }

        return avatarUrl;
    }

    private String getImageUrl(int channelCode, File file) throws Exception {
        /*OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
        ServerInfo serverInfo = ossConsoleClient.getUploadStore(channelCode);
        if (serverInfo == null) {
            return null;
        }

        String ossUrl = serverInfo.getOssUrl();
        String token = serverInfo.getToken();
        OssStoreClient ossStoreClient = new OssStoreClient(ossUrl);
        UploadFileRet uploadFileRet = ossStoreClient.postObject(file, channelCode, token);
        if (uploadFileRet != null) {
            String uploadId = uploadFileRet.getUploadId();
            ImageInfo imageInfo = ossConsoleClient.getImageInfo(channelCode, uploadId);
            if (imageInfo != null) {
                return imageInfo.getUrl();
            }
        }*/

        return null;
    }

    @Override
    public PageList<JobInfo> getJobs(int pn, int ps) {
        List<JobDetail> list = jobDetailMapper.findAll();
        List<JobInfo> jobList = list.stream().map(jobDetail -> {
            long jobId = jobDetail.getJobId();
            String jobName = jobDetail.getJobName();
            String status = JobStatus.getDescByCode(jobDetail.getStatus());
            LocalDateTime startAt = jobDetail.getStartAt();
            LocalDateTime endAt = jobDetail.getEndAt();
            return new JobInfo(jobId, jobName, status, startAt, endAt);
        }).collect(Collectors.toList());
        return PageList.pageList(pn, ps, jobList.size(), jobList);
    }
}
