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

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 cn.reghao.autodop.dmaster.util.db.PageList;
import cn.reghao.autodop.dmaster.util.db.PageSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2021-04-04 21:24:18
 */
@Api(tags = "通知页面")
@RequestMapping("/sys")
@Controller
public class NotificationPageController {
    private EmailAccountRepository emailRepository;
    private SmsAccountRepository smsRepository;
    private DingAccountRepository dingRepository;
    private NotifyGroupRepository receiverRepository;

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

    @ApiOperation(value = "邮件通知配置页面")
    @GetMapping("/notify/email")
    public String emailNotifyConfigPage(Model model) {
        PageRequest pageRequest = PageSort.pageRequest();
        Page<EmailAccount> page = emailRepository.findAll(pageRequest);
        PageList<EmailAccount> pageList = PageList.pageList(page);

        model.addAttribute("page", page);
        model.addAttribute("list", pageList.getList());
        return "/sys/notify/email";
    }

    @GetMapping("/notify/email/add")
    public String emailNotifyConfigAddPage(Model model) {
        return "/sys/notify/emailadd";
    }

    @GetMapping("/notify/email/edit/{id}")
    public String emailNotifyConfigEditPage(@PathVariable("id") EmailAccount emailAccount, Model model) {
        model.addAttribute("emailAccount", emailAccount);
        return "/sys/notify/emailadd";
    }

    @ApiOperation(value = "短信通知配置页面")
    @GetMapping("/notify/sms")
    public String smsNotifyConfigPage(Model model) {
        PageRequest pageRequest = PageSort.pageRequest();
        Page<SmsAccount> page = smsRepository.findAll(pageRequest);
        PageList<SmsAccount> pageList = PageList.pageList(page);

        model.addAttribute("page", page);
        model.addAttribute("list", pageList.getList());
        return "/sys/notify/sms";
    }

    @GetMapping("/notify/sms/add")
    public String smsNotifyConfigAddPage(Model model) {
        return "/sys/notify/smsadd";
    }

    @GetMapping("/notify/sms/edit/{id}")
    public String smsNotifyConfigEditPage(@PathVariable("id") SmsAccount smsAccount, Model model) {
        model.addAttribute("smsAccount", smsAccount);
        return "/sys/notify/smsadd";
    }

    @ApiOperation(value = "钉钉通知配置页面")
    @GetMapping("/notify/ding")
    public String dingNotifyConfigPage(Model model) {
        PageRequest pageRequest = PageSort.pageRequest();
        Page<DingAccount> page = dingRepository.findAll(pageRequest);
        PageList<DingAccount> pageList = PageList.pageList(page);

        model.addAttribute("page", page);
        model.addAttribute("list", pageList.getList());
        return "/sys/notify/ding";
    }

    @GetMapping("/notify/ding/add")
    public String dingNotifyConfigAddPage(Model model) {
        return "/sys/notify/dingadd";
    }

    @GetMapping("/notify/ding/edit/{id}")
    public String dingNotifyConfigEditPage(@PathVariable("id") DingAccount dingAccount, Model model) {
        model.addAttribute("dingAccount", dingAccount);
        return "/sys/notify/dingadd";
    }

    @ApiOperation(value = "全局通知接收配置页面")
    @GetMapping("/notify/receiver")
    public String receiverConfigPage(Model model) {
        PageRequest pageRequest = PageSort.pageRequest();
        Page<NotifyGroup> page = receiverRepository.findAll(pageRequest);
        PageList<NotifyGroup> pageList = PageList.pageList(page);

        model.addAttribute("page", page);
        model.addAttribute("list", pageList.getList());
        return "/sys/notify/receiver";
    }

    @GetMapping("/notify/receiver/add")
    public String receiverConfigAddPage(Model model) {
        return "/sys/notify/receiveradd";
    }

    @GetMapping("/notify/receiver/edit/{id}")
    public String receiverConfigEditPage(@PathVariable("id") NotifyGroup notifyGroup, Model model) {
        model.addAttribute("notifyGroup", notifyGroup);
        return "/sys/notify/receiveradd";
    }

    @GetMapping("/notify/receiver/{id}/receivers")
    public String receiversPage(@PathVariable("id") NotifyGroup notifyGroup, Model model) {
        model.addAttribute("list", notifyGroup.getReceivers());
        return "/sys/notify/notifyreceivers";
    }
}
