const logger = require('log4js').getLogger('FSQ');
const foursquare = require('node-foursquare-venues')('IXXFUGW3NC3DEVS2V5EU4NV4CL5E12AYGUPIR2D3U3B5DX4B', 'MZRIJDCEKUMVERA1OKVAIZI0TYAEBD3W2A2AGPTPI5TOLL1D', '20170801');
const Twitter = require('twitter');
const yelp = require('yelp-fusion');

const client = yelp.client('YlF_b6D149xr_xnrrYudlSnpn1A53b67vALlIK2HnD0ymBXQocRvPW3KjGN8jZNw0KnyAqxGaOzU7CLVPr84_KbnTxutNRXFVR9axmRqGN6ccda1xahoZo58KC2GWnYx');

const { get, isEmpty } = require('lodash');

const { reduceExplore, reduceYelp, reduceFullFS, reduceTwitter } = require('./reducers/rightbyme');

const twitterClient = new Twitter({
  'consumer_key': 'bJvwgjA9O52j7rC6mqoeefPLO',
  'consumer_secret': 'NB6ASJxxMI9yaOgTAlWEpd18J1BdtIOYb4iz1HivIVpPqSLBY5',
  'access_token_key': '4773651-1IrcQuKzEBQYe89Ooe0wCEP3reiyJZ1SQl003b5DoU',
  'access_token_secret': 'AHWGNQLAVGrXeUU3FU1ubdro4z5zc5igeiN4B9qI99xIt'
});

logger.level = 'debug';

function doFSVenueSearch(ll, data = {}) {
  let payLoad = Object.assign({}, data);
  logger.debug('>> doFSVenueSearch');

  return new Promise((resolve, reject) => {
    const fsObj = {
      'll': ll,
      'radius': 15,
      'v': '20170801',
      'limit': 1
    };

    if (isEmpty(payLoad))
      foursquare.venues.search(fsObj, function(err, fsData) {
        if (err)
          return reject(err);
        else {
          const venues = get(fsData, 'response.venues');
          if (venues.length > 0) {
            fsP1 = venues[0];

            payLoad = reduceExplore(fsP1);

            return resolve(payLoad);
          }
        }
      });

    else

      return resolve(payLoad);
  });
}

function doFSVenueExplore(ll) {
  let payLoad = {};
  logger.debug('>> doFSVenueExplore', ll);

  return new Promise((resolve, reject) => {
    const fsObj = {
      'll': ll,
      'radius': 35,
      'v': '20170801',
      'limit': 1
    };

    foursquare.venues.explore(fsObj, function(err, fsData) {
      if (err)
        return reject(err);
      else {
        const groups = get(fsData, 'response.groups');
        const items = groups[0];
        const venues = items.items[0];

        if (venues) {
          fsP1 = venues.venue;

          payLoad = reduceExplore(fsP1);
        }

        return resolve(payLoad);
      }
    });
  });
}

function doYelpSearch(data = {}) {
  const payLoad = Object.assign({}, data);
  logger.debug('>> doYelpSearch');

  const yelpSearch = {};

  return new Promise((resolve, reject) => {
    if (!isEmpty(payLoad)) {
      yelpSearch.term = payLoad.name;
      yelpSearch.latitude = payLoad.latitude;
      yelpSearch.longitude = payLoad.longitude;
      yelpSearch.location = payLoad.address;
      yelpSearch.radius = 250;
      yelpSearch.sort_by = 'best_match';
      client.search(yelpSearch).then(response => {
        const respArray = get(response, 'jsonBody.businesses');
        yelpReply = (respArray.length > 0) ? respArray[0] : {};

        reduceYelp(yelpReply);

        payLoad.yelp = reduceYelp(yelpReply);

        return resolve(payLoad);
      }).catch(e => {
        console.error(e);

        return reject(e);
      });
    }
    else

      return resolve(payLoad);
  });
}

