const { User } = require('../models/user'),
  passport = require('passport'),
  LocalStrategy = require('passport-local'),
  multer = require('multer');

exports.index = (req, res) => {
  // console.log(req.user);
  res.render('user/profile', {
    user: req.user,
    conect: req.user,
    message: req.flash('editProfile')
  });
};

exports.getUser = (req, res) => {
  // console.log(req.params.id);
  const username = req.params.id;

  User.findOne({ username: username }, (err, user) => {
    if (err)
      console.log(err);
    else if (user) {
      // console.log(user);
      res.render('user/profile', { user: user, conect: req.user, message: '' });
    } else {
      console.log('No existe el Usuario');
      res.redirect('/');
    }
  });
};

exports.editUser = (req, res) => {
  const id = req.params.id;
  const conect = req.user;
  // console.log(req.user);
  // console.log(id);

  if (req.user.state != 1) {
    if (id !== conect.username) res.redirect('/');
  }

  User.findOne({ username: id }, (err, user) => {
    if (err) console.log(err);
    else if (user !== null)
      res.render('user/edit', { user: user, conect: conect });
    else
      res.redirect('/');
  });
};

exports.photoUser = (req, res) => {
  const id = req.params.id;
  const conect = req.user;

  if (req.user.state != 1) {
    if (id !== conect.username) res.redirect('/');
  }

  User.findOne({ username: id }, (err, user) => {
    if (err) console.log(err);
    else if (user !== null)
      res.render('user/photo', { user: user, conect: conect });
    else
      res.redirect('/');
  });
};

// POST
exports.editPhotoUser = (req, res) => {
  const id = req.params.id;
  const conect = req.user;

  if (req.user.state != 1) {
    if (id !== conect.username) res.redirect('/');
  }

  User.findOne({ username: id }, (err, user) => {
    if (err)
      console.log(err);
    else if (user !== null) {
      const storage = multer.diskStorage({
        destination: (req, file, callback) => {
          callback(null, './images/profile');
        },
        filename: (req, file, callback) => {
          callback(null, file.fieldname + '-' + Date.now());
        }
      });
      const upload = multer({ storage: storage }).single('userPhoto');
      // upload(req, res, function(err) {
      //   if (err)
      //     return res.end('Error uploading file.');
      //   res.end('File is uploaded');
      // });
    } else
      res.redirect('/');
  });
};

// PUT
exports.saveEditUser = (req, res) => {
  const id = req.params.id;
  const conect = req.user;
  console.log(conect);
  console.log(id);

  User.findOne({ username: id }, (err, user) => {
    if (err)
      console.log(err);
    else {
      console.log(user);

      user.name = req.body.name;
      user.last_name = req.body.last;
      user.user_id = req.body.user_id;
      user.email = req.body.email;
      user.profession = req.body.profession;

      user.save(err => {
        if (!err) {
          req.flash(
            'editProfile',
            'Perfil de ' + req.params.id + ' editado con éxito'
          );
          if (conect.state === 1)
            res.redirect('/admin/users/');
          else
            res.render('user/profile', {
              user: user,
              conect: user,
              message: req.flash('editProfile')
            });
        } else
          res.render('index');
      });
    }
  });
};
