package com.nokia.sms.controller;

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 com.nokia.common.http.R;
import com.nokia.sms.pojo.Sms;
import com.nokia.sms.service.SgipSmsService;
import com.nokia.sms.service.UserService;
import com.nokia.sms.vo.MultiNumberSms;
import com.nokia.sms.vo.SingleNumberSms;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/sms")
public class SmsController {

    private final SgipSmsService smsService;
    private final UserService userService;

    public SmsController(SgipSmsService sgipSmsService, UserService userService) {
        this.smsService = sgipSmsService;
        this.userService = userService;
    }

    @PostMapping("sendByPhoneNumber")
    public R sendByPhoneNumber(@RequestBody SingleNumberSms singleNumberSms) {
        log.debug("sendByPhoneNumber收到入参: {}", singleNumberSms);
        if ((null == singleNumberSms.getPhoneNumber()) || ("".equals(singleNumberSms.getPhoneNumber()))) {
            return R.error().message("phoneNumber不能为空");
        }
        Sms sms = new Sms(singleNumberSms);
        return send(sms);
    }

    @PostMapping("sendByPhoneNumbers")
    public R sendByPhoneNumbers(@RequestBody MultiNumberSms multiNumberSms) {
        log.debug("sendByPhoneNumbers收到入参: {}", multiNumberSms);
        if ((null == multiNumberSms.getPhoneNumbers()) || (multiNumberSms.getPhoneNumbers().length == 0)) {
            return R.error().message("phoneNumbers不能为空");
        }
        Sms sms = new Sms(multiNumberSms);
        return send(sms);
    }

    @PostMapping("sendByLoginName")
    public R sendByLoginName(@RequestBody SingleNumberSms singleNumberSms) {
        log.debug("sendByLoginName收到入参: {}", singleNumberSms);
        if ((null == singleNumberSms.getLoginName()) || ("".equals(singleNumberSms.getLoginName()))) {
            return R.error().message("loginName不能为空");
        }
        String phoneNumber = userService.getPhoneNumberByLoginName(singleNumberSms.getLoginName());
        if ((null == phoneNumber) || ("".equals(phoneNumber))) {
            return R.error().message("用户 " + singleNumberSms.getLoginName() + " 未预留手机号码...");
        }
        Sms sms = new Sms(singleNumberSms);
        sms.setPhoneNumbers(new String[] { phoneNumber });
        return send(sms);
    }

    @PostMapping("sendByLoginNames")
    public R sendByLoginNames(@RequestBody MultiNumberSms multiNumberSms) {
        log.debug("sendByLoginName收到入参: {}", multiNumberSms);
        if ((null == multiNumberSms.getLoginNames()) || (multiNumberSms.getLoginNames().length == 0)) {
            return R.error().message("loginName不能为空");
        }
        String[] phoneNumbers = new String[multiNumberSms.getLoginNames().length];
        for (int i = 0; i < phoneNumbers.length; i++) {
            String phoneNumber = userService.getPhoneNumberByLoginName(multiNumberSms.getLoginNames()[i]);
            if ((null == phoneNumber) || ("".equals(phoneNumber))) {
                return R.error().message("用户 " + multiNumberSms.getLoginNames()[i] + " 未预留手机号码...");
            }
            phoneNumbers[i] = phoneNumber;
        }
        Sms sms = new Sms(multiNumberSms);
        sms.setPhoneNumbers(phoneNumbers);
        return send(sms);
    }

    private R send(Sms sms) {
        String result = smsService.sendMessage(sms);
        log.debug("发送结果----{}", result);
        if ("成功".equals(result)) {
            return R.ok();
        } else {
            return R.error().message(sms.getStatus());
        }
    }
}
