import {createAction} from 'redux-actions'
import _ from 'underscore'

const actionProps = {
  toggleLastfmSettings: ['setup'],
  saveLastfmUser: ['username', 'reset'],
  saveMaxPlayCount: ['maxPlayCount', 'reset'],
  updateWeather: ['summary', 'temperature', 'apparentTemperature', 'unit', 'icon'],
  updateCurrentTrack: ['id', 'artists', 'title', 'link', 'cover', 'blacklistSkip', 'artist'],
  updateLastfm: ['link', 'playCount', 'lastfmLike'],
  updateBio: ['summary', 'bio', 'similars', 'tags'],
  updateLyrics: ['id'],
  updateSpotifyLike: ['spotifyLike'],
  dismissAlert: ['key'],
  sendAlert: ['text', 'type'],
  updateIsPlaying: ['isPlaying'],
  addToBlacklist: ['trackId'],
  setBlacklist: ['blacklist'],
  updateSpotifyProfile: ['premium'],
  updateColors: ['colors'],
  updateSearchResult: ['result'],
  keyPress: ['keyCode'],
  updateFullscreen: ['enabled']
};

// it's possible to call any action as action(arg1,arg2,arg3)
// or action({agr2:1, arg1:2, arg3:3}) but only props from actionProps[name] are passed.
// in case of some props from actionProps[name] not exist they will be created with undefined value
// if only one prop is defined in actionProps[name] - the argument is passed as is.
// if no props is defined in actionProps[name] - arguments are ignored,
// if action is not defined in actionProps - it is passed without props.
function load(name) {
  const combine = (props, values = []) => _.object(props || [], values);
  const payload = args => args.length == 1 && Array.isArray(actionProps[name]) && actionProps[name].length > 1;

  return createAction(name,
    (...args) => payload(args) ? _.extendOwn(combine(actionProps[name]), args.pop()) : combine(actionProps[name], args)
  );
}

export default function (needed) {
  let filtered = {};
  if (Array.isArray(needed)) {
    for (let i = 0; i < needed.length; i++) {
      filtered[needed[i]] = load(needed[i]);
    }
  } else if (typeof needed == 'string') {
    filtered[needed] = load(needed);
  } else {
    for (const prop of Object.keys(needed)) {
      filtered[needed[prop]] = load(prop);
    }
  }

  return filtered;
}