const express = require('express');
const router = express.Router();

const tools = require('../tools/general');

const server_constants = require('../tools/server_constants');

const { authenticate } = require('../middleware/authenticate');

const { User } = require('../models/user');

const { MsgTemplate } = require('../models/msg_template');

const telegrambot = require('../telegram/telegrambot');

const _ = require('lodash');

const { ObjectId } = require('mongodb');

router.post('/', authenticate, async (req, res) => {
  try {
    const idapp = req.body.idapp;
    let username = req.body.username;

    if ((!User.isAdmin(req.user.perm) && !User.isManager(req.user.perm)) && (username) !== req.user.username) {
      // If without permissions, exit
      return res.status(server_constants.RIS_CODE_ERR_UNAUTHORIZED).send({ code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: '' });
    }
    let aportador_solidario = req.user.aportador_solidario;
    let aportador_solidario_nome_completo = req.user.aportador_solidario_nome_completo;

    if (username) {
      aportador_solidario = await User.getAportadorSolidarioByUsername(idapp, username);
      aportador_solidario_nome_completo = await User.getNameSurnameByUsername(idapp, username);
    } else {
      username = req.user.username;
    }

    const dashboard = await User.getDashboard(idapp, aportador_solidario, username, aportador_solidario_nome_completo);
    if (dashboard)
      res.send({ dashboard });
    else
      res.status(400).send(e);

  } catch (e) {
    res.status(400).send(e);
  }

});

router.post('/downline', authenticate, async (req, res) => {
  try {
    const idapp = req.body.idapp;
    let username = req.body.username;

    if ((!User.isAdmin(req.user.perm) && !User.isManager(req.user.perm)) && (username) !== req.user.username) {
      // If without permissions, exit
      return res.status(server_constants.RIS_CODE_ERR_UNAUTHORIZED).send({ code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: '' });
    }
    let aportador_solidario = req.user.aportador_solidario;
    let aportador_solidario_nome_completo = req.user.aportador_solidario_nome_completo;

    if (username) {
      aportador_solidario = await User.getAportadorSolidarioByUsername(idapp, username);
      aportador_solidario_nome_completo = await User.getNameSurnameByUsername(idapp, username);
    } else {
      username = req.user.username;
    }

    const downline = await User.getDownline(idapp, aportador_solidario, username, aportador_solidario_nome_completo);
    if (downline)
      res.send({ downline });
    else
      res.status(400).send(e);

  } catch (e) {
    res.status(400).send(e);
  }

});

router.post('/getmsg_templates', authenticate, async (req, res) => {
  const idapp = req.body.idapp;

  let ris = await MsgTemplate.findAllIdApp(idapp);

  if (!!ris)
    res.send({ code: server_constants.RIS_CODE_OK, ris });
  else
    res.status(400).send(e);


});

router.post('/getflotte', authenticate, async (req, res) => {
  const idapp = req.body.idapp;

  let arrflotte = await Flotta.findAllIdApp(idapp);

  if (!!arrflotte)
    res.send({ code: server_constants.RIS_CODE_OK, arrflotte });
  else
    res.status(400).send(e);

});


router.post('/getdata', authenticate, async (req, res) => {
  const idapp = req.body.idapp;
  const data = req.body.data;

  try {

    const mystr = await Nave.getNavePos(idapp, parseInt(data.riga), parseInt(data.col));

    if (mystr)
      res.send({ code: server_constants.RIS_CODE_OK, ris: mystr });
    else
      res.status(400).send(e);
  } catch (e) {
    console.log(e.message)
  }

});


module.exports = router;
