// admin/announcement.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0

'use strict';

const express = require('express');

const { SiteController } = require('../../../lib/site-lib');

class AnnouncementAdminController extends SiteController {

  constructor (dtp) {
    super(dtp, module.exports);
  }

  async start ( ) {
    const router = express.Router();
    router.use(async (req, res, next) => {
      res.locals.currentView = 'admin';
      res.locals.adminView = 'announcement';
      return next();
    });

    router.param('announcementId', this.populateAnnouncementId.bind(this));

    router.post('/:announcementId', this.postUpdateAnnouncement.bind(this));
    router.post('/', this.postCreateAnnouncement.bind(this));

    router.get('/create', this.getAnnouncementEditor.bind(this));
    router.get('/:announcementId', this.getAnnouncementEditor.bind(this));

    router.get('/', this.getHomeView.bind(this));

    router.delete('/:announcementId', this.deleteAnnouncement.bind(this));

    return router;
  }

  async populateAnnouncementId (req, res, next, announcementId) {
    const { announcement: announcementService } = this.dtp.services;
    try {
      res.locals.announcement = await announcementService.getById(announcementId);
      return next();
    } catch (error) {
      return next(error);
    }
  }

  async postUpdateAnnouncement (req, res, next) {
    const { announcement: announcementService } = this.dtp.services;
    try {
      await announcementService.update(res.locals.announcement, req.body);
      res.redirect('/admin/announcement');
    } catch (error) {
      return next(error);
    }
  }

  async postCreateAnnouncement (req, res, next) {
    const { announcement: announcementService } = this.dtp.services;
    try {
      await announcementService.create(req.body);
      res.redirect('/admin/announcement');
    } catch (error) {
      return next(error);
    }
  }

  async getAnnouncementEditor (req, res) {
    res.render('admin/announcement/editor');
  }

  async getHomeView (req, res, next) {
    const { announcement: announcementService } = this.dtp.services;
    try {
      res.locals.pagination = this.getPaginationParameters(req, 20);
      res.locals.announcements = await announcementService.getAnnouncements(res.locals.pagination);
      res.render('admin/announcement/index');
    } catch (error) {
      return next(error);
    }
  }

  async deleteAnnouncement (req, res) {
    const { announcement: announcementService } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('delete-announcement');
      await announcementService.remove(res.locals.announcement);
      displayList.reload();
      res.status(200).json({ success: true, displayList });
    } catch (error) {
      this.log.error('failed to delete announcement', { error });
      res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }
}

module.exports = {
  name: 'announcement',
  slug: 'announcement',
  create: async (dtp) => {
    let controller = new AnnouncementAdminController(dtp);
    return controller;
  },
};