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

const { Reaction } = require('../models/reaction');

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

const Hours = require('../models/hours');
const { ObjectID } = require('mongodb');

const sendemail = require('../sendemail');

const { Settings } = require('../models/settings');

const { SendNotif } = require('../models/sendnotif');

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

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

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

const _ = require('lodash');

const reg = require('../reg/registration');

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

const TypedError = require('../modules/ErrorHandler');
const globalTables = require('../tools/globalTables');

const mongoose = require('mongoose').set('debug', false);

router.post('/cmd', authenticate_noerror, async (req, res) => {

  const mydata = req.body.mydata;
  const idapp = req.body.idapp;
  const cmd = req.body.cmd;
  const id = req.body.id;
  const tab = req.body.tab;
  const num = req.body.num;
  const value = req.body.value;


  try {
    const table = tools.getTableByNumTab(tab);
    const mytable = globalTables.getTableByTableName(table);
    let username = '';
    if (req.user) {
      username = req.user.username;
    } else {
      return res.send({ state: 0, record: null });
    }

    let ris = null;
    let record = null;

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

    let objtoupdate = {};

    if (cmd === shared_consts.CMD_REACTION.SET_FAVORITE) {
      if (tab) {
        if (value) {
          ris = await Reaction.addFavorite(req, idapp, username, id, tab);
        } else {
          ris = await Reaction.removeFavorite(idapp, username, id, tab);
        }
      }

    } else if (cmd === shared_consts.CMD_REACTION.SET_BOOKMARK) {
      if (tab) {
        if (value)
          ris = await Reaction.addBookmark(req, idapp, username, id, tab);
        else
          ris = await Reaction.removeBookmark(idapp, username, id, tab);

      }
    } else if (cmd === shared_consts.CMD_REACTION.SET_ATTEND) {
      if (tab) {
        if (value)
          ris = await Reaction.addAttend(req, idapp, username, id, tab, num);
        else
          ris = await Reaction.removeAttend(idapp, username, id, tab);

      }
    } else if (cmd === shared_consts.CMD_REACTION.SET_SEEN) {
      if (tab) {
        if (value) {
          ris = await Reaction.addSeen(req, idapp, username, id, tab);
        }
        const tabtofind = tools.getNumTabByTable('mybachecas');
        if (tab === tabtofind) {
          const { MyBacheca } = require('../models/mybacheca');
          record = await MyBacheca.getCompleteRecord(idapp, id);
        }
      }
    }


    let state = (value && ris && ris.ok === 1) ? 1 : ((!value && ris && ris.ok === 1) ? -1 : 0);

    const risreac = await Reaction.calcReactions(idapp, id, tab);
    if (risreac) {
      delete risreac._id;
      objtoupdate = risreac;
    }

    if (mytable) {
      ris = await mytable.updateOne({ _id: id }, {
        $set: objtoupdate,
      })
    }

    return res.send({ state, record });

  } catch (e) {
    res.status(400).send();
    res.send({ code: server_constants.RIS_CODE_ERR, msg: e, state: null, record: null });

    console.log(e.message);
  }

});

module.exports = router;
