package com.weaver.seconddev.chapanda.esign.util;

import com.weaver.eteams.file.client.file.FileObj;
import com.weaver.eteams.file.client.param.RemoteUploadParam;
import com.weaver.file.ud.api.FileUploadService;
import com.weaver.seconddev.chapanda.esign.constant.Constants;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;

@Component
public class Esb2EsignFileUploadUtil {
    private final static Logger log = LoggerFactory.getLogger(Esb2EsignFileUploadUtil.class);

    @Autowired
    private FileUploadService fileUploadService;

    /**
     *
     * @return
     */
    public FileObj uploadFile(String downloadUrl,String fileName,Long folderid){
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        FileObj fileObj = new FileObj();
        log.error("downloadUrl:{}",downloadUrl);
        try {

            Request request = new Request.Builder()
                    .url(downloadUrl)
                    .method("GET", null)
                    .build();
            Response response = client.newCall(request).execute();
            log.error("code:{}",response.code());
            log.error("isSuccessful:{}",response.isSuccessful());
            if(response.code() == 200){
                fileObj = createDocInfo(response,fileName,folderid);
            }
        } catch (IOException e) {
            log.error("e:"+e.getMessage());
            throw new RuntimeException(e);
        }
        return fileObj;
    }

    public FileObj createDocInfo(Response response,String fileName,Long folderid){
        FileObj fileObj = new FileObj();
        if(response.isSuccessful()){
            try {
                long fileSize = response.body().contentLength();
                byte[] fileByte = response.body().bytes();
                log.error("fileByte:{}",fileByte.length);
                if(fileByte.length > 0){

                    String filename = fileName+".pdf";
                    String lastModified = String.valueOf(System.currentTimeMillis());
                    RemoteUploadParam remoteUploadParam = new RemoteUploadParam(filename, lastModified, "document");
                    remoteUploadParam.setFolderId(folderid);
                    remoteUploadParam.setCreateDoc(true);
                    remoteUploadParam.setSize(fileSize);
                    log.error("LongSysUserId:{}",Constants.LongSysUserId);
                    fileObj = fileUploadService.uploadLocalFile(fileByte,Constants.LongSysUserId,"application/octet-stream",remoteUploadParam);
                    log.error("fileObj:{}",fileObj.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("e:{}",e);
            }
        }
        return fileObj;
    }


    public FileObj createDocInfo2(Response response){
        FileObj fileObj = new FileObj();
        if(response.isSuccessful()){
            InputStream inputStream = null;
            try {
                inputStream = response.body().byteStream();
                long fileSize = response.body().contentLength();

                byte[] fileByte = response.body().bytes();
                log.error("fileByte:{}",fileByte.length);

                String filename = "测试合同11.pdf";
                String lastModified = String.valueOf(System.currentTimeMillis());
                RemoteUploadParam remoteUploadParam = new RemoteUploadParam(filename, lastModified, "document");
                remoteUploadParam.setFolderId(Constants.htwjFolderId);
                remoteUploadParam.setCreateDoc(true);
                remoteUploadParam.setSize(fileSize);

                log.error("LongSysUserId:{}",Constants.LongSysUserId);
                fileObj = fileUploadService.uploadLocalFile(fileByte,Constants.LongSysUserId,"application/octet-stream",remoteUploadParam);

                log.error("fileObj:{}",fileObj.toString());

                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
                log.error("e:{}",e);
            } finally {
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return fileObj;
    }

}
