package com.njyunzhi.pet_identity.controller;

import com.njyunzhi.pet_identity.common.*;
import com.njyunzhi.pet_identity.config.AliyunConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "通用")
@RestController
@RequestMapping("/{client}")
public class CommController extends BaseController {

    @Autowired
    OSSUtils ossUtils;

    @Autowired
    AliyunConfig aliyunConfig;

    @Autowired
    SMSCaptcha smsCaptcha;

    @Autowired
    HttpUtils httpUtils;

    /**
     * 图片
     * @param multipartFile
     * @return
     */
    @ApiOperation(value = "图片上传", notes = "图片上传")
    @PostMapping("/image")
    public ResponseBean uploadImage(@ApiParam("客户端") @PathVariable String client,
                                    @RequestParam("file") MultipartFile multipartFile) {
        try {
            String img = ossUtils.putObject(multipartFile);
            return ResponseBean.success(img);
        } catch (IOException e) {
            return ResponseBean.error("上传图片失败: " + e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    /**
     * base64 图片上传
     * @param data
     * @return
     */
    @ApiOperation(value = "base64 图片上传", notes = "base64 图片上传")
    @PostMapping("/image/base64")
    public ResponseBean uploadBase64Image(@ApiParam("客户端") @PathVariable String client,
                                          @RequestBody Map<String, String> data) {
        try {
            String base64Str = data.get("base64");
            String img = ossUtils.putObject(base64Str);
            return ResponseBean.success(img);
        } catch (IOException e) {
            return ResponseBean.error("上传图片失败: " + e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    @ApiOperation(value = "发送手机验证码", notes = "发送手机验证码")
    @PostMapping("/sms-captcha")
    public ResponseBean sendCaptcha(@ApiParam("客户端") @PathVariable String client,
                                    @ApiParam("手机号") @RequestParam String phone) throws Exception {
        smsCaptcha.send(phone, smsCaptcha.randCaptcha(6));
        return ResponseBean.success("发送成功");
    }

    /**
     * 获取文件上传凭证
     * @return
     */
    @ApiOperation(value = "获取文件上传凭证", notes = "获取文件上传凭证")
    @GetMapping("/oss-sts")
    public ResponseBean getOSSSTS(@ApiParam("客户端") @PathVariable String client) {
        AliyunConfig.Oss oss = aliyunConfig.getOss();

        try {
            Map<String, Object> res = ossUtils.getSTSAKToken();
            res.put("endpoint", oss.getEndpoint());
            res.put("bucket", oss.getBucketName());
            res.put("bucketURL", oss.getBucketURL());
            res.put("region", oss.getRegion());
            res.put("path", getMonth());

            return ResponseBean.success(res);
        } catch (Exception e) {
            return ResponseBean.error("获取上传凭证失败! ", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    private String getMonth() {
        return DateTimeFormatter.ofPattern("yyyy-MM").format(LocalDateTime.now());
    }

}
