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

'use strict';

const express = require('express');

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

class SiteLinkAdminController 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 = 'site-link';
      return next();
    });

    router.param('linkId', this.populateLinkId.bind(this));

    router.post('/:linkId', this.postUpdateLink.bind(this));
    router.post('/', this.postCreateLink.bind(this));

    router.get('/create', this.getLinkEditor.bind(this));
    router.get('/:linkId', this.getLinkEditor.bind(this));

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

    router.delete('/:linkId', this.deleteLink.bind(this));

    return router;
  }

  async populateLinkId (req, res, next, linkId) {
    const { siteLink: siteLinkService } = this.dtp.services;
    try {
      res.locals.link = await siteLinkService.getById(linkId);
      return next();
    } catch (error) {
      this.log.error('failed to populate site link', { linkId, error });
      return next(error);
    }
  }

  async postUpdateLink (req, res, next) {
    const { siteLink: siteLinkService } = this.dtp.services;
    try {
      await siteLinkService.update(res.locals.link, req.body);
      res.redirect('/admin/site-link');
    } catch (error) {
      this.log.error('failed to update site link', { error });
      return next(error);
    }
  }

  async postCreateLink (req, res, next) {
    const { siteLink: siteLinkService } = this.dtp.services;
    try {
      await siteLinkService.create(req.body);
      res.redirect('/admin/site-link');
    } catch (error) {
      this.log.error('failed to create site link', { error });
      return next(error);
    }
  }

  async getLinkEditor (req, res) {
    res.render('admin/site-link/editor');
  }

  async getHomeView (req, res, next) {
    const { siteLink: siteLinkService } = this.dtp.services;
    try {
      res.locals.links = await siteLinkService.getLinks();
      res.render('admin/site-link/index');
    } catch (error) {
      this.log.error('failed to present the Site Link Admin home view', { error });
      return next(error);
    }
  }

  async deleteLink (req, res) {
    const { siteLink: siteLinkService } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('delete-site-link');
      await siteLinkService.remove(res.locals.link);
      displayList.navigateTo('/admin/site-link');
      res.status(200).json({ success: true, displayList });
    } catch (error) {
      this.log.error('failed to delete site link', { error });
      res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }
}

module.exports = {
  name: 'adminSiteLink',
  slug: 'admin-site-link',
  create: async (dtp) => { return new SiteLinkAdminController(dtp); },
};