'use strict';

var express = require('express');
var appRouter = express.Router();
var controllers = require('../controllers');
var sharedUrlRoutes = require('../../shared/SharedUrlRoutes');
const { upload } = require('../file/FileSettings');

var buildUrl = function(url) {
    return '/' + url;
};

module.exports = function(app) {
    /**
     * add your routes here
     * for example:
     *    appRouter.get('/get', controllers.YourController.findAll());
     */

    // Authentication routes
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.login), controllers.AuthenticationController.login);
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.loginSaml), controllers.AuthenticationController.loginSaml);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loginGoogle), controllers.AuthenticationController.loginGoogle);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loginGoogleCallback),
        controllers.AuthenticationController.loginGoogleCallback);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loginFacebook), controllers.AuthenticationController.loginFacebook);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loginFacebookCallback),
            controllers.AuthenticationController.loginFacebookCallback);
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.logout), controllers.AuthenticationController.logout);
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.logoutAllDevices), controllers.AuthenticationController.logoutAllDevices);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loggedUser), controllers.AuthenticationController.getLoggedUser);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loggedUserData), controllers.AuthenticationController.getLoggedUserData);

    const getLoggedAccControlDocumentController = controllers.AuthenticationController.getLoggedUserAccountControlDocument;
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loggedUserAccountControlDocument), getLoggedAccControlDocumentController);

    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.testPassword), controllers.AuthenticationController.testPassword);

    appRouter.post(sharedUrlRoutes.serverRoutes.publicLogin, controllers.AuthenticationController.publicLogin);

    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.appActivateAccountAction),
        controllers.AuthenticationController.doActivateAccount);
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.appForgotPassAction),
        controllers.AuthenticationController.doSendResetLink);

    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.appResetpassAction), controllers.AuthenticationController.doResetPassword);

    // End Authentication routes

    // route for testing the session from a client
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.sessionTest), controllers.SessionTestController.testSession);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.healthCheck), controllers.HealthCheckController.healthCheck);

    // get current server time
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.serverTime), controllers.TimeController.getServerTime);

    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.packageImport), controllers.PackageController.packageImport);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.packageExport), controllers.PackageController.packageExport);
    appRouter.delete(buildUrl(sharedUrlRoutes.serverRoutes.packageCleanUp), controllers.PackageController.packageCleanUp);

    // Routes for file upload
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.uploadFiles),
        upload.single("file"), controllers.UploadFileController.uploadFiles);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.deleteFile), controllers.UploadFileController.deleteFile);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.getFile), controllers.UploadFileController.getFile);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.getFileWithName), controllers.UploadFileController.getFile);
    // End Routes for file upload

    var cache = require('../utils/UtilHttp').nocache;

    //Document
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.documentExistsFull), cache, controllers.DocumentController.documentExists);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.documentExistsIncludeDeleted), cache, 
        controllers.DocumentController.documentExists);

    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.syncDocuments), cache, controllers.DocumentController.syncDocuments);

    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.syncRecordsByServerUpdatedDateWithPagination), cache,
        controllers.DocumentController.syncDocuments);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.syncDocumentsCountByDateRoute), cache,
        controllers.DocumentController.getSyncDocumentCount);

    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.documentSearch), cache, controllers.DocumentController.search);
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.documentSearch), cache, controllers.DocumentController.searchPost);
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.searchExport), cache,
        controllers.SearchExportController.searchExport);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.generatePit), cache, controllers.DocumentController.generatePit);

    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loadIncludeDocs), cache, controllers.DocumentController.getIncludeDocs);
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.loadIncludeDocs), cache,
      controllers.DocumentController.getIncludeDocsPost);

    // TODO: Deprecated, remove this if no longer used
    // ****************
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.document), cache, controllers.DocumentController.findAll);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.documentFind), cache, controllers.DocumentController.findAll);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.documentsUrl), cache, controllers.DocumentController.findByQuery);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.documentSingle), cache, controllers.DocumentController.find);
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.documentSingle), cache, controllers.DocumentController.findPost);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loadDocsForRulesUrl), cache, controllers.DocumentController.findForRules);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.documentFetch), cache, controllers.DocumentController.findAll);
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.documentFetch), cache, controllers.DocumentController.findAllPost);
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.versionFetch), cache, controllers.DocumentController.findVersions);
    // ****************

    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.templateAndDocumentId), cache, 
        controllers.DocumentController.templateAndDocument);
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.templateAndDocumentWithTemplateId), cache, 
        controllers.DocumentController.templateAndDocument);

    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.documentSingle), cache, controllers.DocumentController.find);
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.documentSingle), cache, controllers.DocumentController.findPost);

    // POST /api/document
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.document), controllers.DocumentController.insert);
    // POST /api/document/:userId - userId parameter will be ignored because the logged in user will always be used
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.documentWithUser), controllers.DocumentController.insert);

    //  PUT /api/document/:id
    appRouter.put(buildUrl(sharedUrlRoutes.serverRoutes.documentSingle), cache, controllers.DocumentController.update);
    // PUT /api/documentWithUser/:id/:userId - userId parameter will be ignored because the logged in user will always be used
    appRouter.put(buildUrl(sharedUrlRoutes.serverRoutes.documentSingleWithUser), cache, controllers.DocumentController.update);

    //  PUT /api/document/deepDiffUpdate/:id/:template
    appRouter.put(buildUrl(sharedUrlRoutes.serverRoutes.documentSingleOverrideTpl), cache,
        controllers.DocumentController.deepDiffUpdate);
    //  PUT /api/document/deepDiffUpdate/:id
    appRouter.put(buildUrl(sharedUrlRoutes.serverRoutes.documentDeepDiffUpdateSingle), cache,
        controllers.DocumentController.deepDiffUpdate);
    //  PUT /api/document/deepDiffUpdateWithUser/:id/:userId -
    //  userId parameter will be ignored because the logged in user will always be used
    appRouter.put(buildUrl(sharedUrlRoutes.serverRoutes.documentDeepDiffUpdateSingleWithUser), cache,
        controllers.DocumentController.deepDiffUpdate);

    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.documentAudit), cache, controllers.DocumentController.audit);
    appRouter.delete(buildUrl(sharedUrlRoutes.serverRoutes.documentDestroy), cache, controllers.DocumentController.destroy);
    appRouter.delete(buildUrl(sharedUrlRoutes.serverRoutes.documentDestroyWithTemplateIdAndPreviousVersionId),
        cache, controllers.DocumentController.destroy);
    appRouter.put(buildUrl(sharedUrlRoutes.serverRoutes.undeleteDocument), cache, controllers.DocumentController.undestroy);

    // bulk upload documents
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.bulkUploadDocuments), controllers.DocumentController.bulkUploadDocuments);

    //load configuration from database
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loadClientConfig), controllers.ConfigController.loadClientConfig);
    // set the base path based on the config. This allows the app to exist under a different path. Eg. setting urlBasePath to
    // test means the app will be under <HOST_NAME>/test instead of just <HOST_NAME>/

    //custom component functions
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.executeFunction),
        controllers.ComponentFunctionController.executeFunction);
    appRouter.post(buildUrl(sharedUrlRoutes.serverRoutes.executeFunctionWS),
        controllers.ComponentFunctionController.executeFunction);

    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loadComponentDoc),
        controllers.ComponentLoadController.loadComponentDoc);

    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.clientResources), controllers.ClientResourceController.getClientResources);

    // vendor libraries
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loadVendorLibrary),
        controllers.ComponentLoadController.loadVendorLibrary);

    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loadVendorLibraries),
        controllers.ComponentLoadController.loadVendorLibraries);

    const getLoggedAccGroupConfigDocumentController = controllers.AuthenticationController.getLoggedUserAccountGroupConfigDocument;
    appRouter.get(buildUrl(sharedUrlRoutes.serverRoutes.loggedUserAccountConfigDocument),getLoggedAccGroupConfigDocumentController);

    // put the routes into express
    var basePath = require('./BaseURLPath').basePathWithSlashPrefix;
    app.use(basePath, appRouter);
};
