'use strict';

const Images = require('../src/images');
const Twitter = require('../src/twitter');
const ReplyTweet = require('../src/tweets/reply_tweet');
const RequestTweet = require('../src/tweets/request_tweet');
//const Facebook = require('../src/facebook');

let stream = Twitter.stream();

stream.on('tweet', (payload) => {
  let request_tweet = new RequestTweet(payload);

  if (request_tweet.shouldReply()) {
    let image = new Images().getFromText(request_tweet.getText());
    if (!image) { return; }

    let reply_tweet = new ReplyTweet(request_tweet, image);
    Twitter.reply(reply_tweet);
  }
});

stream.on('error', (payload) => {
  console.error(payload);
});



const http = require('http');
const Bot = require('messenger-bot');
let bot = new Bot({
  token: process.env.FACEBOOK_PAGE_TOKEN,
  verify: process.env.FACEBOOK_VERIFY_TOKEN,
  app_secret: process.env.FACEBOOK_APP_SECRET
});

bot.on('error', (err) => {
  console.log(err.message);
});

bot.on('message', (payload, reply) => {
  //let text = payload.message.text; //change this to emoji
  console.log(payload.message);
  if (payload.message.text == undefined) {
    console.log('ingen text i meddelandet');
    return;
  }
  else {
    console.log(payload.message.text);
    let image = new Images().getFromText(payload.message.text);
    if (!image) { return; }
    console.log(image);
    let text = '';
    if (image.url == undefined) {
      text = `${image.key} Tyvärr! 🤔😞😬 Kanske hittar du något på https://digitaltmuseum.se?`;
    }
    else {
      text = `${image.key} ${image.url}`;
    }
    console.log(text);

    bot.getProfile(payload.sender.id, (err, profile) => {
      if (err) throw err;

      reply({ text }, (err) => {
        if (err) throw err;

        console.log(`Echoed back to ${profile.first_name} ${profile.last_name}: ${text}`);
      });
    });
  }
});

http.createServer(bot.middleware()).listen(3000);
console.log('Echo bot server running at port 3000.');

@@ -1144,247 +1144,742 @@
     
-console.log('ingen text i meddelandet');%0A    return;%0A  %7D%0A  else %7B%0A    console.log(payload.message.text);%0A    let image = new Images().getFromText(payload.message.text);%0A    if (!image) %7B return; %7D%0A    console.log(image);%0A    let text = '';
+let text = 'Ditt meddelande inneh%C3%B6ll ingen emoji. Skicka en emoji s%C3%A5 f%C3%A5r du ett foto eller f%C3%B6rem%C3%A5l fr%C3%A5n ett svenskt museum till svar.';%0A    console.log('ingen text i meddelandet');%0A    bot.getProfile(payload.sender.id, (err, profile) =%3E %7B%0A      if (err) throw err;%0A%0A      reply(%7B text %7D, (err) =%3E %7B%0A        if (err) throw err;%0A%0A        console.log(%60Echoed back to $%7Bprofile.first_name%7D $%7Bprofile.last_name%7D: $%7Btext%7D%60);%0A      %7D);%0A    %7D);%0A  %7D%0A  else %7B%0A    console.log(payload.message.text);%0A    let image = new Images().getFromText(payload.message.text);%0A    let text = '';%0A    if (!image) %7B%0A      text = 'Ditt meddelande inneh%C3%B6ll ingen emoji. Skicka en emoji s%C3%A5 f%C3%A5r du ett foto eller f%C3%B6rem%C3%A5l fr%C3%A5n ett svenskt museum till svar.';%0A    %7D%0A
 %0A   

send reply when there's no emoji facebook