package com.yunfeiyun.agmp.common.web.system.domain;

import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.constant.ResConstants;
import com.yunfeiyun.agmp.common.exception.BizException;
import com.yunfeiyun.agmp.common.exception.OssFileUploadException;
import com.yunfeiyun.agmp.common.framework.oss.cloud.OSSFactory;
import com.yunfeiyun.agmp.common.framework.web.service.PolicyService;
import com.yunfeiyun.agmp.common.utils.StringUtils;
import com.yunfeiyun.agmp.common.utils.file.FileUploadUtils;
import com.yunfeiyun.agmp.common.utils.spring.SpringUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 资源构造器
 *
 * @author 杨晓辉
 */
public class ResBuilder {

    private String resId;
    private List<String> resIds;
    private String[] resIdes;
    private String resBusId;
    private ResConstants.mediaType resMediatype;
    private ResConstants.BizType resBiztype;
    private String resUrl;
    private Long resSeq = 1L;
    private String resCreatorname;
    private String resCreator;
    private String resCreateddate;
    private MultipartFile file;
    private List<MultipartFile> files;

    public ResBuilder setFiles(List<MultipartFile> files) {
        this.files = files;
        return this;
    }

    public ResBuilder setResIdes(String[] resIdes) {
        this.resIdes = resIdes;
        return this;
    }

    public ResBuilder setResIds(List<String> resIds) {
        this.resIds = resIds;
        return this;
    }

    public ResBuilder setFile(MultipartFile file) {
        this.file = file;
        return this;
    }

    public ResBuilder setResId(String resId) {
        this.resId = resId;
        return this;
    }

    public ResBuilder setResBusId(String resBusId) {
        this.resBusId = resBusId;
        return this;
    }

    public ResBuilder setResMediatype(ResConstants.mediaType resMediatype) {
        this.resMediatype = resMediatype;
        return this;
    }

    public ResBuilder setResBiztype(ResConstants.BizType resBiztype) {
        this.resBiztype = resBiztype;
        return this;
    }

    public ResBuilder setResUrl(String resUrl) {
        this.resUrl = resUrl;
        return this;
    }

    public ResBuilder setResSeq(Long resSeq) {
        this.resSeq = resSeq;
        return this;
    }

    public ResBuilder setResCreatorname(String resCreatorname) {
        this.resCreatorname = resCreatorname;
        return this;
    }

    public ResBuilder setResCreator(String resCreator) {
        this.resCreator = resCreator;
        return this;
    }

    public ResBuilder setResCreateddate(String resCreateddate) {
        this.resCreateddate = resCreateddate;
        return this;
    }



    public SysRes build(){
        SysRes sysRes =  new SysRes();
        try{
            sysRes.setResId(StringUtils.isEmpty(resId) ? sysRes.getId() : resId);
            if(resBiztype !=null ) sysRes.setResBiztype(resBiztype.getKey());
            if(resMediatype !=null ) sysRes.setResMediatype(resMediatype.getValue());
            sysRes.setResSeq(resSeq);
            sysRes.setResCreateddate(resCreateddate);
            sysRes.setResBusId(resBusId);
            sysRes.setResCreator(resCreator);
            sysRes.setResCreatorname(resCreatorname);
            if(file !=null && sysRes.getResId() != null && resBiztype !=null  ) {
                sysRes.setResUrl(FileUploadUtils.upload(resBiztype, file, sysRes.getResId()));
                if(SpringUtils.getBean(PolicyService.class).isOssCloudPolicy()){
                    sysRes.setResOssUrl(  OSSFactory.build().uploadSuffix(file.getBytes(), FileUploadUtils.getExtension(file) , resBiztype.getPath(), FilenameUtils.getBaseName(file.getOriginalFilename())));
                }
            }
        }catch (OssFileUploadException e){
            sysRes.setResOssUrl(sysRes.getResUrl());  //阿里云可能不通，直接使用线下
            return sysRes;
        }catch (Exception e){
            throw new BizException(ErrorCode.UPLOAD_FAIL.getMessage(), ErrorCode.UPLOAD_FAIL.getCode());
        }
        return sysRes;
    }

