package com.yanzuoguang.cloud.file;

import com.yanzuoguang.util.vo.file.YzgFileMoveReqVo;
import com.yanzuoguang.util.vo.file.YzgFileVideoImageReqVo;
import com.yanzuoguang.mq.service.MqService;
import com.yanzuoguang.mq.vo.MessageVo;
import com.yanzuoguang.mq.vo.QueueVo;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 删除目录
 *
 * @author 颜佐光
 */
@Component
public class YzgFileProcedure implements InitializingBean {

    public static final String YZG_REMOVE_TEMP_FOLDER = "YZG_REMOVE_TEMP_FOLDER";
    public static final String YZG_MOVE_FILE = "YZG_MOVE_FILE";
    public static final String YZG_CONVERT_IMAGE = "YZG_CONVERT_IMAGE";
    public static final String YZG_CONVERT_VIDEO = "YZG_CONVERT_VIDEO";

    @Autowired
    private MqService mqService;

    /**
     * 缓存24小时
     */
    private MemoryCache<Boolean> folderCache = new MemoryCache<>(24 * 60 * 60);

    /**
     * Invoked by a BeanFactory after it has set all bean properties supplied
     * (and satisfied BeanFactoryAware and ApplicationContextAware).
     * <p>This method allows the bean instance to perform initialization only
     * possible when all bean properties have been set and to throw an
     * exception in the event of misconfiguration.
     *
     * @throws Exception in the event of misconfiguration (such
     *                   as failure to set an essential property) or if initialization fails.
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        mqService.createQueue(new QueueVo(YZG_REMOVE_TEMP_FOLDER));
        mqService.createQueue(new QueueVo(YZG_MOVE_FILE));
        mqService.createQueue(new QueueVo(YZG_CONVERT_IMAGE));
        mqService.createQueue(new QueueVo(YZG_CONVERT_VIDEO));
    }

    /**
     * 删除临时文件夹,会自动删除空目录的父文件夹
     *
     * @param serverFolder
     */
    public void removeTempFolder(String serverFolder) {
        synchronized (folderCache) {
            if (StringHelper.toBoolean(folderCache.get(serverFolder))) {
                return;
            }
            folderCache.put(serverFolder, true);
        }
        // 获取当前时间
        Date dt = new Date();
        Date dtTo = DateHelper.addDay(dt, 2);
        String dtToday = DateHelper.getToday(dtTo);
        // 获取延迟时间
        long time = DateHelper.getDateTime(dtToday).getTime() - dt.getTime();
        // 发送删除临时目录的命令
        mqService.message(new MessageVo(YZG_REMOVE_TEMP_FOLDER, serverFolder, time));
    }

    /**
     * 移动文件
     *
     * @param req
     */
    public void moveFile(YzgFileMoveReqVo req) {
        moveFile(JsonHelper.serialize(req), 0);
    }

    /**
     * 移动文件
     *
     * @param json
     * @param dedTime
     */
    public void moveFile(String json, int dedTime) {
        mqService.message(new MessageVo(YZG_MOVE_FILE, json, dedTime));
    }


    /**
     * 转换图片
     *
     * @param req
     */
    public void convertImage(YzgFileVideoImageReqVo req) {
        convertImage(JsonHelper.serialize(req), 0);
    }

    /**
     * 转换图片
     *
     * @param json
     * @param dedTime
     */
    public void convertImage(String json, int dedTime) {
        mqService.message(new MessageVo(YZG_CONVERT_IMAGE, json, dedTime));
    }

    /**
     * 转换视频
     *
     * @param req
     */
    public void convertVideo(YzgFileVideoImageReqVo req) {
        convertVideo(JsonHelper.serialize(req), 0);
    }

    /**
     * 转换视频
     *
     * @param json
     * @param dedTime
     */
    public void convertVideo(String json, int dedTime) {
        mqService.message(new MessageVo(YZG_CONVERT_VIDEO, json, dedTime));
    }

}