/* @flow */
import { LIB_DID_DISPOSE, LIB_DID_INIT } from '../lib-jitsi-meet';
import {
    MEDIA_TYPE,
    SET_AUDIO_MUTED,
    SET_CAMERA_FACING_MODE,
    SET_VIDEO_MUTED,
    setAudioMuted,
    setVideoMuted
} from '../media';
import { MiddlewareRegistry } from '../redux';
import { createLocalTracks, destroyLocalTracks } from './actions';
import { TRACK_UPDATED } from './actionTypes';
import { getLocalTrack, setTrackMuted } from './functions';
/**
 * Middleware that captures LIB_DID_DISPOSE and LIB_DID_INIT actions and,
 * respectively, creates/destroys local media tracks. Also listens to
 * media-related actions and performs corresponding operations with tracks.
 *
 * @param {Store} store - Redux store.
 * @returns {Function}
 */
MiddlewareRegistry.register(store => next => action => {
    switch (action.type) {
    case LIB_DID_DISPOSE:
        store.dispatch(destroyLocalTracks());
        break;
    case LIB_DID_INIT:
        store.dispatch(createLocalTracks());
        break;
    case SET_AUDIO_MUTED:
        _setMuted(store, action, MEDIA_TYPE.AUDIO);
        break;
    case SET_CAMERA_FACING_MODE:
        store.dispatch(
            createLocalTracks({
                devices: [ MEDIA_TYPE.VIDEO ],
                facingMode: action.cameraFacingMode
            })
        );
        break;
    case SET_VIDEO_MUTED:
        _setMuted(store, action, MEDIA_TYPE.VIDEO);
        break;
    case TRACK_UPDATED:
        return _trackUpdated(store, next, action);
    }
    return next(action);
});
/**
 * Gets the local track associated with a specific MEDIA_TYPE in a
 * specific Redux store.
 *
 * @param {Store} store - The Redux store from which the local track associated
 * with the specified mediaType is to be retrieved.
 * @param {MEDIA_TYPE} mediaType - The MEDIA_TYPE of the local track to
 * be retrieved from the specified store.
 * @private
 * @returns {Track} The local Track associated with the specified
 * mediaType in the specified store.
 */
function _getLocalTrack(store, mediaType: MEDIA_TYPE) {
    return getLocalTrack(store.getState()['features/base/tracks'], mediaType);
}
/**
 * Mutes or unmutes a local track with a specific media type.
 *
 * @param {Store} store - The Redux store in which the specified action is
 * dispatched.
 * @param {Action} action - The Redux action dispatched in the specified store.
 * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
 * which is being muted or unmuted.
 * @private
 * @returns {void}
 */
function _setMuted(store, action, mediaType: MEDIA_TYPE) {
    const localTrack = _getLocalTrack(store, mediaType);
    localTrack && setTrackMuted(localTrack.jitsiTrack, action.muted);
}
/**
 * Intercepts the action TRACK_UPDATED in order to synchronize the
 * muted states of the local tracks of features/base/tracks with the muted
 * states of features/base/media.
 *
 * @param {Store} store - The Redux store in which the specified action
 * is being dispatched.
 * @param {Dispatch} next - The Redux dispatch function to dispatch the
 * specified action to the specified store.
 * @param {Action} action - The Redux action TRACK_UPDATED which is
 * being dispatched in the specified store.
 * @private
 * @returns {Object} The new state that is the result of the reduction of the
 * specified action.
 */
function _trackUpdated(store, next, action) {
    // Determine the muted state of the local track before the update.
    const track = action.track;
    let mediaType;
    let oldMuted;
    if ('muted' in track) {
        // XXX The return value of JitsiTrack.getType() is of type MEDIA_TYPE
        // that happens to be compatible with the type MEDIA_TYPE defined by
        // jitsi-meet-react.
        mediaType = track.jitsiTrack.getType();
        const localTrack = _getLocalTrack(store, mediaType);
        if (localTrack) {
            oldMuted = localTrack.muted;
        }
    }
    const result = next(action);
    if (typeof oldMuted !== 'undefined') {
        // Determine the muted state of the local track after the update. If the
        // muted states before and after the update differ, then the respective
        // media state should by synchronized.
        const localTrack = _getLocalTrack(store, mediaType);
        if (localTrack) {
            const newMuted = localTrack.muted;
            if (oldMuted !== newMuted) {
                switch (mediaType) {
                case MEDIA_TYPE.AUDIO:
                    store.dispatch(setAudioMuted(newMuted));
                    break;
                case MEDIA_TYPE.VIDEO:
                    store.dispatch(setVideoMuted(newMuted));
                    break;
                }
            }
        }
    }
    return result;
}