    public SysRes build(String resBusId){
        SysRes sysRes =  new SysRes();
        try{
            if(resBiztype !=null ) sysRes.setResBiztype(resBiztype.getKey());
            if(resMediatype !=null ) sysRes.setResMediatype(resMediatype.getValue());
            sysRes.setResSeq(resSeq);
            sysRes.setResCreateddate(resCreateddate);
            sysRes.setResBusId(resBusId);
            sysRes.setResCreator(resCreator);
            sysRes.setResCreatorname(resCreatorname);
            if(file !=null && sysRes.getResId() != null && resBiztype !=null  ) {
                sysRes.setResUrl(FileUploadUtils.upload(resBiztype, file, sysRes.getResId()));
                if(SpringUtils.getBean(PolicyService.class).isOssCloudPolicy()){
                    sysRes.setResOssUrl(  OSSFactory.build().uploadSuffix(file.getBytes(), FileUploadUtils.getExtension(file) , resBiztype.getPath(), FilenameUtils.getBaseName(file.getOriginalFilename())));
                }
            }
        }catch (OssFileUploadException e){
            sysRes.setResOssUrl(sysRes.getResUrl());  //阿里云可能不通，直接使用线下
            return sysRes;
        }catch (Exception e){
            throw new BizException(ErrorCode.UPLOAD_FAIL.getMessage(), ErrorCode.UPLOAD_FAIL.getCode());
        }
        return sysRes;
    }


    public List<SysRes> build(List resIds){
        List<SysRes> sysRess =  new ArrayList<>();
        this.resIds.forEach(v ->{
            SysRes sysRes = new SysRes();
            sysRes.setResId( v.toString() );
            sysRes.setResSeq(resSeq);
            sysRes.setResBusId(resBusId);
            sysRess.add(sysRes);
        });
        return sysRess;
    }

    public List<SysRes> build(String[] resIdes){
        List<SysRes> sysRess =  new ArrayList<>();
        Iterator<String> iterator = Arrays.stream(this.resIdes).iterator();
        while(iterator.hasNext()) {
            SysRes sysRes = new SysRes();
            sysRes.setResId( iterator.next() );
            sysRes.setResSeq(resSeq);
            sysRes.setResBusId(resBusId);
            sysRess.add(sysRes);
        }
        return sysRess;
    }

    public List<SysRes> build(List<MultipartFile> files , int type){
        List<SysRes> sysRess =  new ArrayList<>();
        try{
            for (MultipartFile file : this.files){
                SysRes sysRes =  new SysRes();
                sysRes.setResId( sysRes.getId() );
                if(resBiztype !=null ) sysRes.setResBiztype(resBiztype.getKey());
                if(resMediatype !=null ) sysRes.setResMediatype(resMediatype.getValue());
                sysRes.setResSeq(resSeq);
                sysRes.setResCreateddate(resCreateddate);
                sysRes.setResBusId(resBusId);
                sysRes.setResCreator(resCreator);
                sysRes.setResCreatorname(resCreatorname);
                if(file !=null && sysRes.getResId() != null && resBiztype !=null  ) {
                    sysRes.setResUrl(FileUploadUtils.upload(resBiztype, file, sysRes.getResId()));
                    if(SpringUtils.getBean(PolicyService.class).isOssCloudPolicy()){
                        sysRes.setResOssUrl(  OSSFactory.build().uploadSuffix(file.getBytes(), FileUploadUtils.getExtension(file) , resBiztype.getPath(), FilenameUtils.getBaseName(file.getOriginalFilename())));
                    }
                }
                return sysRess;
            }
        }catch (Exception e){
            throw new BizException(ErrorCode.UPLOAD_FAIL.getMessage(), ErrorCode.UPLOAD_FAIL.getCode());
        }
        return sysRess;
    }


}
