package com.gic.haoban.manage.service.service.out.impl;

import com.alibaba.fastjson.JSON;
import com.gic.commons.util.StringUtil;
import com.gic.haoban.manage.api.response.SendSmsResponse;
import com.gic.haoban.manage.api.service.ValidationCodeApiService;
import com.gic.marketing.api.dto.SmsSendResponseDTO;
import com.gic.marketing.api.service.AccountOverdueSmsSendService;
import com.gic.thirdparty.api.constant.YTXErrCode;
import com.gic.thirdparty.api.dto.CommunicationLogDTO;
import com.gic.thirdparty.api.dto.TelResDTO;
import com.gic.thirdparty.api.service.CommunicationService;
import com.gic.thirdparty.api.service.VoiceService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/22.
 */
@Service
public class ValidationCodeApiServiceImpl implements ValidationCodeApiService {

    private Logger logger = LoggerFactory.getLogger(ValidationCodeApiServiceImpl.class);

    @Autowired
    private VoiceService voiceService;

    @Autowired
    private CommunicationService communicationService;

    @Autowired
    private AccountOverdueSmsSendService accountOverdueSmsSendService;

    private static final String SUCCESS = "0";


    /**
     * 短信验证码
     *
     * @param nationCode
     * @param phoneNumber
     * @param code
     */
    @Override
    public SendSmsResponse sendValidationCode(String nationCode, String phoneNumber, String code) {
        SendSmsResponse response = new SendSmsResponse();
        logger.info("短信验证码:nationCode：{} phoneNumber:{} 验证码：{} ", nationCode, phoneNumber, code);
        if (StringUtils.isBlank(phoneNumber)) {
            response.setSuccess(false);
            response.setMessage("手机号码不能空");
        }

        Map<String, String> map = new HashMap<>();
        map.put("1", code);
        map.put("2", "15");
        SmsSendResponseDTO responseDTO = accountOverdueSmsSendService.smsSend("-1", phoneNumber, nationCode, 2, map);


        if (SUCCESS.equals(responseDTO.getErrCode())) {
            logger.info("给phoneNumber：{} 发送短信成功！", phoneNumber);
            response.setSuccess(true);
        } else {
            logger.info("手机号码：" + phoneNumber + "短信验证码发送失败：" + responseDTO.getErrMessage());
        }

        response.setMessage(responseDTO.getErrMessage());
        return response;
    }


    /**
     * 短信验证码
     *
     * @param nationCode
     * @param phoneNumber
     * @param code
     */
//    @Override
//    public int smsValidationCode(String nationCode, String phoneNumber, String code) {
//        logger.info("短信验证码:nationCode：{} phoneNumber:{} 验证码：{} ", nationCode, phoneNumber, code);
//        if (StringUtils.isBlank(phoneNumber)) {
//            return 3;
//        }
//
//        Map<String, String> map = new HashMap<String, String>();
//        map.put("1", code);
//        map.put("2", "15");
//        SmsSendResponseDTO responseDTO = accountOverdueSmsSendService.smsSend("-1", phoneNumber, nationCode, 2, map);
//        if (SUCCESS.equals(responseDTO.getErrCode())) {
//            logger.info("给phoneNumber：{} 发送短信成功！", phoneNumber);
//            return 1;
//        } else {
//            logger.info("手机号码：" + phoneNumber + "短信验证码发送失败：" + responseDTO.getErrMessage());
//            return 2;
//        }
//    }

