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

import cn.reghao.file.api.iface.JobService;
import cn.reghao.jutil.jdk.string.SnowFlake;
import cn.reghao.tnb.content.api.iface.AdminVideoService;
import cn.reghao.tnb.content.api.iface.MallService;
import cn.reghao.tnb.file.app.zjob.db.JobDetailMapper;
import cn.reghao.tnb.file.app.zorder.db.OrderMapper;
import cn.reghao.tnb.file.app.zjob.JobContext;
import cn.reghao.tnb.file.app.zjob.DelayJob;
import cn.reghao.tnb.file.app.zjob.task.ConvertTask;
import cn.reghao.tnb.file.app.zjob.task.ConvertTaskInfo;
import cn.reghao.tnb.file.app.zjob.task.OrderTask;
import cn.reghao.tnb.file.app.zjob.task.PublishVideoTask;
import cn.reghao.tnb.file.app.zjob.model.constant.JobStatus;
import cn.reghao.tnb.file.app.zjob.model.po.JobDetail;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author reghao
 * @date 2024-12-04 13:31:48
 */
@DubboService
@Service
public class JobServiceImpl implements JobService {
    @DubboReference(check = false, timeout = 60_000)
    private AdminVideoService adminVideoService;
    @DubboReference(check = false, timeout = 60_000)
    private MallService mallService;

    private final SnowFlake idGenerator;
    private final JobContext jobContext;
    private final JobDetailMapper jobDetailMapper;
    private final String baseDir;
    private final OrderMapper orderMapper;

    public JobServiceImpl(JobContext jobContext, JobDetailMapper jobDetailMapper, OrderMapper orderMapper) {
        this.idGenerator = new SnowFlake(1L, 1L);
        this.jobContext = jobContext;
        this.jobDetailMapper = jobDetailMapper;
        this.baseDir = System.getProperty("java.io.tmpdir");
        this.orderMapper = orderMapper;
    }

    @Override
    public long addOrderTimeoutJob(long orderId, long delaySecond) {
        long jobId = idGenerator.nextId();
        OrderTask orderTask = new OrderTask(mallService, orderMapper, orderId);
        DelayJob delayJob = new DelayJob(jobId, orderTask, delaySecond);
        jobContext.addJob(delayJob);

        int status = JobStatus.Prepare.getValue();
        JobDetail jobDetail = new JobDetail(jobId, delayJob, status);
        jobDetailMapper.save(jobDetail);
        return jobId;
    }

    @Override
    public long addPublishVideoJob(String videoId, long publishAt) {
        long jobId = idGenerator.nextId();
        PublishVideoTask publishVideoTask = new PublishVideoTask(jobId, jobDetailMapper, adminVideoService, videoId);
        long delaySecond = (publishAt - System.currentTimeMillis())/1000;
        DelayJob delayJob = new DelayJob(jobId, publishVideoTask, delaySecond);
        jobContext.addJob(delayJob);

        int status = JobStatus.Prepare.getValue();
        JobDetail jobDetail = new JobDetail(jobId, delayJob, status);
        jobDetailMapper.save(jobDetail);
        return jobId;
    }

    @Override
    public long addConvertVideoJob(String videoFileId, int channelCode) {
        long jobId = idGenerator.nextId();
        try {
            /*OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
            ObjectInfo objectInfo = ossConsoleClient.getObjectInfo(channelCode, videoFileId);
            if (objectInfo == null) {
                throw new Exception("not found ObjectInfo by videoFileId " + videoFileId);
            }

            String objectName = objectInfo.getObjectName();*/
            String localPath = "ossConsoleClient.getObject(objectName, channelCode, baseDir)";
            String destPath = String.format("%s/%s", baseDir, UUID.randomUUID());
            ConvertTaskInfo convertTaskInfo =
                    new ConvertTaskInfo(videoFileId, channelCode, localPath, destPath, adminVideoService);
            ConvertTask convertTask = new ConvertTask(jobId, jobDetailMapper, convertTaskInfo);
            long delaySecond = 5;
            DelayJob delayJob = new DelayJob(jobId, convertTask, delaySecond);
            jobContext.addJob(delayJob);

            int status = JobStatus.Running.getValue();
            JobDetail jobDetail = new JobDetail(jobId, delayJob, status);
            jobDetailMapper.save(jobDetail);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jobId;
    }
}
