'use strict';
define(function() {
  var wysiwig = function(
    ParametersFactory,
    taRegisterTool,
    taOptions,
    $compile
  ) {
    return {
      templateUrl: 'js/XG/widgets/mapapp/customforms/views/wysiwig.html',

      restrict: 'A',

      link: function(scope, elt, attrs) {
        taRegisterTool('colourRed', {
          iconclass: 'fa fa-square red',
          action: function() {
            this.$editor().wrapSelection('COUCOUC', '<embed></embed>', true);
          },
        });
        taOptions.toolbar[1].push('colourRed');

        scope.formsname = attrs['formsname'];
        console.log(scope);
        scope.savefroms = function() {
          var html = { html: scope.htmlVariable };
          ParametersFactory.add(html, 'Forms', scope.formsname).then(
            function() {
              console.log('formulaire sauvé');
            }
          );
        };
        scope.$watch('htmlVariable', function() {
          scope.htmlVariable = $compile(scope.htmlVariable)(scope);
        });
      },
    };
  };

  wysiwig.$inject = [
    'ParametersFactory',
    'taRegisterTool',
    'taOptions',
    '$compile',
  ];
  return wysiwig;
});
