'use strict';

var angular = require('angular');
require('../modules');
require('../services/introService');

angular
  .module('intro.directives')
  .directive('sinoIntroPage', sinoIntroPage)
  .controller('SinoIntroPageCtrl', SinoIntroPageCtrl);

/**
 * 介绍项页面
 * 作用范围：
 *   dom的属性
 * 例子：
 *   <div sino-intro-page="xxxId"></div>
 * 参数：
 *   sino-intro-page: 介绍页面的ID， 配置项中的name属性
 * 
 * @ngInject
 */
function sinoIntroPage(introService) {
  
  return {
    restrict: 'EA',
    replace: false,
    controller: 'SinoIntroPageCtrl'
  }
}

function SinoIntroPageCtrl($scope, $element, $attrs, introService) {
  var self = this;
  
  self.putElement = putElement;
  
  var name = $attrs.sinoIntroPage;
  
  $scope.$on('$ionicView.afterEnter', function() {
    if (name) {
      introService.start(name);
    }
  });
  
  function putElement(stepId, element) {
    var option = introService.getOption(name);
    if (option) {
      var steps = option.steps;
      steps.forEach(function(step) {
        if (step.id === stepId) {
          step.element = element;
        }
      });
    }
  }
}