package com.keystar.plane.inspection.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.keystar.plane.inspection.config.MinIoConfig;
import io.minio.MinioClient;
import io.minio.Result;
import io.minio.errors.*;
import io.minio.messages.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xmlpull.v1.XmlPullParserException;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MinioService {

    @Resource
    private MinioClient minioClient;

    @Autowired
    private MinIoConfig minIoConfig;


    /**
     * 判断 bucket是否存在
     * @param bucketName
     * @return
     */
    public boolean bucketExists(String bucketName){
        try {
            return minioClient.bucketExists(bucketName);
        } catch (Exception e) {
            log.error("判断 bucket是否存在,出现错误:{}",e);
            return false;
        }
    }

    /**
     * 根据 bucket 获取该桶下边的所有目录
     * @param bucketName
     */
    public List<Item> listObject(String bucketName){
        try {
            Iterable<Result<Item>> myObjects = minioClient.listObjects(bucketName);
            return getItems(myObjects);
        } catch (Exception e) {
            log.error("获取桶{}的文件内容,出现错误:{}",bucketName,e);
            return null;
        }
    }

    private List<Item> getItems(Iterable<Result<Item>> myObjects) throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException, InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException, InternalException {
        List<Item> items = CollUtil.newArrayList();
        for (Result<Item> result : myObjects) {
            items.add(result.get());
        }
        return items;
    }

    /**
     * 获取文件流
     * @param bucketName
     * @param objectName
     * @return
     */
    public InputStream getObject(String bucketName, String objectName)  {
        try {
            return minioClient.getObject(bucketName,objectName);
        } catch (Exception e) {
            log.error("获取桶{}的{}文件内容,出现错误:{}",bucketName,objectName,e);
        }
        return null;
    }


    /***
     * 根据 bucket 和 prefix 获取该所有目录和文件
     * @param bucketName
     * @param prefix
     * @param recursive
     * @param useVersion1
     * @return
     */
    public List<Item> listObject(String bucketName,String prefix, boolean recursive, boolean useVersion1){
        try {
            Iterable<Result<Item>> myObjects = minioClient.listObjects(bucketName,prefix,recursive,useVersion1);
            return getItems(myObjects);
        } catch (Exception e) {
            log.error("获取桶{}的文件内容,出现错误:{}",bucketName,e);
            return null;
        }
    }


    /**
     * 获取最新架次的红外图片地址
     **/
    public List<String> getInfraredPhoto(){
        List<String> infraredList = new ArrayList<>();
        // 获取最新航线
        List<Item> missions = this.listObject(minIoConfig.getBucketName(), minIoConfig.getResourcePath(), false, false);
        String missionId = missions.get(missions.size()-1).objectName();
        // 获取最新架次
        List<Item> records = this.listObject(minIoConfig.getBucketName(), missionId, false, false);
        String recordsId = records.get(records.size()-1).objectName();
        // 获取红外图片
        List<Item> photos = this.listObject(minIoConfig.getBucketName(), recordsId, false, false);
        List<String> photoList = photos.stream().map(Item::objectName).collect(Collectors.toList());
        for ( String name : photoList){
            if (name.contains("THRM.jpg") || name.contains("T.jpg")){
                infraredList.add(name);
            }
        }
        return infraredList;
    }



    

}
