package com.bizzan.bitrade.controller.authno;

import com.bizzan.bitrade.entity.Member;
import com.bizzan.bitrade.service.MemberService;
import com.bizzan.bitrade.service.SysConfigService;
import com.bizzan.bitrade.util.MessageResult;
import com.bizzan.bitrade.vendor.provider.support.EmailProvider;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.TemplateException;

@Slf4j
@RestController
@RequestMapping("noauth/experience-gift")
public class ExperienceGiftNotifyController {
    private static final String EXPERIENCE_GIFT_GRANT_TEMPLATE = "experienceGiftGrant.ftl";

    @Autowired
    private MemberService memberService;

    @Autowired
    private EmailProvider emailProvider;

    @Autowired
    private SysConfigService sysConfigService;

    @PostMapping("grant-email")
    public MessageResult sendGrantEmail(@RequestBody GrantEmailReq req) throws TemplateException, IOException {
        if (req == null || req.getMemberId() == null) {
            return MessageResult.error("memberId 不能为空");
        }
        Member member = memberService.findOne(req.getMemberId());
        if (member == null) {
            return MessageResult.error("用户不存在");
        }
        if (StringUtils.isBlank(member.getEmail())) {
            return MessageResult.error("用户未绑定邮箱");
        }

        String amountText = req.getAmount() == null ? "0" : req.getAmount().stripTrailingZeros().toPlainString();
        String subject = "【CionVision】体验金发放通知 | " + amountText + "U 体验金已入账";
        String html = renderGrantEmailHtml(amountText);
        emailProvider.sendSingleSendMail(member.getEmail(), subject, html);

        Map<String, Object> data = new HashMap<>();
        data.put("memberId", req.getMemberId());
        data.put("email", member.getEmail());
        data.put("amount", req.getAmount());
        return MessageResult.success("发送成功", data);
    }

    public static class GrantEmailReq {
        private Long memberId;
        private BigDecimal amount;

        public Long getMemberId() {
            return memberId;
        }

        public void setMemberId(Long memberId) {
            this.memberId = memberId;
        }

        public BigDecimal getAmount() {
            return amount;
        }

        public void setAmount(BigDecimal amount) {
            this.amount = amount;
        }
    }

    private String renderGrantEmailHtml(String amountText) throws IOException, TemplateException {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_26);
        cfg.setClassForTemplateLoading(this.getClass(), "/templates");
        cfg.setDefaultEncoding("UTF-8");
        Template template = cfg.getTemplate(EXPERIENCE_GIFT_GRANT_TEMPLATE, "UTF-8");
        Map<String, Object> model = new HashMap<>(4);
        model.put("amountText", amountText);
        model.put("projectName", sysConfigService.get().getProjectName());
        model.put("projectLogo", sysConfigService.get().getProjectLogo());
        return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
    }
}
