package com.ycwl.basic.device.operator;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.ycwl.basic.device.entity.common.FileObject;
import com.ycwl.basic.storage.StorageFactory;
import com.ycwl.basic.storage.adapters.IStorageAdapter;
import com.ycwl.basic.storage.entity.AliOssStorageConfig;
import com.ycwl.basic.storage.entity.StorageFileObject;
import com.ycwl.basic.storage.enums.StorageType;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class AliOssStorageOperator extends ADeviceStorageOperator {
    private IStorageAdapter adapter;

    public AliOssStorageOperator(String configJson) {
        loadConfig(configJson);
    }

    @Override
    public void loadConfig(String configJson) {
        AliOssStorageConfig config = JSON.parseObject(configJson, AliOssStorageConfig.class);
        adapter = StorageFactory.get(StorageType.ALI_OSS, config);
    }


    private List<StorageFileObject> getOssObjectListByPrefix(String prefix) {
        return adapter.listDir(prefix);
    }

    private List<FileObject> getOssFileListByPrefix(String prefix) {
        List<StorageFileObject> objectList = getOssObjectListByPrefix(prefix);
        if (objectList == null) {
            return null;
        }
        return objectList.stream().filter(item -> item.getName().endsWith(".ts")).map(item -> {
            FileObject object = new FileObject();
            object.setPath(item.getPath());
            object.setUrl(adapter.getUrlForDownload(item.getFullPath()));
            object.setNeedDownload(true);
            String[] splitDir = item.getPath().split("/");
            String splitDate = splitDir[splitDir.length - 1];
            String[] splitExt = item.getName().split("\\.", 2);
            String[] splitDt = splitExt[0].split("_", 2);
            String createTime = splitDt[0];
            String endTime = splitDt[1];
            object.setName(item.getName());
            object.setEndTime(DateUtil.parse(splitDate+endTime, "yyyyMMddHHmmss"));
            object.setCreateTime(DateUtil.parse(splitDate+createTime, "yyyyMMddHHmmss"));
            return object;
        }).collect(Collectors.toList());
    }

    private boolean removeFilesByPrefix(String prefix) {
        return adapter.deleteDir(prefix);
    }

    @Override
    public List<FileObject> getFileListByDtRange(Date startDate, Date endDate) {
        if (startDate == null || endDate == null) {
            return null;
        }
        List<FileObject> fileList = new ArrayList<>();
        if (startDate.after(endDate)) {
            return fileList;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.set(Calendar.SECOND, 0);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        while (calendar.getTime().before(endDate)) {
            String prefix = dateFormat.format(calendar.getTime());
            List<FileObject> fileListByPrefix = getOssFileListByPrefix(prefix);
            if (fileListByPrefix == null) {
                return null;
            }
            fileList.addAll(fileListByPrefix);
            calendar.add(Calendar.MINUTE, 1);
        }
        calendar.clear();
        return fileList.stream()
                .sorted(Comparator.comparing(FileObject::getCreateTime))
                .filter(item -> item.getEndTime().after(startDate))
                .filter(item -> item.getCreateTime().before(endDate))
                .collect(Collectors.toList());
    }

    @Override
    public boolean removeFilesBeforeDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd/");
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String prefix = dateFormat.format(calendar.getTime());
        return removeFilesByPrefix(prefix);
    }
}
