package com.liquidnet.service.sweet.controller;

import com.liquidnet.commons.lang.util.CurrentUtil;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.sweet.param.SweetOpenSendMsgParam;
import com.liquidnet.service.sweet.service.impl.SweetWechatTemplateServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@Api(tags = "服务号-模版消息")
@Slf4j
@Validated
@RestController
@RequestMapping("/wechatTemplate")
public class SweetWechatTemplateController {

    @Autowired
    private SweetWechatTemplateServiceImpl sweetTemplateService;

    @GetMapping("send")
    @ApiOperation("发送模版消息")
    public ResponseDto send() {
        return sweetTemplateService.sendRemindMsg();
    }

    @PostMapping("openSend")
    @ApiOperation("发送模版消息公共接口")
    public ResponseDto openSend(@Valid @RequestBody SweetOpenSendMsgParam param) {
        return sweetTemplateService.openSendMsg(param);
    }

    @PostMapping("remind")
    @ApiOperation("小程序演出提醒记录")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", dataType = "String", name = "openId", value = "微信openId", required = true),
            @ApiImplicitParam(type = "form", dataType = "String", name = "unionId", value = "微信unionId", required = true),
            @ApiImplicitParam(type = "form", dataType = "String", name = "performancesId", value = "演出ID", required = true),
    })
    public ResponseDto remind(
            @RequestParam("openId") @NotBlank(message = "缺少参数") String openId,
            @RequestParam("unionId") @NotBlank(message = "缺少参数") String unionId,
            @RequestParam("performancesId") @NotBlank(message = "缺少参数") String performancesId
    ) {
        return sweetTemplateService.remind(openId, unionId, performancesId);
    }

    @PostMapping("followStatus")
    @ApiOperation("获取关注状态")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", dataType = "String", name = "unionId", value = "微信unionId", required = true),
            @ApiImplicitParam(type = "form", dataType = "Integer", name = "type", value = "type 1正在 2摩登", required = false),
            @ApiImplicitParam(type = "form", dataType = "String", name = "userId", value = "正在用户ID", required = false),
            @ApiImplicitParam(type = "form", dataType = "String", name = "phone", value = "正在用户手机号", required = false),
    })
    public ResponseDto followStatus(
            @RequestParam() String unionId,
            @RequestParam(defaultValue = "1") Integer type,
            @RequestParam(defaultValue = "") String userId,
            @RequestParam(defaultValue = "") String phone
    ) {
        log.info("followStatus参数 [unionId:{},type:{},userId:{},phone:{}]", unionId, type, userId, phone);
        boolean status = sweetTemplateService.followStatus(unionId, type, userId, phone);
        if (status) {
            return ResponseDto.success(1);
        } else {
            return ResponseDto.success(2);
        }
    }

    @PostMapping("followStatusPhone")
    @ApiOperation("获取关注状态通过手机号")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", dataType = "Integer", name = "type", value = "type 1正在 2摩登", required = true),
    })
    public ResponseDto followStatusPhone(
            @RequestParam() Integer type
    ) {
        String phone = (String) CurrentUtil.getTokenClaims().get(CurrentUtil.TOKEN_MOBILE);
        boolean status = sweetTemplateService.followStatusPhone(type, phone);
        if (status) {
            return ResponseDto.success(1);
        } else {
            // TODO: 2023/12/18 zhanggb 过于依赖微信关注事件推送导致数据不完整，暂不判断关注事件直接通过客户端授权登录的状态继续后续领取会员卡
//            return ResponseDto.success(2);
            log.warn("获取关注状态通过手机号[token={},mobile={},type={}]", CurrentUtil.getToken(), phone, type);
            return ResponseDto.success(1);
        }
    }

}