function doFSGetFullVenue(data = {}) {
  const payLoad = Object.assign({}, data);
  logger.debug('>> doFSGetFullVenue');

  // https://api.foursquare.com/v2/venues/4c5ff51213791b8d0d5c4eaf?client_id=IXXFUGW3NC3DEVS2V5EU4NV4CL5E12AYGUPIR2D3U3B5DX4B&client_secret=MZRIJDCEKUMVERA1OKVAIZI0TYAEBD3W2A2AGPTPI5TOLL1D&v=20180224
  return new Promise((resolve, reject) => {
    if (!isEmpty(payLoad)) {
      // stuff
      // more
      const id = payLoad.id;
      foursquare.venues.venue(id, {}, function(err, fsData) {
        if (err)
          return reject(err);
        else {
          const partPayload = reduceFullFS(get(fsData, 'response.venue'));
          const newPayload = Object.assign(payLoad, partPayload);

          return resolve(newPayload);
        }
      });
    }
    else

      return resolve(payLoad);
  });
}

function doTweetSearch(data = {}) {
  const payLoad = Object.assign({}, data);
  logger.debug('>> doTweetSearch');

  return new Promise((resolve, reject) => {
    if (!isEmpty(payLoad)) {
      console.log('payLoad.twitter', payLoad.twitter);
      if (payLoad.twitter !== '') {
        const params = { 'screen_name': payLoad.twitter };

        twitterClient.get('statuses/user_timeline', params, function(error, tweets, response) {
          if (error)
            return reject(error);
          else {
            // const partPayload = reduceFullFS(get(fsData, 'response.venue'));
            // const newPayload = Object.assign(payLoad, partPayload);

            // return resolve(newPayload);
            const reducedTweets = reduceTwitter(tweets);

            payLoad.tweets = reducedTweets;

            return resolve(payLoad);
          }
        });
      }
      else
        return resolve(payLoad);
    }
    else

      return resolve(payLoad);
  });
}

function doGetRightByMe(ll) {
  return new Promise((resolve, reject) => {
    doFSVenueExplore(ll)
      .then((d) => {
        return doFSVenueSearch(ll, d);
      })
      .catch((e) => {
        logger.error(e);

        return reject(e);
        // res.status(500).send('There was an error!');
      })
      .then((d) => {
        return doYelpSearch(d);
      })
      .catch((e) => {
        logger.error(e);

        return reject(e);
        // res.status(500).send('There was an error!');
      })
      .then((d) => {
        // return doYelpSearch(d)
        return doTweetSearch(d);
      })
      .then((d) => {
        // return doYelpSearch(d)
        return doFSGetFullVenue(d);
      })
      .catch((e) => {
        logger.error(e);

        return reject(e);
        // res.status(500).send('There was an error!');
      })
      .then((d) => {
        logger.info('Final', d.name, d.id);;

        return resolve(d);
      });
  });
}

async function test(ll, near) {
  return new Promise((resolve, reject) => {
    doFSVenueExplore(ll)
      .then((d) => {
        return doFSVenueSearch(ll, d);
      })
      .catch((e) => {
        logger.error(e);

        return reject(e);
        // res.status(500).send('There was an error!');
      })
      .then((d) => {
        return doYelpSearch(d);
      })
      .catch((e) => {
        logger.error(e);

        return reject(e);
        // res.status(500).send('There was an error!');
      })
      .then((d) => {
        // return doYelpSearch(d)
        return doTweetSearch(d);
      })
      .then((d) => {
        // return doYelpSearch(d)
        return doFSGetFullVenue(d);
      })
      .catch((e) => {
        logger.error(e);

        return reject(e);
        // res.status(500).send('There was an error!');
      })
      .then((d) => {
        logger.info('Final', d.name, d.id);;

        return resolve(d);
      });
  });
}
module.exports = { doGetRightByMe };

const tests = {
  'cruachan' : '56.3946,-5.1166',
  'morrisonsGarage' : '55.9429,-4.5622',
  'tescohelensburgh' : '56.003466,-4.733689',
  'howlinwolf' : '55.863991,-4.257788'
};

// doGetRightByMe('56.3946,-5.1166'); // cruachan

// doGetRightByMe('55.9429,-4.5622'); // morrisons garage

// 56.3890134, "lng" : -5.0939317

// cruachan 56.3946,-5.1166

