package cn.teammodel.controller.frontend;

import cn.hutool.json.JSONObject;
import cn.teammodel.common.R;
import cn.teammodel.manager.wx.WechatUtilService;
import cn.teammodel.model.dto.Wechat.*;
import cn.teammodel.model.vo.WechatResponseMsgVo;
import cn.teammodel.service.WechatService;
import com.alibaba.fastjson2.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/wechat")
@Api(tags = "对接微信开放平台接口")
public class WechatController {

    @Resource
    private WechatUtilService wechatUtilService;

    @Resource
    private WechatService wechatService;

    /**
     * 获取openId
     * @param reqDto
     * @return
     */
    @PostMapping("openId")
    @ApiOperation("发送订阅消息")
    public R<String> GetOpenId(@RequestBody @Valid BaseInfoReqDto reqDto){
        String  openIdReq = wechatUtilService.GetOpenId(reqDto.getCode());
        JSONObject jsonObject = new JSONObject(openIdReq);
        String openId = jsonObject.getStr("openid");
        return R.success(openId);
    }

    /**
     * 评价完成通知
     * @param appraiseDto
     * @return
     */
    @PostMapping("send/appraise/message")
    @ApiOperation("发送评价模板订阅消息")
    public WechatResponseMsgVo SendAppraiseSubscriptionMessage(@RequestBody @Valid AppraiseReqDto appraiseDto) {
        WechatResponseMsgVo wechatResponseMsgVo = wechatService.AppraisePushMsg(appraiseDto);
        //WechatResponseMsgVo wechatResponseMsgVo = JSON.parseObject(sendMsg, WechatResponseMsgVo.class);
        return wechatResponseMsgVo;
    }

    /**
     * 考试成绩通知
     * @param examReqDto
     * @return
     */
    @PostMapping("send/exam/message")
    @ApiOperation("发送考试成绩订阅消息")
    public WechatResponseMsgVo SendExamSubscriptionMessage(@RequestBody @Valid ExamReqDto examReqDto) {
        WechatResponseMsgVo sendMsg = wechatService.ExamPushMsg(examReqDto);
        return sendMsg;
    }
    /**
     * 报告生成通知
     * @param reportReqDto
     * @return
     */
    @PostMapping("send/report/message")
    @ApiOperation("发送报告生成订阅消息")
    public WechatResponseMsgVo SendReportSubscriptionMessage(@RequestBody @Valid ReportReqDto reportReqDto) {
        WechatResponseMsgVo sendMsg = wechatService.ReportPushMsg(reportReqDto);
        return sendMsg;
    }

    /**
     * 职业测试报告通知
     * @param vocationalReqDto
     * @return
     */
    @PostMapping("send/vocational/message")
    @ApiOperation("发送职业测试报告订阅消息")
    public WechatResponseMsgVo SendVocationalSubscriptionMessage(@RequestBody @Valid VocationalReqDto vocationalReqDto) {
        WechatResponseMsgVo sendMsg = wechatService.VocationalPushMsg(vocationalReqDto);
        return sendMsg;
    }

    /**
     * 发送订阅消息
     * @return
     */
    @PostMapping("test/send/subscription/message")
    @ApiOperation("测试发送订阅消息")
    public String TestSendSubscriptionMessage() {
        //String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();

        String token = wechatUtilService.GetToken();
        String accessToken = wechatUtilService.GetAccessToken();

        // 处理微信推送数据结构
        JSONObject mapData = new JSONObject();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("value", "用户-PL");
        mapData.set("name1", map1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("value", "2022-04-14 14:55:00");
        mapData.set("time2", map2);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("value", "测评名称-标准能力测评");
        mapData.set("thing3", map3);

        Map<String, Object> map4 = new HashMap<>();
        map4.put("value", 88);
        mapData.set("number4", map4);

        Map<String, String> data = new HashMap<>();
        data.put("name1", "PL");
        data.put("time2", "2025-04-10 16:00:00");
        data.put("thing3", "测试");
        data.put("number4", "88");

        // 发送订阅消息
        //String openId = wechatService.GetOpenId1("0f3R2lll2Gfspf4c7Uml2hLmOS3R2llI");
        String openId2 = wechatUtilService.GetOpenId("0d38dTll2jsBnf4JZyol2D9ybg38dTlH");
        JSONObject jsonObject = new JSONObject(openId2);
        String openId = jsonObject.getStr("openid");
        String templateId = "tmMsJJSh3bVhbCh34x15pTqNwPXIL52mk7r64Z8BnZs";
        String pushMsg = wechatUtilService.PushMsg(templateId,openId,mapData,"pages/teacher/home");


        //String openId = wechatService.GetOpenId("PDLost");
//        String result = wechatHttpUtils.pushMsg(token);
//
//        Map<String, Object> message = new HashMap<>();
//        message.put("name1", openId); // 用户的OpenID，需要从前端获取或者数据库中获取
//        message.put("time2", new Date()); // 完成时间
//        message.put("thing3", "测试"); //测评名称
//        message.put("number4", 50); // 成绩
//        wechatHttpUtils.sendSubscribeMessage0("",message);
        return  pushMsg;
    }

}
