var angular = require('angular');

require('../modules');
require('../../../commons/services/CacheService');

angular.module('flowmodulebase')
  .directive('sinoModel', function(CacheService, $parse) {
    return {
      restrict: 'A',
      priority: 1,
      scope: true,
      compile: function(tElement, tAttrs) {
        tAttrs.$set('ngModel', tAttrs.sinoModel);
        return function(scope, element, attrs) {
          var name = attrs.sinoModel;
          if(attrs.hasOwnProperty('defaultValue')) {
            scope[name] = CacheService.get(name, attrs.defaultValue);
          } else {
            scope[name] = CacheService.get(name, true);
          }

          scope.$watch(name, function(value) {
            if (value === true || value === false) {
              CacheService.put(name, value);
            }
          });
        }
      }
    };
  });