// tesco helensburgh
// const location = { 'homeDistance':12.941340256604686, 'workDistance':33.47577415510536, 'latitude':56.003466, 'longitude':-4.733689, 'atHome':false, 'atWork':false, 'timestamp':1519638618383, 'll':'56.003466,-4.733689', 'llFixed':'56.003,-4.734', 'city':'Helensburgh', 'cityCC':'Helensburgh,GB', 'address':'9-13 Sinclair St, Helensburgh G84, UK' };

// test('55.863991,-4.257788');

// howling wolf http://localhost:8110/rightbyme?ll=55.863991,-4.257788

/*
{"response":{"result":[{"cards":[{"card_id":"83a014c3-c0a4-3242-b5a8-79d011b677d6","rendering_engine":"custom","type":"WEATHER","type_display_name":"Weather","ttl":1519810720,"layout":{"template":"weather"},"data":{},"modules":{},"reason":"","notify":false,"ranking_arguments":{"STREAM_TYPE:MAIN":1.0,"rule_score":3.0,"TIER":3.0,"TIME_OF_DAY_RANGE:MORNING":1.0,"score":0.6},"instrumentation":{"rid":"13kfji9d97ld0","bucket":"ga"}},{"card_id":"c81eac4b-4553-3d60-85a6-178968bd5065","rendering_engine":"custom","type":"NEWS_DIGEST","type_display_name":"News Digest","ttl":1519810720,"layout":{"template":"news_digest"},"data":{},"modules":{},"reason":"","notify":false,"ranking_arguments":{"STREAM_TYPE:MAIN":1.0,"rule_score":2.0,"TIER":2.0,"TIME_OF_DAY_RANGE:MORNING":1.0,"score":0.55},"instrumentation":{"rid":"13kfji9d97ld0","bucket":"ga"}},{"card_id":"4b5b8c71-58d2-3f26-abce-318e3d3a01d6","rendering_engine":"custom","type":"MORNING_NIGHT","type_display_name":"Set Alarm","ttl":1519810720,"layout":{"template":"morning_night"},"data":{},"modules":{},"reason":"","notify":false,"ranking_arguments":{"USER_LOCATION:OTHER":1.0,"USER_SPEED:STILL":1.0,"STREAM_TYPE:MAIN":1.0,"rule_score":3.0,"TIER":3.0,"TIME_OF_DAY_RANGE:MORNING":1.0,"score":0.5},"instrumentation":{"rid":"13kfji9d97ld0","bucket":"ga"}},{"card_id":"bcacee7a-d79a-3e4e-a5b8-77c5b6480a8c","rendering_engine":"custom","type":"CALENDAR","type_display_name":"Calendar","ttl":1519810720,"layout":{"template":"calendar"},"data":{},"modules":{},"reason":"","notify":false,"ranking_arguments":{"USER_LOCATION:OTHER":1.0,"STREAM_TYPE:MAIN":1.0,"rule_score":2.0,"TIER":2.0,"TIME_OF_DAY_RANGE:MORNING":1.0,"score":0.25},"instrumentation":{"rid":"13kfji9d97ld0","bucket":"ga"}},{"card_id":"9953c0e9-5b77-3162-bcf4-e19fcd1f541f","rendering_engine":"custom","type":"VENUE_INFO","type_display_name":"Venues","ttl":1519641520,"layout":{"template":"venue_info"},"data":{"name":"Tesco","category":"Grocery Store","iconUrl":"https://ss3.4sqi.net/img/categories_v2/shops/food_grocery_64.png","id":"4c5ff51213791b8d0d5c4eaf","provider":"foursquare","tips":["Prepare to be deafened by relief calls over the loudspeaker"],"images":[],"address":"23-25 Sinclair Street","city":"Helensburgh","state":"Argyll and Bute","zip":"G84 8SR","twitter":{"handle":"uktescooffers","viewIntent":"https://twitter.com/uktescooffers#Intent;action=android.intent.action.VIEW;package=com.twitter.android;end","tweetIntent":"#Intent;action=android.intent.action.SEND;component=com.twitter.android/.PostActivity;S.android.intent.extra.TEXT=@uktescooffers;end"},"yelp":{"url":"https://m.yelp.com/biz/tesco-stores-helensburgh?adjust_creative=ogmBMO91tbdmscbTIaQEdA&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ogmBMO91tbdmscbTIaQEdA","rating":3.5,"reviewCount":4,"viewIntent":"https://m.yelp.com/biz/tesco-stores-helensburgh?adjust_creative=ogmBMO91tbdmscbTIaQEdA&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ogmBMO91tbdmscbTIaQEdA#Intent;action=android.intent.action.VIEW;end"}},"modules":{},"reason":"","notify":false,"ranking_arguments":{"poi_latitude":56.00348039818216,"poi_longitude":-4.733884334564209,"USER_LOCATION:OTHER":1.0,"req_longitude":-4.7336884,"req_latitude":56.003464,"STREAM_TYPE:MAIN":1.0,"USER_LOCATION_POI_CONFIDENCE:HIGH":1.0,"rule_score":0.0,"TIER":0.0,"USER_DIST_FROM_REQ_LOC:WITHIN_100M":1.0,"score":0.25},"instrumentation":{"rid":"13kfji9d97ld0","bucket":"ga"}},{"card_id":"eea4f59a-e8a2-3e12-a917-96e254c416d5","rendering_engine":"custom","type":"VENUE_CHOOSER","type_display_name":"Around Me","ttl":1519641520,"layout":{"template":"venue_chooser"},"data":{"venues":[{"name":"Lido's Fish & Chips","category":"Fish & Chips Shop","iconUrl":"https://ss3.4sqi.net/img/categories_v2/food/fishandchips_64.png","id":"4b9a9a77f964a5207ec535e3","provider":"foursquare","eid":"{\"card\":\"venue\",\"id\":\"4b9a9a77f964a5207ec535e3\"}"},{"name":"Mr Kebab","category":"Fast Food Restaurant","iconUrl":"https://ss3.4sqi.net/img/categories_v2/food/fastfo

        {"response":{"result":[{"cards":[{"card_id":"9953c0e9-5b77-3162-bcf4-e19fcd1f541f","rendering_engine":"custom","type":"VENUE_INFO","type_display_name":"Venues","ttl":1519654292,"layout":{"template":"venue_info"},"data":{"name":"Speirs Wharf","category":"Pool","iconUrl":"https://ss3.4sqi.net/img/categories_v2/parks_outdoors/pool_64.png","id":"5211c15a11d2c83298e7f8c2","provider":"foursquare","tips":[],"images":["https://igx.4sqi.net/img/general/640x640/72939115_8s_CraZskGLT5uJz5DzRiyBvKXgy6BobQBH5u0Vcsy4.jpg","https://igx.4sqi.net/img/general/960x364/62519220_Z_xAdlllLg7Mb1BUt1b9G18IKTpF1jyeNqGCMo0WhRs.jpg","https://igx.4sqi.net/img/general/960x541/24390977_Ws2HHTyM1zKBlvejccG1BxGTt0hKBURtWQOPPxYc7IM.jpg","https://igx.4sqi.net/img/general/960x960/149524__j5bRwPX5zy5RmOu-uleVtQ-KekAS-rXAJLgc_JjipY.jpg","https://igx.4sqi.net/img/general/640x640/4894760_ZOelH_F7ljq0Mjk6Zzs7KHH8-Dn470lbq4IfFg7WGiM.jpg","https://igx.4sqi.net/img/general/640x640/4894760_kW7NmETQYJxkGfBBnhHhQdDHcVouBj7OWpZBbyy0OTM.jpg"],"telephone":"","address":"","city":"","state":"","zip":"","latitude":55.872669822119796,"longitude":-4.257480441815151,"twitter":{"handle":null,"viewIntent":null,"tweetIntent":null},"yelp":{"url":null,"rating":0.0,"reviewCount":0,"viewIntent":null}},"modules":{},"reason":"","notify":false,"ranking_arguments":{"poi_latitude":55.872669822119796,"poi_longitude":-4.257480441815151,"USER_LOCATION:OTHER":1.0,"req_longitude":-4.2577868,"req_latitude":55.86399,"USER_LOCATION_POI_CONFIDENCE:HIGH":1.0,"STREAM_TYPE":1.0,"rule_score":0.0,"TIER":0.0,"USER_DIST_FROM_REQ_LOC:WITHIN_100M":1.0,"score":0.0},"instrumentation":{"rid":"fn62krdd981s4","bucket":"ga"}}]}],"error":null}}
*/

