/**
* 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";

    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 = {

        /**
        * 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) {
            var trackIds = jamClient.SessionGetIDs();
            var allTracks = jamClient.SessionGetControlState(trackIds);
            var localMusicTracks = [];
            var i;
            var instruments = [];
            var localTrackExists = false;

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

                console.log("allTracks[" + i + "].instrument_id=" + allTracks[i].instrument_id);
                // check if local track config exists
                if (allTracks[i].instrument_id !== 0) {
                    localTrackExists = true;
                }
              }
            }

            var trackObjects = [];

            console.log("localTrackExists=" + localTrackExists);

            // get most proficient instrument from API if no local track config exists
            if (!localTrackExists) {
              if (context.JK.userMe.instruments && context.JK.userMe.instruments.length > 0) {
                  var track = {
                      instrument_id: context.JK.userMe.instruments[0].instrument_id,
                      sound: "stereo"
                  };
                  trackObjects.push(track);

                  var desc = context.JK.userMe.instruments[0].description;
                  jamClient.TrackSetInstrument(1, context.JK.server_to_client_instrument_map[desc]);
                  jamClient.TrackSaveAssignments();
              }
            }
            // use all tracks previously configured
            else {
                console.log("localMusicTracks.length=" + localMusicTracks.length);
                for (i=0; i < localMusicTracks.length; i++) {
                    var track = {};
                    var instrument_description = '';
                    console.log("localMusicTracks[" + i + "].instrument_id=" + localMusicTracks[i].instrument_id);

                    // no instruments configured
                    if (localMusicTracks[i].instrument_id === 0) {
                      if (context.JK.userMe.instruments && context.JK.userMe.instruments.length > 0) {
                          track.instrument_id = context.JK.userMe.instruments[0].instrument_id;
                      }
                      else {
                        track.instrument_id = context.JK.client_to_server_instrument_map[250].server_id;
                      }
                    }
                    // instruments are configured
                    else {
                      if (context.JK.client_to_server_instrument_map[localMusicTracks[i].instrument_id]) {
                        track.instrument_id = context.JK.client_to_server_instrument_map[localMusicTracks[i].instrument_id].server_id;
                      }
                      // fall back to Other
                      else {
                        track.instrument_id = context.JK.client_to_server_instrument_map[250].server_id;
                        jamClient.TrackSetInstrument(i+1, 250);
                        jamClient.TrackSaveAssignments();
                      }
                    }
                    if (localMusicTracks[i].stereo) {
                        track.sound = "stereo";
                    }
                    else {
                        track.sound = "mono";
                    }
                    trackObjects.push(track);
                }
            }
            return trackObjects;
        }
    };

})(window, jQuery);