package cn.reghao.autodop.dmaster.notification.controller;

import cn.reghao.jdkutil.result.WebBody;
import cn.reghao.autodop.dmaster.notification.model.po.DingAccount;
import cn.reghao.autodop.dmaster.notification.model.po.EmailAccount;
import cn.reghao.autodop.dmaster.notification.model.po.NotifyGroup;
import cn.reghao.autodop.dmaster.notification.model.po.SmsAccount;
import cn.reghao.autodop.dmaster.notification.db.repository.DingAccountRepository;
import cn.reghao.autodop.dmaster.notification.db.repository.NotifyGroupRepository;
import cn.reghao.autodop.dmaster.notification.db.repository.EmailAccountRepository;
import cn.reghao.autodop.dmaster.notification.db.repository.SmsAccountRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author reghao
 * @date 2021-04-04 21:24:18
 */
@Api(tags = "通知接口")
@RequestMapping("/api/sys/notify")
@RestController
public class NotificationController {
    private EmailAccountRepository emailRepository;
    private SmsAccountRepository smsRepository;
    private DingAccountRepository dingRepository;
    private NotifyGroupRepository receiverRepository;

    public NotificationController(EmailAccountRepository emailRepository,
                                  SmsAccountRepository smsRepository,
                                  DingAccountRepository dingRepository,
                                  NotifyGroupRepository receiverRepository) {
        this.emailRepository = emailRepository;
        this.smsRepository = smsRepository;
        this.dingRepository = dingRepository;
        this.receiverRepository = receiverRepository;
    }

    @ApiOperation(value = "添加/修改邮件通知账户")
    @PostMapping(value = "/email", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> addEmailAccount(@Validated EmailAccount emailAccount) {
        emailRepository.save(emailAccount);
        return ResponseEntity.ok().body(WebBody.success());
    }

    @ApiOperation(value = "删除邮件通知账户")
    @DeleteMapping(value = "/email", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> deleteEmailAccount(@Validated EmailAccount emailAccount) {
        return ResponseEntity.ok().body(WebBody.success());
    }

    @ApiOperation(value = "添加/修改短信通知账户")
    @PostMapping(value = "/sms", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> addSmsAccount(@Validated SmsAccount smsAccount) {
        smsRepository.save(smsAccount);
        return ResponseEntity.ok().body(WebBody.success());
    }

    @ApiOperation(value = "删除短信通知账户")
    @DeleteMapping(value = "/sms", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> deleteSmsAccount(@Validated SmsAccount smsAccount) {
        return ResponseEntity.ok().body(WebBody.success());
    }

    @ApiOperation(value = "添加/修改钉钉通知账户")
    @PostMapping(value = "/ding", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> addDingAccount(@Validated DingAccount dingAccount) {
        dingRepository.save(dingAccount);
        return ResponseEntity.ok().body(WebBody.success());
    }

    @ApiOperation(value = "删除钉钉通知账户")
    @DeleteMapping(value = "/ding", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> deleteDingAccount(@Validated DingAccount dingAccount) {
        return ResponseEntity.ok().body(WebBody.success());
    }

    @ApiOperation(value = "添加/修改全局通知接收")
    @PostMapping(value = "/receiver", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> addNotifyReceiver(@Validated NotifyGroup notifyReceiver) {
        // TODO 检查通知类型和通知账户是否存在
        receiverRepository.save(notifyReceiver);
        return ResponseEntity.ok().body(WebBody.success());
    }

    @ApiOperation(value = "删除全局通知接收")
    @DeleteMapping(value = "/receiver", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> deleteNotifyReceiver(@Validated NotifyGroup notifyReceiver) {
        return ResponseEntity.ok().body(WebBody.success());
    }
}
