'use strict';

/* globals confirm */
/* globals alert */
module.exports =

/*@ngInject*/
function GrammarActivitiesEditCmsCtrl (
  $scope, GrammarActivity, $state, _, ConceptsFBService
) {
  $scope.grammarActivity = {};
  $scope.grammarActivity.concepts = [];

  GrammarActivity.getOneByIdFromFB($state.params.id).then(function (ga) {
    $scope.grammarActivity = ga;
    $scope.grammarActivity.concepts = _.map($scope.grammarActivity.concepts, function (c, k) {
      c.$id = k;
      ConceptsFBService.getById(k).then(function (cfb) {
        if (cfb) {
          c.concept_level_2 = cfb.concept_level_2;
          c.concept_level_1 = cfb.concept_level_1;
          c.concept_level_0 = cfb;
        }
      });
      return c;
    });
  });

  function buildConcepts(set) {
    return _.chain(set)
      .map(function (s) {
        if (s.concept_level_0 && s.concept_level_0.$id) {
          return [s.concept_level_0.$id, {
            quantity: Number(s.quantity),
            ruleNumber: s.concept_level_0.ruleNumber
          }];
        } else {
          if (s.$id && _.isNumber(s.quantity) && _.isNumber(s.ruleNumber)) {
            return [s.$id, {
              quantity: Number(s.quantity),
              ruleNumber: Number(s.ruleNumber)
            }];
          } else {
            return [s.$id, {}];
          }
        }
      })
      .object()
      .value();
  }

  $scope.processGrammarActivityForm = function () {
    var ga = $scope.grammarActivity;
    var id = $state.params.id;
    var updatedGrammarActivity = {
      title: ga.title,
      description: ga.description,
      concepts: buildConcepts(ga.concepts),
      standard: ga.standard,
      standard_level: ga.standard_level,
      topicCategory: ga.topicCategory
    };

    GrammarActivity.updateToFB(id, updatedGrammarActivity).then(function () {
      $state.go('cms-grammar-activities');
    }, function (err) {
      alert(err);
    });
  };

  $scope.confirmDelete = function (id) {
    var d = confirm('Are you absolutely sure you want to delete activity: ' + id + '?');
    if (d) {
      GrammarActivity.deleteByIdFromFB(id).then(function () {
        $state.go('cms-grammar-activities');
      }, function (err) {
        throw err;
      });
    }
  };
};

@@ -177,16 +177,62 @@
 BService
+, Activity,%0A  grammarActivityClassificationUid
 %0A) %7B%0A  $
@@ -1871,24 +1871,525 @@
 nction () %7B%0A
+      var grammarActivityUid = id;%0A      var lmsActivity = new Activity(%7B%0A        uid: grammarActivityUid,%0A        name: updatedGrammarActivity.title,%0A        description: updatedGrammarActivity.description,%0A        topic_uid: updatedGrammarActivity.standard.uid,%0A        activity_classification_uid: grammarActivityClassificationUid%0A      %7D);%0A      if (!lmsActivity.isValid()) %7B%0A        throw new Error(lmsActivity.errorMessages);%0A      %7D%0A      return lmsActivity.create();%0A    %7D).then(function () %7B%0A
       $state
@@ -2420,26 +2420,32 @@
 ies');%0A    %7D
-, 
+).catch(
 function (er

work on putting activity