/*
 * Opensocial - users colloboration system
 * author: Feng Jimin
 */

var globalGivenGifts = {};
var globalViewer = {};
var globalFriends = {};
var options = ['Give a detailed problem description?', 'Call the area expert for help.', 'Consider alternative options.', 'Just leave it as it is.'];

function postActivity(nut, friend) {
  var title = globalViewer.getDisplayName() + ' told ' + globalFriends[friend] + ' to ' + options[nut];
  var params = {};
  params[opensocial.Activity.Field.TITLE] = title;
  var activity = opensocial.newActivity(params);
  opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH, function() {});
}

function updateStatus() {
  var locData = document.getElementById('loc').value;
  var taskData = document.getElementById('task').value;
  var statusData = document.getElementById('mystatus').value;
  
  var nut = document.getElementById('comment').value;
  var friend = document.getElementById('person').value;
  globalGivenGifts[friend] = nut;
  var json = gadgets.json.stringify(globalGivenGifts);
  
  var req = opensocial.newDataRequest();
  req.add(req.newUpdatePersonAppDataRequest(opensocial.DataRequest.PersonId.VIEWER, 'comments', json));
  req.add(req.newUpdatePersonAppDataRequest(opensocial.DataRequest.PersonId.VIEWER, 'loc', locData));
  req.add(req.newUpdatePersonAppDataRequest(opensocial.DataRequest.PersonId.VIEWER, 'task', taskData));
  req.add(req.newUpdatePersonAppDataRequest(opensocial.DataRequest.PersonId.VIEWER, 'status', statusData));
	
  req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
  req.add(req.newFetchPeopleRequest('VIEWER_FRIENDS'), 'viewerFriends');
  req.add(req.newFetchPersonAppDataRequest('VIEWER', 'comments'), 'comments');
  req.add(req.newFetchPersonAppDataRequest('VIEWER_FRIENDS', 'comments'), 'viewerFriendComments');
  req.add(req.newFetchPersonAppDataRequest('VIEWER', 'loc'), 'loc');
  req.add(req.newFetchPersonAppDataRequest('VIEWER_FRIENDS', 'loc'), 'viewerFriendLoc');
  req.add(req.newFetchPersonAppDataRequest('VIEWER', 'task'), 'task');
  req.add(req.newFetchPersonAppDataRequest('VIEWER_FRIENDS', 'task'), 'viewerFriendTask');
  req.add(req.newFetchPersonAppDataRequest('VIEWER', 'status'), 'status');
  req.add(req.newFetchPersonAppDataRequest('VIEWER_FRIENDS', 'status'), 'viewerFriendStatus');  
  req.send(onLoadFriends);
}

function makeOptionsMenu() {                 
  var html = new Array();
  html.push('<select id="comment">');
  for (var i = 0; i < options.length; i++) {
    html.push('<option value="' + i + '">' + options[i] + '</option>');
  }
  html.push('</select>');
  document.getElementById('comments').innerHTML = html.join('');
}

function loadFriends() {
  var req = opensocial.newDataRequest();
  req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
  req.add(req.newFetchPeopleRequest('VIEWER_FRIENDS'), 'viewerFriends');
  req.add(req.newFetchPersonAppDataRequest('VIEWER', 'comments'), 'comments');
  req.add(req.newFetchPersonAppDataRequest('VIEWER_FRIENDS', 'comments'), 'viewerFriendComments');
  req.add(req.newFetchPersonAppDataRequest('VIEWER', 'loc'), 'loc');
  req.add(req.newFetchPersonAppDataRequest('VIEWER_FRIENDS', 'loc'), 'viewerFriendLoc');
  req.add(req.newFetchPersonAppDataRequest('VIEWER', 'task'), 'task');
  req.add(req.newFetchPersonAppDataRequest('VIEWER_FRIENDS', 'task'), 'viewerFriendTask');
  req.add(req.newFetchPersonAppDataRequest('VIEWER', 'status'), 'status');
  req.add(req.newFetchPersonAppDataRequest('VIEWER_FRIENDS', 'status'), 'viewerFriendStatus');  
  req.send(onLoadFriends);
}

