package com.uas.platform.b2b.manage.support;

import com.alibaba.fastjson.JSONObject;
import com.uas.message.common.domain.MapMessage;
import com.uas.message.common.domain.Page;
import com.uas.message.mail.domain.MailLog;
import com.uas.message.mail.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 基于RestTemplate实现邮件发送
 *
 * @author hejq
 * @date 2018-07-04
 */
@Service
public class RestMailServiceImpl implements MailService {

    private RestTemplate restTemplate;

    /**
     * 邮件服务主机地址
     */
    private static String MAIL_CONSOLE_HOST = "http://message.ubtob.com/";

    /**
     * 发送邮件给单个人url
     */
    private String MAIL_SEND_URL = "mail/send";

    /**
     * 发送邮件给多个人 url
     */
    private String MAIL_SEND_MANY_URL = "mail/send/o2m";

    @Autowired
    public RestMailServiceImpl(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Override
    public List<Map<String, Object>> sendByDefault(String[] strings, String s, String s1) {
        // 暂未使用
        return null;
    }

    @Override
    public MailLog send(MapMessage mapMessage) {
        // 暂未使用
        return null;
    }

    @Override
    public List<MailLog> sendAll(MapMessage mapMessage) {
        // 暂未使用
        return null;
    }

    /**
     * 发送邮件给个人
     * @param templateId
     * @param receiver
     * @param params
     * @return
     */
    @Override
    public MailLog send(String templateId, String receiver, Map<String, Object> params) {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        JSONObject object = new JSONObject();
        object.put("templateId", templateId);
        object.put("receiver", receiver);
        object.put("params", params);

        HttpEntity<String> formEntity = new HttpEntity<String>(object.toJSONString(), headers);
        ResponseEntity<MailLog> responseEntity = restTemplate.postForEntity(MAIL_CONSOLE_HOST + MAIL_SEND_URL, formEntity, MailLog.class);
        return responseEntity.getBody();
    }

    /**
     * 发送邮件给多个人
     * @param templateId
     * @param receivers
     * @param params
     * @return
     */
    @Override
    public List<MailLog> sendAll(String templateId, Set<String> receivers, Map<String, Object> params) {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        JSONObject object = new JSONObject();
        object.put("templateId", templateId);
        object.put("receivers", receivers);
        object.put("params", params);

        HttpEntity<String> formEntity = new HttpEntity<String>(object.toJSONString(), headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(MAIL_CONSOLE_HOST + MAIL_SEND_MANY_URL, formEntity, String.class);
        List<MailLog> mailLogs = JSONObject.parseArray(responseEntity.getBody(), MailLog.class);
        return mailLogs;
    }

    @Override
    public List<MailLog> sendAll(List<MapMessage> list) {
        // 暂未使用
        return null;
    }

    @Override
    public Page<MailLog> findLogs(int i, int i1) {
        // 暂未使用
        return null;
    }

    @Override
    public Page<MailLog> findLogs(String s, int i, int i1) {
        // 暂未使用
        return null;
    }

    @Override
    public Page<MailLog> findLogs(boolean b, int i, int i1) {
        // 暂未使用
        return null;
    }

    @Override
    public Page<MailLog> findLogs(String s, String s1, Boolean aBoolean, int i, int i1) {
        // 暂未使用
        return null;
    }

    @Override
    public void deleteLogs(String s) {
        // 暂未使用
    }
}
