package com.skr.mdm.api.app.controller;

import com.skr.mdm.api.app.service.MdmAppStoreService;
import com.skr.mdm.bean.qo.FileChunkQO;
import com.skr.mdm.bean.qo.StrategyUserQO;
import com.skr.mdm.bean.qo.app.*;
import com.skr.mdm.bean.vo.app.*;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author nfq
 * @date 2022-11-24 10:33
 */
@Validated
@RestController
@RequestMapping("app/store")
@Api(tags = "应用发布")
public class MdmAppStoreController {

    @Autowired
    private MdmAppStoreService appStoreService;

    @ApiOperation(value = "列表", response = MdmAppStoreVO.class)
    @PostMapping(value = "list")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:list')")
    public ResponseResult listApp(@RequestBody @Valid AppStoreSearchQO appStoreSearchQO,
                                  @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.listApp(appStoreSearchQO, loginAdminDTO);
    }

    @ApiOperation(value = "详情", response = MdmAppStoreVersionVO.class)
    @PostMapping(value = "info")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:list')")
    public ResponseResult getAppVersionInfo(@RequestParam @NotNull(message = "应用ID不能为空") Integer appId) {
        return appStoreService.getAppVersionInfo(appId);
    }

    @ApiOperation(value = "全部应用名称", response = MdmAppStoreNameVO.class)
    @PostMapping(value = "all/appName")
    public ResponseResult listAllAppName(@RequestParam(required = false) String appName,
                                         @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.listAllAppName(appName, loginAdminDTO);
    }

    @ApiOperation(value = "检查文件是否存在", hidden = true)
    @GetMapping(value = "/upload/chunk")
    public ResponseResult checkChunkExist(FileChunkQO chunkQO) {
        return appStoreService.checkChunkExist(chunkQO);
    }

    @ApiOperation(value = "上传文件分片", hidden = true)
    @PostMapping(value = "upload/chunk")
    public ResponseResult uploadChunk(FileChunkQO chunkDTO, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.uploadChunk(chunkDTO, loginAdminDTO);
    }

    @ApiOperation(value = "解析APK文件", response = MdmApkInfoVO.class)
    @PostMapping(value = "analysis/apk")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:add')")
    public ResponseResult analyticNewApp(@RequestParam("file") MultipartFile file,
                                         @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.analyticNewApp(file, loginAdminDTO);
    }

    @ApiOperation(value = "上传应用图标", response = String.class)
    @PostMapping(value = "icon/upload")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:list')")
    public ResponseResult uploadAppIcon(@RequestParam("file") MultipartFile file,
                                        @RequestParam(required = false) Integer appId,
                                        @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.uploadAppIcon(file, appId, loginAdminDTO);
    }

    @ApiOperation(value = "解析应用新版本", response = MdmApkInfoVO.class)
    @PostMapping(value = "analysis/apk/version")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:version')")
    public ResponseResult analyticAppNewVersion(@RequestParam("file") MultipartFile file,
                                                @RequestParam @NotNull(message = "应用ID不能为空") Integer appId,
                                                @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.analyticAppNewVersion(file, appId, loginAdminDTO);
    }

    @ApiOperation(value = "添加新应用")
    @PostMapping(value = "add")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:add')")
    public ResponseResult addApp(@RequestBody @Valid MdmAppStoreAddQO mdmAppStoreAddQO,
                                 @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.addApp(mdmAppStoreAddQO, loginAdminDTO);
    }

    @ApiOperation(value = "编辑应用")
    @PostMapping(value = "update")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:info')")
    public ResponseResult updateApp(@RequestBody @Valid MdmAppStoreUpdateQO mdmAppStoreUpdateQO,
                                    @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.updateApp(mdmAppStoreUpdateQO, loginAdminDTO);
    }

    @ApiOperation(value = "编辑轻应用")
    @PostMapping(value = "lightApp/update")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:info')")
    public ResponseResult updateLightApp(@RequestBody @Valid MdmLightAppUpdateQO mdmLightAppUpdateQO,
                                         @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.updateLightApp(mdmLightAppUpdateQO, loginAdminDTO);
    }

    @ApiOperation(value = "删除应用")
    @PostMapping(value = "delete")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:delete')")
    public ResponseResult deleteApp(@RequestParam @NotEmpty(message = "应用ID不能为空") List<Integer> appIdList,
                                    @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.deleteApp(appIdList, loginAdminDTO);
    }

    @ApiOperation(value = "删除应用版本")
    @PostMapping(value = "delete/version")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:version')")
    public ResponseResult deleteAppVersion(@RequestParam @NotEmpty(message = "应用版本ID不能为空") List<Integer> appVersionIdList,
                                           @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.deleteAppVersion(appVersionIdList, loginAdminDTO);
    }

    @ApiOperation(value = "启用禁用")
    @PostMapping(value = "onOrOff")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:on:off')")
    public ResponseResult onOrOffApp(@RequestParam @NotNull(message = "应用ID不能为空") Integer appId,
                                     @RequestParam @NotNull(message = "状态不能为空") Integer state,
                                     @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.onOrOffApp(appId, state, loginAdminDTO);
    }

    @ApiOperation(value = "应用版本启用禁用")
    @PostMapping(value = "version/onOrOff")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:version')")
    public ResponseResult onOrOffAppVersion(@RequestParam @NotNull(message = "应用ID不能为空") Integer appVersionId,
                                            @RequestParam @NotNull(message = "状态不能为空") Integer state,
                                            @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.onOrOffAppVersion(appVersionId, state, loginAdminDTO);
    }

    @ApiOperation(value = "指定用户列表", response = AppStoreUserVO.class)
    @PostMapping(value = "user/list")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:list')")
    public ResponseResult listAppUser(@RequestBody @Valid StrategyUserQO strategyUserQO) {
        return appStoreService.listAppUser(strategyUserQO);
    }

    @ApiOperation(value = "指定部门列表", response = AppStoreDeptVO.class)
    @PostMapping(value = "dept/list")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:list')")
    public ResponseResult listAppDept(@RequestBody @Valid StrategyUserQO strategyUserQO) {
        return appStoreService.listAppDept(strategyUserQO);
    }

    @ApiOperation(value = "版本升级")
    @PostMapping(value = "upgrade")
    @PreAuthorize("hasAnyAuthority('sys:app:publish:version')")
    public ResponseResult upgradeVersion(@RequestBody @Valid AppStoreVersionUpgradeQO appStoreVersionUpgradeQO,
                                         @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.upgradeVersion(appStoreVersionUpgradeQO, loginAdminDTO);
    }

    @ApiOperation(value = "进度条")
    @PostMapping("file/progress")
    public ResponseResult getFileUploadProgress(@RequestParam String fileSize, @RequestParam String sessionId,
                                                @ApiIgnore Authentication authentication) {
        LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
        return appStoreService.getFileUploadProgress(fileSize, sessionId, admin);
    }
}