import {createSelector} from 'reselect';
import {isUuid, constructHydraId, deconstructHydraId} from '@ciscospark/react-component-utils';

const getWidget = (state) => state.widgetSpace;
const getSpark = (state) => state.spark;
const getMedia = (state) => state.media;
const getConversation = (state) => state.conversation;
const getUsers = (state) => state.users;
const getCurrentUser = (state, ownProps) => ownProps.currentUser;
const getFeatures = (state) => state.features;
const getOwnProps = (state, ownProps) => ownProps;

export const getSpaceDetails = createSelector(
  [getWidget, getConversation, getUsers, getCurrentUser],
  (widget, conversation, users, currentUser) => {
    const spaceDetails = widget.get('spaceDetails');
    if (conversation.get('id') && currentUser) {
      let title = spaceDetails.get('title');
      let avatarId = conversation.get('id');
      let toUser;
      // Grab avatar if we're in a direct conversation
      if (spaceDetails.get('type') === 'direct') {
        toUser = conversation.get('participants')
          .filter((p) => p.get('emailAddress') !== currentUser.email).first();
        if (toUser) {
          title = toUser.get('displayName');
          avatarId = toUser.get('id');
        }
      }
      const toPerson = toUser ? toUser.get('id') : widget.get('toPerson');

      return widget.get('spaceDetails').merge({
        avatarId,
        title,
        toPerson
      });
    }
    return spaceDetails;
  }
);

export const getActivityTypes = createSelector(
  [getWidget, getFeatures],
  (widget, features) => {
    const spaceType = widget.getIn(['spaceDetails', 'type']);
    const activityTypes = widget.get('activityTypes').toJS();
    const filteredActivityTypes = [];
    activityTypes.forEach((activityType) => {
      let isValid = true;
      // Filter activity Type based on spaceType
      if (spaceType && !activityType.spaceTypes.includes(spaceType)) {
        isValid = false;
      }
      // Filter activity type based on feature requirement
      if (activityType.feature && !activityType.feature.hide && !features.getIn(['items', activityType.feature.key])) {
        isValid = false;
      }
      // Hide flag on feature means to hide activity if feature is enabled
      if (activityType.feature && activityType.feature.hide && features.getIn(['items', activityType.feature.key])) {
        isValid = false;
      }
      if (isValid) {
        filteredActivityTypes.push(activityType);
      }
    });
    return filteredActivityTypes;
  }
);

const getCall = createSelector(
  [getConversation, getMedia],
  (conversation, media) => {
    const call = media.getIn(['byId', conversation.get('locusUrl')]);
    if (call && call.isDismissed) {
      return null;
    }
    return call;
  }
);

const getTo = createSelector(
  [getSpaceDetails, getOwnProps],
  (spaceDetails, ownProps) => {
    const {
      spaceId,
      toPersonId,
      toPersonEmail
    } = ownProps;
    let to = toPersonId || toPersonEmail;

    if (spaceDetails && spaceDetails.has('id')) {
      return spaceDetails.get('id');
    }
    if (spaceId) {
      if (isUuid(spaceId)) {
        to = constructHydraId('ROOM', spaceId);
      }
      else {
        const {type} = deconstructHydraId(spaceId);
        if (type === 'ROOM') {
          to = spaceId;
        }
      }
    }

    return to;
  }
);

export const getSpaceWidgetProps = createSelector(
  [getWidget, getSpark, getMedia, getSpaceDetails, getActivityTypes, getCall, getTo],
  (widget, spark, media, spaceDetails, activityTypes, call, to) => ({
    activityTypes,
    media,
    sparkInstance: spark.get('spark'),
    sparkState: spark.get('status'),
    spaceDetails,
    widgetStatus: widget.get('status'),
    call,
    to
  })
);

@@ -86,16 +86,28 @@
 tHydraId
+, hydraTypes
 %7D from '
@@ -2978,40 +2978,10 @@
 t to
- = toPersonId %7C%7C toPersonEmail
 ;%0A
+
 %0A   
@@ -3148,22 +3148,31 @@
 HydraId(
-'
+hydraTypes.
 ROOM
-'
 , spaceI
@@ -3273,14 +3273,23 @@
 === 
-'
+hydraTypes.
 ROOM
-'
 ) %7B%0A
@@ -3336,16 +3336,392 @@
 %7D%0A    %7D%0A
+    else if (toPersonId) %7B%0A      if (isUuid(toPersonId)) %7B%0A        to = constructHydraId(hydraTypes.PEOPLE, toPersonId);%0A      %7D%0A      else %7B%0A        const %7Btype%7D = deconstructHydraId(toPersonId);%0A        if (type === hydraTypes.PEOPLE) %7B%0A          to = toPersonId;%0A        %7D%0A      %7D%0A    %7D%0A    else %7B%0A      to = toPersonEmail;%0A    %7D%0A%0A    // %60to%60 should be an email or hydra id
 %0A    ret

handle uuids for toPersonId