// comment.js
// Copyright (C) 2021 Digital Telepresence, LLC
// License: Apache-2.0

'use strict';

const express = require('express');
const numeral = require('numeral');

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

class CommentController extends SiteController {

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

  async start ( ) {
    const { dtp } = this;
    const { limiter: limiterService, session: sessionService } = dtp.services;

    const authRequired = sessionService.authCheckMiddleware({ requiredLogin: true });

    const router = express.Router();
    dtp.app.use('/comment', router);

    router.use(async (req, res, next) => {
      res.locals.currentView = module.exports.slug;
      return next();
    });

    router.param('commentId', this.populateCommentId.bind(this));

    router.post('/:commentId/vote', authRequired, this.postVote.bind(this));

    router.get('/:commentId/replies', this.getCommentReplies.bind(this));

    router.delete('/:commentId',
      authRequired,
      limiterService.createMiddleware(limiterService.config.comment.deleteComment),
      this.deleteComment.bind(this),
    );
  }

  async populateCommentId (req, res, next, commentId) {
    const { comment: commentService } = this.dtp.services;
    try {
      res.locals.comment = await commentService.getById(commentId);
      if (!res.locals.comment) {
        return next(new SiteError(404, 'Comment not found'));
      }
      res.locals.post = res.locals.comment.resource;
      return next();
    } catch (error) {
      this.log.error('failed to populate commentId', { commentId, error });
      return next(error);
    }
  }

  async postVote (req, res) {
    const { contentVote: contentVoteService } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('comment-vote');
      const { message, resourceStats } = await contentVoteService.recordVote(req.user, 'Comment', res.locals.comment, req.body.vote);
      displayList.setTextContent(
        `button[data-comment-id="${res.locals.comment._id}"][data-vote="up"] span.dtp-item-value`,
        numeral(resourceStats.upvoteCount).format(resourceStats.upvoteCount > 1000 ? '0,0.0a' : '0,0'),
      );
      displayList.setTextContent(
        `button[data-comment-id="${res.locals.comment._id}"][data-vote="down"] span.dtp-item-value`,
        numeral(resourceStats.downvoteCount).format(resourceStats.upvoteCount > 1000 ? '0,0.0a' : '0,0'),
      );
      displayList.showNotification(message, 'success', 'bottom-center', 3000);
      res.status(200).json({ success: true, displayList });
    } catch (error) {
      this.log.error('failed to process comment vote', { error });
      return res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }

  async getCommentReplies (req, res) {
    const { comment: commentService } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('get-replies');

      if (req.query.buttonId) {
        displayList.removeElement(`li.dtp-load-more[data-button-id="${req.query.buttonId}"]`);
      }

      Object.assign(res.locals, req.app.locals);

      res.locals.countPerPage = parseInt(req.query.cpp || "20", 10);
      if (res.locals.countPerPage < 1) {
        res.locals.countPerPage = 1;
      }
      if (res.locals.countPerPage > 20) {
        res.locals.countPerPage = 20;
      }

      res.locals.pagination = this.getPaginationParameters(req, res.locals.countPerPage);
      res.locals.comments = await commentService.getReplies(res.locals.comment, res.locals.pagination);

      const html = await commentService.renderTemplate('replyList', res.locals);

      const replyList = `ul.dtp-reply-list[data-comment-id="${res.locals.comment._id}"]`;
      displayList.addElement(replyList, 'beforeEnd', html);

      const replyListContainer = `.dtp-reply-list-container[data-comment-id="${res.locals.comment._id}"]`;
      displayList.removeAttribute(replyListContainer, 'hidden');

      if (Array.isArray(res.locals.comments) && (res.locals.comments.length > 0)) {
        displayList.removeElement(`p#empty-comments-label[data-comment-id="${res.locals.comment._id}"]`);
      }

      res.status(200).json({ success: true, displayList });
    } catch (error) {
      this.log.error('failed to display comment replies', { error });
      res.status(error.statusCode || 500).json({ success: false, message: error.message });
    }
  }

  async deleteComment (req, res) {
    const { comment: commentService } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('add-recipient');

      await commentService.remove(res.locals.comment, 'removed');

      let selector = `article[data-comment-id="${res.locals.comment._id}"] .comment-content`;
      displayList.setTextContent(selector, 'Comment removed');

      displayList.showNotification(
        'Comment removed successfully',
        'success',
        'bottom-center',
        5000,
      );

      res.status(200).json({ success: true, displayList });
    } catch (error) {
      this.log.error('failed to remove comment', { error });
      return res.status(error.statusCode || 500).json({
        success: false,
        message: error.message
      });
    }
  }
}

module.exports = {
  slug: 'comment',
  name: 'comment',
  create: async (dtp) => { return new CommentController(dtp); },
};