/* eslint-disable vars-on-top */
/* eslint-disable no-var */
const express = require('express');

const router = express.Router();

const mountRegisterRoutes = require('../features/register/routes');
const mountLoginRoutes = require('../features/login/routes');
const mountLogoutRoutes = require('../features/logout/routes');
const mountResetPasswordRoutes = require('../features/reset-password/routes');
const mountProfileRoutes = require('../features/profile/routes');

const companyApi = require('../api/company');
const materialApi = require('../api/material');
const divisionApi = require('../api/division');
const userApi = require('../api/users');
const editApi = require('../api/editable');
const clearApi = require('../api/auto-clear');
const summaryApi =  require('../api/summary');

async function isAuthenticated(req, res, next) {
  if (req.user && req.isAuthenticated()) {
    var editable = await editApi.isEditable(req.session.passport.user.jwt);
    var autoClear = await clearApi.autoClear(req.session.passport.user.jwt);
    res.locals.editable = editable ? editable.attributes.Active : false;
    res.locals.autoClear = autoClear ? autoClear.attributes : null;
    res.locals.role = req.session.passport.user.role ? req.session.passport.user.role.name : null;
    if(res.locals.role !='Admin'){
      req.session.companySelected = req.session.passport.user.company;
      req.session.divisionSelected = req.session.passport.user.division;
    }
    return next();
  }

  return res.redirect('/login');
}

/* GET home page. */
router.get('/', isAuthenticated, async (req, res) => {
  let divisions = [];
  let materials = [];
  let summaries = [];
  const companies = await companyApi.companyList(req.session.passport.user.jwt);
  const { session } = req;
  if(req.session.passport.user.role&&req.session.passport.user.role.name=='Admin'){
    summaries = await summaryApi.summaryList(req.session.passport.user.jwt);
  }
  if(session.companySelected){
    divisions = await divisionApi.divisionList(
      session.companySelected,
      req.session.passport.user.jwt
    );
  }
  if (session.companySelected && session.divisionSelected && session.categorySelected) {
    materials = await materialApi.materialList(
      session.categorySelected,
      session.companySelected,
      session.divisionSelected,
      req.session.passport.user.jwt
    );
  }
  res.render('pages/dashboard', {
    pageTitle: 'Dashboard',
    material: materials,
    division: session.divisionSelected,
    company: session.companySelected,
    divisions,
    companies,
    category: session.categorySelected,
    summaries:summaries
  });
});

router.post('/', isAuthenticated, async (req, res) => {
  let divisions = [];
  let materials = [];
  let summaries = [];

  const { session } = req;
  if(req.session.passport.user.role&&req.session.passport.user.role.name=='Admin'){
    summaries = await summaryApi.summaryList(req.session.passport.user.jwt);
  }
  if ((req.body.division||session.divisionSelected) && req.body.company && req.body.Category) {
    session.companySelected = req.body.company;
    session.divisionSelected = req.body.division || session.divisionSelected;
    session.categorySelected = req.body.Category;
    divisions = await divisionApi.divisionList(req.body.company, req.session.passport.user.jwt);
    materials = await materialApi.materialList(
      req.body.Category,
      req.body.company,
      req.body.division,
      req.session.passport.user.jwt
    );
  }
  const companies = await companyApi.companyList(req.session.passport.user.jwt);
  res.render('pages/dashboard', {
    pageTitle: 'Dashboard',
    material: materials,
    division: req.body.division,
    company: req.body.company,
    divisions,
    companies,
    category: req.body.Category,
    summaries:summaries
  });
});

router.get('/material/:id', isAuthenticated, async (req, res) => {
  const { id } = req.params;
  const material = await materialApi.material(id, req.session.passport.user.jwt);
  if (material) {
    res.render('pages/material', {
      pageTitle: 'Materi',
      id,
      ...material,
    });
  } else {
    res.status(404);
  }
});

router.get('/settings', isAuthenticated, async (req, res) => {
  res.render('pages/settings', {
    pageTitle: 'Settings',
  });
});

router.get('/icons', isAuthenticated, (req, res) => {
  res.render('pages/icons');
});

router.get('/maps', isAuthenticated, (req, res) => {
  res.render('pages/maps');
});

router.get('/users', isAuthenticated, async (req, res) => {
  var error = req.query.s;
  const companies = await companyApi.companyList(req.session.passport.user.jwt);
  const users = await userApi.getUsers(req.session.passport.user.jwt, 1);
  if (users) {
    res.render('pages/users', {
      pageTitle: 'Users',
      users,
      page: 1,
      companies,
      error: error!= undefined,
      alert:(error)?"Operasi gagal. Terjadi kesalahan!":undefined,
    });
  } else {
    res.status(404);
  }
});

router.post('/users', isAuthenticated, async (req, res) => {
  let divisions = [];
  const companies = await companyApi.companyList(req.session.passport.user.jwt);
  if (req.body.company) {
    divisions = await divisionApi.divisionList(req.body.company, req.session.passport.user.jwt);
  }

  const users = await userApi.getUsers(
    req.session.passport.user.jwt,
    req.body.page,
    req.body.company,
    req.body.division
  );
  if (users) {
    res.render('pages/users', {
      pageTitle: 'Users',
      users,
      page: req.body.page,
      company: req.body.company,
      division: req.body.division,
      companies,
      divisions,
    });
  } else {
    res.status(404);
  }
});


mountRegisterRoutes(router);
mountLoginRoutes(router);
mountLogoutRoutes(router, [isAuthenticated]);
mountResetPasswordRoutes(router);
mountProfileRoutes(router, [isAuthenticated]);

module.exports = router;
