package com.yiqi.crm.controller;

import com.yiqi.common.config.CompositeWxMaService;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.crm.service.CrmWechatSettingService;
import com.yiqi.crm.service.IWxMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.java.Log;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.yiqi.common.core.domain.AjaxResult.success;

/**
 * @author xiaohei
 * @date 2023/7/18
 */
@Log
@Api(tags = "微信消息回复配置")
@AllArgsConstructor
@RestController
@RequestMapping(value = "/crm/wechatSetting")
public class CrmWechatSettingController {

    @Autowired
    CompositeWxMaService compositeWxMaService;

    @Autowired
    CrmWechatSettingService crmWechatSettingService;

    @Autowired
    IWxMenuService wxMenuService;

    @ApiOperation("测试")
    @GetMapping(value = "test")
    public AjaxResult test(@RequestParam @ApiParam("测试参数") @NotEmpty(message = "测试参数不能为空") String test) {
        return success();
    }

    @GetMapping("/handleWxEvent")
    public String check(String signature,
                        String timestamp,
                        String nonce,
                        String echostr) {
        if (!compositeWxMaService.getPublic().checkSignature(timestamp, nonce, signature)) {
            throw new IllegalArgumentException("非法请求，可能属于伪造的请求！");
        }
        return echostr;
    }

    /**
     * 回复微信公众号信息
     *
     * @param requestBody
     * @param signature
     * @param timestamp
     * @param nonce
     * @param openid
     * @param encType
     * @param msgSignature
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    @ApiOperation("回复微信公众号信息")
    @PostMapping(value = "/handleWxEvent", produces = "application/xml; charset=UTF-8")
    public String handleWxEvent(@RequestBody String requestBody,
                                @RequestParam("signature") String signature,
                                @RequestParam("timestamp") String timestamp,
                                @RequestParam("nonce") String nonce,
                                @RequestParam("openid") String openid,
                                @RequestParam(name = "encrypt_type", required = false) String encType,
                                @RequestParam(name = "msg_signature", required = false) String msgSignature) throws IOException, DocumentException {
        log.info("\n接收微信请求： requestBody : " + requestBody);
        // 获取回复信息
        return crmWechatSettingService.getOutMsg(requestBody, signature, encType, timestamp, nonce, msgSignature);
    }

    /**
     * 上传图片到微信素材平台
     *
     * @param file
     * @param type
     * @return mediaId
     * @throws IOException
     */
    @PostMapping("/uploadMaterial")
    public String uploadMaterial(MultipartFile file, @RequestParam String type) throws IOException {
        return crmWechatSettingService.uploadMaterial(file, type);
    }

    /**
     * 获取菜单信息
     *
     * @return
     * @throws WxErrorException
     */
    @GetMapping("/getMenu")
    public AjaxResult getMenu() throws WxErrorException {
        WxMpMenu wxMpMenu = null;
        try {
            wxMpMenu = compositeWxMaService.getPublic().getMenuService().menuGet();
        } catch (Exception e) {
            log.warning(e.getMessage());
        }
        return success(wxMpMenu);
    }

    /**
     * 更新微信公众号菜单
     *
     * @param menuButton
     * @return mediaId
     * @throws IOException
     */
    @PostMapping("/updateMenu")
    public AjaxResult updateMenu(WxMenuButton menuButton) {
        List<WxMenuButton> buttons = new ArrayList<>();
        return success();
    }

    /**
     * 删除菜单(会删除全部)
     *
     * @return mediaId
     * @throws WxErrorException
     */
    @GetMapping("/deleteMenu")
    public AjaxResult deleteMenu() throws WxErrorException {
        compositeWxMaService.getPublic().getMenuService().menuDelete();
        return success();
    }

}
