/**
* Functions related to VU meters.
* These functions are intimately tied to the markup defined in the
* VU templates in _vu_meters.html.erb
*/
(function(context, $) {

    "use strict";

    var ChannelGroupIds = context.JK.ChannelGroupIds

    context.JK = context.JK || {};

    // As these are helper functions, just have a single
    // static object with functions. Each function should
    // take all necessary arguments to complete its work.
    context.JK.TrackHelpers = {

        getTrackInfo: function(jamClient, masterTracks) {

          if(masterTracks === undefined) {
              masterTracks = context.jamClient.SessionGetAllControlState(true);
          }

          var userTracks = context.JK.TrackHelpers.getUserTracks(jamClient, masterTracks);
          var backingTracks = context.JK.TrackHelpers.getBackingTracks(jamClient, masterTracks);
          var metronomeTracks = context.JK.TrackHelpers.getTracks(jamClient, ChannelGroupIds.MetronomeGroup);

          return {
            userTracks: userTracks,
            backingTracks: backingTracks,
            metronomeTracks: metronomeTracks
          }
        },

        // allTracks is the result of SessionGetAllControlState; as an optimization
        getTracks: function(jamClient, groupId, allTracks) {
            var tracks = [];

            if(!allTracks) {
              allTracks = context.jamClient.SessionGetAllControlState(true);
            }
            //var trackIds = jamClient.SessionGetIDs();
            //var allTracks = jamClient.SessionGetControlState(trackIds, true);

            // get client's tracks
            for (var i=0; i < allTracks.length; i++) {
                if (allTracks[i].group_id === groupId) {
                    tracks.push(allTracks[i]);
                }
            }

            return tracks;
        },

      // allTracks is the result of SessionGetAllControlState; as an optimization
        getBackingTracks: function(jamClient, allTracks) {
            var mediaTracks  = context.JK.TrackHelpers.getTracks(jamClient, ChannelGroupIds.MediaTrackGroup, allTracks);

            var backingTracks = []
            context._.each(mediaTracks, function(mediaTrack) {
                // the check for 'not managed' means this is not a track opened by a recording, basically
                // we do not try and sync these sorts of backing tracks to the server, because they
                // are already encompassed by
                if(mediaTrack.media_type == "BackingTrack" && !mediaTrack.managed) {
                    var track = {};
                    track.client_track_id = mediaTrack.persisted_track_id;
                    track.client_resource_id = mediaTrack.rid;
                    track.filename = mediaTrack.filename;
                    backingTracks.push(track);
                }
            })

            return backingTracks;
        },

        /**
        * This function resolves which tracks to configure for a user
        * when creating or joining a session. By default, tracks are pulled
        * from jamClient. If none exist there, the first instrument from the
        * user's profile is used.
        */
        getUserTracks: function(jamClient, allTracks) {
            var localMusicTracks = [];
            var localMidiTracks = [];
            var i;

            localMusicTracks = context.JK.TrackHelpers.getTracks(jamClient, ChannelGroupIds.AudioInputMusicGroup, allTracks);
            localMidiTracks = context.JK.TrackHelpers.getTracks(jamClient, ChannelGroupIds.MidiInputMusicGroup, allTracks);

            localMusicTracks = localMusicTracks.concat(localMidiTracks)

            var trackObjects = [];

            for (i=0; i < localMusicTracks.length; i++) {
                var track = {};
                track.client_track_id = localMusicTracks[i].id;
                track.client_resource_id = localMusicTracks[i].rid;

                if(localMusicTracks[i].instrument_id === 0) {
                  track.instrument_id =  context.JK.server_to_client_instrument_map["Other"].server_id;
                }
                else {

                  var instrument = context.JK.client_to_server_instrument_map[localMusicTracks[i].instrument_id]
                  if (instrument) {
                      track.instrument_id = instrument.server_id
                  }
                    else {
                      logger.debug("backend reported an invalid instrument ID of " + localMusicTracks[i].instrument_id)
                      track.instrument_id = 'other'
                  }

                }
                if (localMusicTracks[i].stereo) {
                    track.sound = "stereo";
                }
                else {
                    track.sound = "mono";
                }
                trackObjects.push(track);
            }
            return trackObjects;
        }
    };

})(window, jQuery);