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

'use strict';

const express = require('express');

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

class SettingsController extends SiteController {

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

  async start ( ) {
    const router = express.Router();

    const imageUpload = this.createMulter('uploads', {
      limits: {
        fileSize: 1024 * 1000 * 12,
      },
    });

    router.use(async (req, res, next) => {
      res.locals.currentView = 'admin';
      res.locals.adminView = 'settings';
      return next();
    });

    router.post('/', this.postUpdateSettings.bind(this));
    
    router.post('/images/updateSiteIcon', imageUpload.single('imageFile'), this.postUpdateSiteIcon.bind(this));
    
    router.post('/images/updatePostImage', imageUpload.single('imageFile'), this.postUpdatePostImage.bind(this));

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

    router.get('/images', this.getImageSettings.bind(this));
    
    return router;
  }

  async postUpdateSettings (req, res, next) {
    const { cache: cacheService } = this.dtp.services;
    try {
      const settingsKey = `settings:${this.dtp.config.site.domainKey}:site`;
      await cacheService.setObject(settingsKey, req.body);
      res.redirect('/admin/settings');
    } catch (error) {
      return next(error);
    }
  }

  async getSettingsView (req, res, next) {
    try {
      res.render('admin/settings/editor');
    } catch (error) {
      return next(error);
    }
  }

  async getImageSettings (req, res, next) {
    const { image: imageService } = this.dtp.services;
    res.locals.adminView = 'image-settings';
    res.locals.pageTitle = `Image settings for ${this.dtp.config.site.name}`;
    try {
      res.locals.siteIcon = await imageService.getSiteIconInfo();
      res.locals.postImage = await imageService.getPostImageInfo();
      res.render('admin/settings/images');
    } catch (error) {
      return next(error);
    }
  }

  async postUpdateSiteIcon (req, res) {
    const { image: imageService } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('site-icon');
      await imageService.updateSiteIcon(req.body, req.file);
      displayList.showNotification(
        'Site Icon updated successfully.',
        'success',
        'bottom-center',
        2000,
      );
      res.status(200).json({
        success: true,
        displayList,
      });
    } catch (error) {
      this.log.error('failed to update site icon', { error });
      return res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }

  async postUpdatePostImage (req, res) {
    const { image: imageService } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('site-post-image');
      await imageService.updatePostImage(req.body, req.file);
      displayList.showNotification(
        'Post Image updated successfully.',
        'success',
        'bottom-center',
        2000,
      );
      res.status(200).json({
        success: true,
        displayList,
      });
    } catch (error) {
      this.log.error('failed to update site icon', { error });
      return res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }

}

module.exports = {
  name: 'adminSettings',
  slug: 'admin-settings',
  create: async (dtp) => { return new SettingsController(dtp); },
};