function onLoadFriends(data) {
  var viewer = globalViewer = data.get('viewer').getData();
  var viewerFriends = data.get('viewerFriends').getData();
  var commentsData = data.get('comments').getData();
  var viewerFriendCommentsData = data.get('viewerFriendComments').getData();
  var locData = data.get('loc').getData();
  var viewerFriendLocData = data.get('viewerFriendLoc').getData();
  var taskData = data.get('task').getData();
  var viewerFriendTaskData = data.get('viewerFriendTask').getData();
  var statusData = data.get('status').getData();
  var viewerFriendStatusData = data.get('viewerFriendStatus').getData();  
  
  html = new Array();
  html.push('<select id="person">');
  viewerFriends.each(function(person) {
    html.push('<option value="' + person.getId() + '">' + person.getDisplayName() + "</option>");
  });
  html.push('</select>');
  document.getElementById('friends').innerHTML = html.join('');
  
  // Print the status and comments of viewer's
  var statusHtml = new Array();
  statusHtml.push('<li>'+ viewer.getDisplayName() +" "+ '<font color="blue">' + locData[viewer.getId()]['loc'] +'</font>' + " " +
			'<font color="green">' + taskData[viewer.getId()]['task'] +'</font>' + " " +
			'<font color="red">' + statusData[viewer.getId()]['status'] +'</font>'+'<br>' + "&nbsp;&nbsp;&nbsp;Comments: ");
  viewerFriends.each(function(person) {
	var personData = viewerFriendCommentsData[person.getId()];
	if (personData) {
      var json = viewerFriendCommentsData[person.getId()]['comments'];
    
      var comments = {}
      if (!json) {
        comments = {};
      }
      try {
        comments = gadgets.json.parse(gadgets.util.unescapeString(json));
      } catch (e) {
        comments = {};
      }
    
      for (i in comments) {
        if (+(i) > 0 && i == viewer.getId()) {
          statusHtml.push('<font color="red">'+options[comments[i]] + '</font>' + ' from ' + person.getDisplayName() + '<br>');
        }
      }
    } // end of if
  });
  
  // Print the status and comments of viewer friends'
  viewerFriends.each(function(person){
	var vfloc = "Undefined";
	var vftask = "Undefined";
	var vfstatus = "Undefined";
	
    if(viewerFriendLocData[person.getId()])	vfloc = viewerFriendLocData[person.getId()]['loc'];
	if(viewerFriendTaskData[person.getId()])	vftask = viewerFriendTaskData[person.getId()]['task'];
	if(viewerFriendStatusData[person.getId()])	vfstatus = viewerFriendStatusData[person.getId()]['status'];
	
    statusHtml.push('<li>' + person.getDisplayName() + " "+ '<font color="blue">' + vfloc +'</font>' + " " + '<font color="green">' + vftask +'</font>' + " " + '<font color="red">' + vfstatus +'</font>'+'<br>' + "&nbsp;&nbsp;&nbsp;Comments: ");
	var personData = commentsData[viewer.getId()];
	if(personData) {
		var json = commentsData[viewer.getId()]['comments'];
		var comments = {}
      if (!json) {
        comments = {};
      }
      try {
        comments = gadgets.json.parse(gadgets.util.unescapeString(json));
      } catch (e) {
        comments = {};
      }
    
      for (i in comments) {
		  if (+(i) > 0 && i == person.getId()) {
          	statusHtml.push('<font color="red">'+ options[comments[i]] +'</font>'+ ' from ' + viewer.getDisplayName() + '<br>');
        }
	  }
	} // end of if
	
	viewerFriends.each(function(one) {
	var oneData = viewerFriendCommentsData[one.getId()];
	if (oneData) {
      var json = viewerFriendCommentsData[one.getId()]['comments'];
    
      var comments = {}
      if (!json) {
        comments = {};
      }
      try {
        comments = gadgets.json.parse(gadgets.util.unescapeString(json));
      } catch (e) {
        comments = {};
      }
    
      for (i in comments) {
        if (+(i) > 0 && i == person.getId()) {
          statusHtml.push('<font color="red">'+options[comments[i]] +'</font>'+ ' from ' + one.getDisplayName() + '<br>');
        }
      } // end of for
    } // end of if
  });
	
  });
  
  document.getElementById('printStatus').innerHTML = statusHtml;
  
}

function init() {
  makeOptionsMenu();
  loadFriends();
}

/*
var globalHtml = '<div id="give"> <form id="gift_form"> Give <span id="gifts"></span> to <span id="friends"></span>. <a href="javascript:void(0);" onclick="giveGift();">Give!</a> </form>   </div> <div id="given"></div> <div id="received"</div>';
document.getElementById('dom_handle').innerHTML = globalHtml;
var newHtml = '<h4>Team Current Status</h4>	<div id="printStatus"></div> <div id="update">	<h4>Update Your Status</h4>	<form id="Supdate">	<table>  <tr> <td>Location</td><td>Task</td><td>Status</td>	</tr> <tr> <td> <input id="loc", onfocus="this.select()", value="Yio Chu Kang",size="14"></input></td>  <td><input id="task", onfocus="this.select()", value="Development", size="14"></input></td> <td><input id="mystatus", onfocus="this.select()", value="Debugging",size="14"></input></td> </tr></table>	<a href="javascript:void(0);" onclick="updateStatus();">Click to Update!</a> <br><br>  Give Comments: <span id="comments"></span> to <span id="friends"></span>  <a href="javascript:void(0);" onclick="giveComments();">Give!</a></form></div>';
document.getElementById('dom_handle').innerHTML = newHtml;
*/