import { Router, Request, Response, NextFunction } from 'express';
import { titreBase } from '../utils/define';
import { authenticate } from '../middlewares/authenticate';
import { authorizeEnseignant } from '../middlewares/authorizeEnseignant';
import { errorCode500 } from '../utils/errors';
import { Cours } from '../models/cours';
import { COOKIE_TOKEN } from '../utils/constant';
import { CoursControlleur } from '../controleurs/coursControleur';
import { coursController } from '../locator';

export class ViewRouter {
  private _router: Router

  get router() {
    return this._router;
  }
  
  constructor() {
    this._router = Router();
    this.init();
  }
  
  public questionnaires(req: Request, res: Response, next: NextFunction) {
    try {
      res.render('questionnaire',
      {
        title: `${titreBase}`,
        user: req.body.user,
      });
    } catch (error) {
      errorCode500(error, req, res);
    }
  }
  
  public async gestionnaireCours(req: Request, res: Response, next: NextFunction){
    try {
      var listeCours = coursController.recupererCours(req.body.user);
      res.render('gestionnaireCours',
      {
        title: `${titreBase}`,
        user: req.body.user,
        code: req.params.code,
        listeCours: listeCours
      });
    } catch (error) {
      errorCode500(error, req, res);
    }
  }
  
  public async cours(req: Request, res: Response, next: NextFunction) {
    try {
      var cours = coursController.recupererCoursDetails(req.params.id);
      res.render('cours',
      {
        title: `${req.params.id}`,
        user: req.body.user,
        code: req.params.code,
        id: req.params.id,
        cours: cours
      });
    } catch (error) {
      errorCode500(error, req, res);
    }
  }

  public async testTable(req: Request, res: Response, next: NextFunction) {
    try {
      res.render('test-table',
      {
        title: `test table`,
      });
    } catch (error) {
      errorCode500(error, req, res);
    }
  }
  
  init() {
    this._router.get('/questionnaires', authenticate, this.questionnaires.bind(this));
    this._router.get('/cours', authenticate, authorizeEnseignant, this.gestionnaireCours.bind(this));
    this._router.get('/cours/:id', authenticate, authorizeEnseignant, this.cours.bind(this));
    this._router.get('/jf', this.testTable.bind(this));
  }
  
}

export const viewRouter = new ViewRouter();