goog.provide('oneup.ui.AdminAchievementsPage');

goog.require('goog.dom');
goog.require('oneup.Achievement');
goog.require('goog.ui.AutoComplete.Basic');

oneup.ui.AdminAchievementsPage = function(args) {
  this.args = args;
  this.achTable = goog.dom.createDom('table', {'class':'adminAchievementsTable'});
  
  var headerRow = goog.dom.createDom('tr');
  headerRow.appendChild(goog.dom.createDom('th', null, 'Id'));
  headerRow.appendChild(goog.dom.createDom('th', null, 'Icon'));
  headerRow.appendChild(goog.dom.createDom('th', null, 'Name'));
  headerRow.appendChild(goog.dom.createDom('th', null, 'Group'));
  headerRow.appendChild(goog.dom.createDom('th', null, 'Actions'));
  this.achTable.appendChild(headerRow);
};

oneup.ui.AdminAchievementsPage.prototype.render = function() {
  var holder = goog.dom.createDom('div');
  
  var newItemHolder = goog.dom.createDom('div', {'class':'newItemBox'});
  newItemHolder.appendChild(goog.dom.createDom('div', {'class':'newItemHeader'}, 'NEW ACHIEVEMENT'));
  newItemHolder.appendChild(goog.dom.createDom('span', null, 'I have '));
  var newItemText = goog.dom.createDom('input');
  newItemHolder.appendChild(newItemText);
  newItemHolder.appendChild(goog.dom.createDom('span', null, 'Group:'));
  var newItemGroup = goog.dom.createDom('select');
  newItemGroup.appendChild(goog.dom.createDom('option', {'value' : 'Travel'}, 'Travel'));
  newItemGroup.appendChild(goog.dom.createDom('option', {'value' : 'Sports, Games and Hobbies'}, 'Sports, Games and Hobbies')); 
  newItemGroup.appendChild(goog.dom.createDom('option', {'value' : 'Arts'}, 'Arts'));
  newItemGroup.appendChild(goog.dom.createDom('option', {'value' : 'Humanities'}, 'Humanities'));
  newItemGroup.appendChild(goog.dom.createDom('option', {'value' : 'Family and Friends'}, 'Family and Friends'));
  newItemGroup.appendChild(goog.dom.createDom('option', {'value' : 'Occupation'}, 'Occupation'));
  newItemGroup.appendChild(goog.dom.createDom('option', {'value' : 'Adult'}, 'Adult'));
  newItemGroup.appendChild(goog.dom.createDom('option', {'value' : 'Food and Drink'}, 'Food and Drink'));
  newItemGroup.appendChild(goog.dom.createDom('option', {'value' : 'Misc'}, 'Misc'));
  newItemHolder.appendChild(newItemGroup);
  var newItemButton = goog.dom.createDom('button', null, 'Add');
  var that = this;
  newItemButton.onclick = function() {that.addItemCallback(newItemText.value, newItemGroup.value)};
  newItemHolder.appendChild(newItemButton);

  holder.appendChild(newItemHolder);

  var achievements = objectToArray(oneup.app.achievements, true);

  for (var i = 0; i < achievements.length; i++) {
    this.addItemRow(achievements[i]);
  }
  holder.appendChild(this.achTable);
  return holder;
}

oneup.ui.AdminAchievementsPage.prototype.addItemCallback = function(name, group) {
  // do datacall
  var that = this;
  oneup.app.dataAccess.newAchievement(name, group, function(response) {
    var id = response.target.getResponseJson().value;
    
    if (id > 0) {
      var ach = new oneup.Achievement({'id':id, 'icon':null, 'group':group, 'name':name});
      oneup.app.achievements[ach.id] = ach;
      oneup.app.achievementGroups[ach.group] = oneup.app.achievementGroups[ach.group] || [];
      oneup.app.achievementGroups[ach.group].push(ach);
      that.addItemRow(ach);
    } else {
      // show error
    };
  })
}

oneup.ui.AdminAchievementsPage.prototype.addItemRow = function(ach) {
  var that = this;
  var row = goog.dom.createDom('tr');
  row.appendChild(goog.dom.createDom('td', null, '' + ach.id));
  row.appendChild(goog.dom.createDom('td', null, '' + ach.icon));
  row.appendChild(goog.dom.createDom('td', null, 'I have ' + ach.name));
  row.appendChild(goog.dom.createDom('td', null, '' + ach.group));
  var actionCell = goog.dom.createDom('td', null);
  var removeButton = goog.dom.createDom('button', null, 'Remove');
  
  removeButton.onclick = function() {
    oneup.app.dataAccess.removeAchievement(ach.id, function() {
      that.achTable.removeChild(row);
      delete oneup.app.achievements[ach.id];
      for (var i = 0; i < oneup.app.achievementGroups[ach.group].length; i++) {
        if (oneup.app.achievementGroups[ach.group][i].id == ach.id) {
          oneup.app.achievementGroups[ach.group].splice(i, 1);
          if (oneup.app.achievementGroups[ach.group].length == 0) {
            delete oneup.app.achievementGroups[ach.group];
          }
        }
      }      
    });
  }
  
  actionCell.appendChild(removeButton);
  row.appendChild(actionCell);
  
  if (this.achTable.firstChild.nextSibling) {
    this.achTable.insertBefore(row, this.achTable.firstChild.nextSibling);
  } else {
    this.achTable.appendChild(row);
  }
}

