package com.ichangzuo.module.file.service.impl;

import cn.hutool.core.io.FileUtil;
import com.ichangzuo.core.security.util.SecurityUtils;
import com.ichangzuo.module.file.service.FileService;
import com.ichangzuo.system.model.dto.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.stereotype.Component;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

@Component
public class FileServiceImpl implements FileService {
    @Autowired
    private Environment env;

    @Override
    public FileInfo uploadFile(MultipartFile file, String type) {
        if (file != null) {
            String uploadPath = env.getProperty("web.resource-path");
            String suffix = FileUtil.getSuffix(file.getOriginalFilename());

            Date date = new Date();
            SimpleDateFormat fmtYM = new SimpleDateFormat("yyyyMM/");
            String subdir = "";

            String dir = "", filename = "";
            if ("user-name".equals(type)) {
                dir = "User/Name/";
                subdir = fmtYM.format(date);

                Long userId = SecurityUtils.getUserId();
                filename = userId.toString() + "_" + date.getTime() + "." + suffix;

            }
            else if("rich-edit".equals(type)){
                dir = "RichEditor/";
                subdir = fmtYM.format(date);
                filename = date.getTime() + "." + suffix;
            }
            else if(type.contains("software")){
                dir = "Software/App/";

                String[] arrTemp = type.split(",");
                if("ac_win".equals(arrTemp[1])) {
                    subdir = "Ac/Pc/";
                    filename = "Ac_Windows_" + arrTemp[2] + ".exe";
                }
                else if("ac_android".equals(arrTemp[1])) {
                    subdir = "Ac/Android/";
                    filename = "Ac_Android_" + arrTemp[2] + ".apk";
                }
                else if("cz_android".equals(arrTemp[1])) {
                    subdir = "Icz/Android/";
                    filename = "Icz_Android_" + arrTemp[2] + ".apk";
                }
                else if("pro_win".equals(arrTemp[1])) {
                    subdir = "Ac_Pro/Pc/";
                    filename = "Ac_Pro_" + arrTemp[2] + ".exe";
                }
                else if("styleTool".equals(arrTemp[1])) {
                    subdir = "Tools/Pc/";
                    filename = "StyleTools_" + arrTemp[2] + ".exe";
                }
                else
                    return null;
            }

            try {
                String upload_file_dir = uploadPath + dir + subdir;
                File upload_file_dir_file = new File(upload_file_dir);
                if (!upload_file_dir_file.exists()) {
                    if (!upload_file_dir_file.mkdirs())
                        return null;
                }

                File targetFile = new File(upload_file_dir_file, filename);
                file.transferTo(targetFile);

                if( !targetFile.setReadable(true,false) )
                    return null;

                FileInfo fileInfo = new FileInfo();
                fileInfo.setName(filename);
                fileInfo.setUrl(subdir + filename);
                return fileInfo;

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    public boolean deleteFile(String filePath) {
        String uploadPathImg = env.getProperty("web.resource-path");
        return false;
    }
}
