package com.czg.controller.admin;

import com.aliyun.sts20150401.models.AssumeRoleResponse;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.constants.ParamCodeCst;
import com.czg.resp.CzgResult;
import com.czg.system.dto.VersionDTO;
import com.czg.system.service.SysParamsService;
import com.czg.system.service.VersionService;
import com.czg.validator.group.InsertGroup;
import com.czg.validator.group.UpdateGroup;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author GYJoker
 */
@RestController
@RequestMapping("/admin/version")
public class VersionController {
    @Resource
    private VersionService versionService;
    @Resource
    private SysParamsService paramsService;


    /**
     * 新增版本
     *
     * @param versionDTO 版本信息
     * @return Long id
     */
    @PostMapping
    @SaAdminCheckPermission(parentName = "版本控制", value = "version:add", name = "版本添加")
    public CzgResult<Long> insertVersion(@RequestBody @Validated({InsertGroup.class}) VersionDTO versionDTO) {
        return versionService.insertVersion(versionDTO);
    }

    /**
     * 更新版本
     *
     * @param versionDTO 版本信息
     * @return Long id
     */
    @PutMapping
    @SaAdminCheckPermission(parentName = "版本控制", value = "version:update", name = "版本修改")
    public CzgResult<Long> updateVersion(@RequestBody @Validated({UpdateGroup.class}) VersionDTO versionDTO) {
        return versionService.updateVersion(versionDTO);
    }

    /**
     * 删除版本
     *
     * @param id 版本id
     * @return Long id
     */
    @DeleteMapping("/{id}")
    @SaAdminCheckPermission(parentName = "版本控制", value = "version:delete", name = "版本删除")
    public CzgResult<Long> deleteVersion(@PathVariable Long id) {
        return versionService.deleteVersion(id);
    }

    /**
     * 获取版本信息
     *
     * @param source 渠道
     * @param type   类型
     * @return VersionDTO
     */
    @GetMapping("/{source}/{type}")
//    @SaAdminCheckPermission(parentName = "版本控制",value = "version:get", name = "版本获取")
    public CzgResult<VersionDTO> getVersionInfo(@PathVariable("source") String source, @PathVariable("type") String type) {
        return versionService.getVersionInfo(source, type);
    }

    /**
     * 获取版本列表
     *
     * @return List<VersionDTO>
     */
    @GetMapping("/list")
    @SaAdminCheckPermission(parentName = "版本控制", value = "version:getList", name = "版本列表")
    public CzgResult<List<VersionDTO>> getVersionList() {
        return versionService.getVersionList();
    }


    /**
     * 获取上传临时凭证
     */
    @GetMapping(value = "/getCredentials")
    public CzgResult<Object> getCredentials() {
        try {
            Map<String, String> ossKeyMap = paramsService.getParamsByMap("ossKeySet", ParamCodeCst.OSS_KEY_SET);
            com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                    // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                    .setAccessKeyId(ossKeyMap.get(ParamCodeCst.AliYun.ALI_OSS_ACCESS_KEY))
                    // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                    .setAccessKeySecret(ossKeyMap.get(ParamCodeCst.AliYun.ALI_OSS_ACCESS_SECRET));

            // Endpoint 请参考 https://api.aliyun.com/product/Sts
            config.endpoint = ossKeyMap.get(ParamCodeCst.AliYun.ALI_OSS_ENDPOINT);

            com.aliyun.sts20150401.Client client = new com.aliyun.sts20150401.Client(config);
            com.aliyun.sts20150401.models.AssumeRoleRequest assumeRoleRequest = new com.aliyun.sts20150401.models.AssumeRoleRequest();
            assumeRoleRequest.setRoleArn(ossKeyMap.get(ParamCodeCst.AliYun.ALI_OSS_ROLE_ARN));
            assumeRoleRequest.setRoleSessionName("test");
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            // 复制代码运行请自行打印 API 的返回值
            AssumeRoleResponse response = client.assumeRoleWithOptions(assumeRoleRequest, runtime);
            return CzgResult.success(response.getBody().getCredentials());
        } catch (Exception error) {
            return CzgResult.failure(error.getMessage());
        }
    }
}
