package com.bohuikeji.frame.module.admin.controller.admin;

import com.bohuikeji.frame.module.admin.controller.api.SysApi;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.sys.SysApp;
import com.bohuikeji.frame.module.admin.entity.util.ApkInfo;
import com.bohuikeji.frame.module.admin.service.ISysAppService;
import com.bohuikeji.frame.module.admin.utils.ApkUtils;
import com.bohuikeji.frame.module.admin.utils.FileUtil;
import com.bohuikeji.frame.module.admin.utils.MinioUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * APP操作类
 * @author Admin
 */
@Slf4j
@RestController
@RequestMapping("/admin/app")
@RequiredArgsConstructor
public class AppAdmin {
    final ISysAppService appService;
    /**
     * 热更新包文件类型
     */
    private static final String[] HOT_TYPES ={".wgt"};

    /**
     * 上传并解析安装包
     * @return
     */
    @PostMapping("/upload")
    public APIEntity<SysApp> upload(@RequestParam("file") MultipartFile mFile,@RequestParam("role")String role) throws IOException {
        // 获取文件名
        String fileName = mFile.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        String url=MinioUtil.uploadFiles(mFile);
        boolean isHot=false;
        for (String hotType : HOT_TYPES) {
            if(hotType.equalsIgnoreCase(prefix)){
                isHot=true;
            }
        }
        File file = File.createTempFile(fileName, prefix);
        mFile.transferTo(file);
        ApkInfo info;
        if (isHot){
            info=ApkUtils.getWgtInfo(file);
        }else {
            info=ApkUtils.getApkInfo(file);
        }
        SysApp app=appService.selectOneByColumn("app_key",String.join("_",role,"android",isHot?"hot":"install"));
        if (info.getVersionCode().compareTo(Integer.valueOf(app.getVersionCode()))<0){
            return APIEntity.fail("您上传包的【大版本号】低于线上版本。({}==>{})",app.getVersionCode(),info.getVersionCode());
        }
        if (!SysApi.versionComp(app.getVersion(),info.getVersionName())){
            return APIEntity.fail("您上传包的【版本号】低于线上版本。({}==>{})",app.getVersion(),info.getVersionName());
        }
        app.setVersionCode(app.getVersionCode()+"==>"+info.getVersionCode());
        app.setVersion(app.getVersion()+"==>"+info.getVersionName());
        app.setUrl(url);
        return APIEntity.success(app);
    }

    /**
     * 确认更新
     * @param app
     * @return
     * @throws IOException
     */
    @PostMapping("/update")
    public APIEntity<Boolean> upload(SysApp app) throws IOException {
        app.setVersion(app.getVersion().split("==>")[1]);
        app.setVersionCode(app.getVersionCode().split("==>")[1]);
        return APIEntity.success(appService.updateById(app));
    }

}