    /**
     * 短信验证码
     *
     * @param nationCode
     * @param phoneNumber
     */
//    @Override
//    public int smsMessageNoReadRemind(String nationCode, String phoneNumber, String unRendPeople, String unReadCount, String enterpriseId) {
//        logger.info("短信验证码:nationCode：{} phoneNumber:{} 未读人数：{},未读数:{} ", nationCode, phoneNumber, unRendPeople, unReadCount);
//        if (StringUtils.isBlank(phoneNumber)) {
//            return 3;
//        }
//
//        Map<String, String> map = new HashMap<String, String>();
//        map.put("1", unRendPeople);
//        map.put("2", unReadCount);
//        SmsSendResponseDTO responseDTO = accountOverdueSmsSendService.smsSend(enterpriseId, phoneNumber, nationCode, 3, map);
//        if (SUCCESS.equals(responseDTO.getErrCode())) {
//            logger.info("给phoneNumber：{} 发送短信成功！", phoneNumber);
//            return 1;
//        } else {
//            logger.info("手机号码：" + phoneNumber + "短信提醒发送失败：" + responseDTO.getErrMessage());
//            return 2;
//        }
//    }

//    @Override
//    public int smsValidationCode(String phoneNumber, String code) {
//        logger.info("短信验证码:phoneNumber:" + phoneNumber + "code:" + code);
//        if (StringUtil.isNotNullStr(phoneNumber, true)) {
//
//            Map<String, String> map = new HashMap<String, String>();
//            map.put("1", code);
//            map.put("2", "15");
//            SmsSendResponseDTO responseDTO = accountOverdueSmsSendService.smsSend("-1", phoneNumber, "86", 2, map);
//            if (responseDTO.getErrCode().equals("0")) {
//                logger.info("发送成功！" + phoneNumber);
//                return 1;
//            } else {
//
//                logger.info("手机号码：" + phoneNumber + "短信验证码发送失败：" + responseDTO.getErrMessage());
//                return 2;
//            }
//        }
//        logger.info("手机号吗为空" + phoneNumber);
//        return 3;
//    }

//    @Override
//    public int verifyValidationCode(String phoneNumber, String code) {
//        TelResDTO res = voiceService.voiceVerify(phoneNumber, code, 2, null);
//        if (null != res && YTXErrCode.ERR_000000 == res.getCode()) {
//            logger.info("成功+" + phoneNumber);
//
//        } else {
//            logger.info("发送失败" + phoneNumber);
//            return 0;
//        }
//
//        // 保存通讯记录
//        CommunicationLogDTO communicationLog = null;
//        try {
//            communicationLog = new CommunicationLogDTO();
//            communicationLog.setCreateTime(new Date());
//            communicationLog.setOperationType(1);
//            communicationLog.setParamInfo(code);
//            communicationLog.setReceivePhone(phoneNumber);
//            communicationLog.setStatus(1);
//            if (null != res) {
//                YTXErrCode errcode = res.getCode();
//                if (!errcode.getCode().equals(
//                        YTXErrCode.ERR_000000.getCode())) {
//                    communicationLog.setStatus(0);
//                    communicationLog.setErrMsg(errcode.getValue());
//                    communicationLog.setFee(0);
//                }
//            } else {
//                communicationLog.setStatus(0);
//                communicationLog.setErrMsg("无返回结果");
//                communicationLog.setFee(0);
//            }
//            communicationService.saveCommunication(communicationLog,
//                    "voice");
//        } catch (Exception e) {
//            logger.info("调用服务模块异常，保存通讯日志记录出错:"
//                    + JSON.toJSONString(communicationLog));
//        }
//        return 1;
//    }

    @Override
    public SendSmsResponse sendValidationCodeHb3(String nationCode, String phoneNumber, String code, String enterpriseId, String storeId, String clerkId, boolean payFlag) {
        SendSmsResponse response = new SendSmsResponse();
        logger.info("短信验证码:nationCode：{} phoneNumber:{} 验证码：{} ", nationCode, phoneNumber, code);
        if (StringUtils.isBlank(phoneNumber)) {
            response.setSuccess(false);
            response.setMessage("手机号码不能空");
        }

        Map<String, String> map = new HashMap<>();
        map.put("1", code);
        map.put("2", "15");
        if (StringUtils.isNotBlank(storeId) && StringUtils.isNotBlank(clerkId)) {
            map.put("clerkId", clerkId);
            map.put("storeId", storeId);
        }
        if (!payFlag) {
            map.put("needPay", "0");
        } else {
            map.put("needPay", "1");
        }
        SmsSendResponseDTO responseDTO = accountOverdueSmsSendService.smsSend(enterpriseId, phoneNumber, nationCode, 2, map);

        if (SUCCESS.equals(responseDTO.getErrCode())) {
            logger.info("给phoneNumber：{} 发送短信成功！", phoneNumber);
            response.setSuccess(true);
        } else {
            logger.info("手机号码：" + phoneNumber + "短信验证码发送失败：" + responseDTO.getErrMessage());
        }

        response.setMessage(responseDTO.getErrMessage());
        return response;
    }
}
