const logger = require('log4js').getLogger('FS 🔧');

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

// Bearer YlF_b6D149xr_xnrrYudlSnpn1A53b67vALlIK2HnD0ymBXQocRvPW3KjGN8jZNw0KnyAqxGaOzU7CLVPr84_KbnTxutNRXFVR9axmRqGN6ccda1xahoZo58KC2GWnYx'

logger.level = 'debug';

function reduceExplore(data) {
  const obj = {};
  if (typeof data === 'undefined' || isEmpty(data)) return obj;
  const { categories, location, contact } = data;

  //  console.log(contact);
  // make copy of object;

  const localObj = Object.assign({}, data);

  const iconPrefix = get(categories[0], 'icon.prefix', '');
  const iconSuffix = get(categories[0], 'icon.suffix', '');

  obj.name = get(localObj, 'name', '');
  obj.category = get(categories[0], 'shortName', '');
  obj.icon = (iconPrefix !== '') ? `${iconPrefix}64${iconSuffix}` : '';
  obj.id = get(localObj, 'id', '');
  obj.provider = 'foursquare';
  obj.address = get(location, 'formattedAddress', []).join(', ');
  obj.city = get(location, 'city', '');
  obj.state = get(location, 'state', '');
  obj.postcode = get(location, 'postalCode', '');
  obj.twitter = get(contact, 'twitter', '');
  obj.facebook = get(contact, 'facebookName', '');
  obj.url = get(localObj, 'url', '');
  obj.latitude = get(location, 'lat', '');
  obj.longitude = get(location, 'lng', '');

  // logger.debug(JSON.stringify(obj));

  return obj;
}

function reduceYelp(data) {
  const obj = {};

  if (typeof data === 'undefined' || isEmpty(data)) return obj;
  const yelpUrlfixer = /([--:\w?@%&+~#=]*\.[a-z]{2,4}\/{0,2})((?:[?&](?:\w+)=(?:\w+))+|[--:\w?@%&+~#=]+)?/g;

  const localObj = Object.assign({}, data);

  obj.url = get(localObj, 'url', '');
  obj.rating = get(localObj, 'rating', '');
  obj.reviewCount = get(localObj, 'review_count', '');

  if (obj.url !== '') {
    const url = yelpUrlfixer.exec(obj.url);
    const urlBit = url[2];
    obj.viewIntent = `https://m.yelp.com/${urlBit}`;
  }
  else
    obj.viewIntent = '';

  return obj;
}

function reduceFullFS(data) {
  const obj = {};
  if (typeof data === 'undefined' || isEmpty(data)) return obj;
  const localObj = Object.assign({}, data);

  const photosCount = get(localObj, 'photos.count', 0);
  const tipsCount = get(localObj, 'tips.count', 0);

  if (photosCount > 0) {
    const photoItems = get(localObj, 'photos.groups[0].items');
    obj.images = photoItems.map(item => {
      const prefix = get(item, 'prefix', '');
      const suffix = get(item, 'suffix', '');
      const width = get(item, 'width', 640);
      const height = get(item, 'height', 480);

      const ratio = width / 640;
      let ratioHeight = ~~(height / ratio);
      if (ratioHeight <= 0) ratioHeight = 640;
      console.log(`${width}, ${height} => ${640}, ${ratioHeight}`);

      return `${prefix}${640}x${ratioHeight}${suffix}`;
    });
  }

  if (tipsCount > 0) {
    const tipItems = get(localObj, 'tips.groups[0].items');
    obj.tips = tipItems.map(item => {
      return get(item, 'text', '');
    });
  }

  return obj;
}

function reduceTwitter(data) {
  let obj = [];
  if (data.length > 0)
    obj = data.map(item => {
      return get(item, 'text');
    });

  return obj;
}

module.exports = { reduceExplore, reduceYelp, reduceFullFS, reduceTwitter };
