import { createSelector } from 'reselect';
import isEmpty from 'lodash/isEmpty';
import moment from 'moment';
import { format } from 'd3-format';

const getData = state => state.data || null;
const getBbox = state => state.bbox || null;
const getDataStatus = state => state.dataStatus || null;
const getSettings = state => state.settings || null;

const getFilteredData = createSelector(
  [getData, getSettings],
  (data, settings) => {
    if (!data || isEmpty(data)) return null;

    const { clouds } = settings;
    return data.filter(item => item.attributes.cloud_score <= clouds);
  }
);

export const getAllTiles = createSelector([getFilteredData], data => {
  if (!data || isEmpty(data)) return [];

  return data.map(item => ({
    id: item.attributes.source,
    url: item.attributes.tile_url,
    thumbnail: item.attributes.thumbnail_url,
    cloudScore: item.attributes.cloud_score,
    dateTime: item.attributes.date_time,
    instrument: item.attributes.instrument,
    description: `${moment(item.attributes.date_time)
      .format('DD MMM YYYY')
      .toUpperCase()} - ${item.attributes.cloud_score}% cloud coverage - ${
      item.attributes.instrument
    }`
  }));
});

export const getTile = createSelector(
  [getData, getSettings],
  (data, settings) => {
    if (!data || isEmpty(data)) return null;

    const { selectedTileIndex } = settings;
    const selectedTile = data[selectedTileIndex].attributes;
    return {
      url: selectedTile.tile_url,
      cloudScore: selectedTile.cloud_score,
      dateTime: selectedTile.date_time,
      instrument: selectedTile.instrument,
      description: `${moment(selectedTile.date_time)
        .format('DD MMM YYYY')
        .toUpperCase()} - ${format('.0f')(
        selectedTile.cloud_score
      )}% cloud coverage - ${selectedTile.instrument}`
    };
  }
);

export const getBounds = createSelector([getBbox], bbox => {
  if (!bbox || isEmpty(bbox)) return null;

  return bbox.geometry.coordinates;
});

export const getSources = createSelector(
  [getData, getDataStatus],
  (data, dataStatus) => {
    if (!data || isEmpty(data)) return null;

    const { tilesPerRequest, requestedTiles } = dataStatus;
    return data
      .slice(requestedTiles, requestedTiles + tilesPerRequest)
      .map(item => ({ source: item.attributes.source }));
  }
);

export const getDates = createSelector([getSettings], settings => {
  const { date, weeks } = settings;
  const currentDate = date ? moment(date) : moment();

  return {
    end: currentDate.format('YYYY-MM-DD'),
    start: currentDate.subtract(weeks, 'weeks').format('YYYY-MM-DD')
  };
});

@@ -1083,16 +1083,37 @@
 ()%7D - $%7B
+format('.0f')(%0A      
 item.att
@@ -1131,16 +1131,22 @@
 ud_score
+%0A    )
 %7D%25 cloud
@@ -1159,23 +1159,16 @@
 age - $%7B
-%0A      
 item.att
@@ -1185,21 +1185,16 @@
 strument
-%0A    
 %7D%60%0A  %7D))

fix cloud coverage format