var angular = require('angular');
require('../modules');
require('../../../commons/services/fileService');
require('../services/SinoFileService');

var module = angular.module('flowmodulebase.directives');

/**
 * 查看原文组件
 * <sino-flow-file fileName="{{official.content}}" title="{{official.title}}" showTitle="{{showTitle}}"></sino-flow-file>
 *
 */
module.directive('sinoFile', function(SinoFileService, fileService, $ionicModal) {
  function showTable(element, scope) {
    var modalScope = scope.$new();
    modalScope.closeModal = function() {
      modal.hide();
      modal.remove();
    };

    var modal = $ionicModal.fromTemplate('<ion-modal-view>\
      <ion-header-bar class="bar-dark">\
        <button class="button icon-left ion-ios-arrow-back button-clear" sino-modal-back-button="closeModal()">返回</button>\
        <h1 class="title">查看表格</h1>\
      </ion-header-bar>\
      <ion-content scroll="true" direction="xy">' +
        element.outerHTML +
      '</ion-content></ion-modal-view>', {
      scope: modalScope
    });
    modal.show();
  }

  function getTableElement(element, parent) {
    parent = parent || document.body;
    while (element !== parent) {
      if (element.tagName.toLowerCase() === 'table') {
        return element;
      }
      element = element.parentElement;
    }
    return null;
  }

    return {
        restrict: 'E',
        replace: true,
        scope: {
          fileName:'=',
          title: '=',
          showTitle: '=',
          cut:'=?',
          notShowButton:'=?',//是否显示【查看原文】按钮
          inArchives:'=?'//是否是收文的正文获取
    },
    templateUrl: 'views/flowmodulebase/sinoFile.tpl.html',
        link: function(scope, element) {
          scope.showTitle = scope.showTitle ? scope.showTitle : '查看原文';
          scope.yuanTitle = '查看正文';
          scope.isHasHongFile = false;
          scope.fileNameHong = scope.fileName + 'copy';

          scope.onContentClick = function($event) {
            var tableElement = getTableElement($event.srcElement, element[0]);
            if (tableElement) {
              showTable(tableElement, scope);
            }
          };

          scope.$watch('::fileName', function() {
            if (scope.fileName) {
              var filePromise;
              SinoFileService.isHasHongFile(scope.fileNameHong).then(function(falg) {
                if(falg) {//有红头文件
                  scope.isHasHongFile = true;
                }
              });

              if(scope.inArchives) {
                filePromise = SinoFileService.getInFileContentByFileName(scope.fileName);
              } else {
                filePromise = SinoFileService.getFileContentByFileName(scope.fileName);
              }
              filePromise.then(function(response) {
                if (!response.content) {
                  element[0].querySelector('.sino-file-content').innerHTML = '';
                  element[0].querySelector('.sino-file-link').style.display = 'block';
                  if(scope.notShowButton) {
                    element[0].querySelector('.sino-file-link').style.display = 'none';
                  }
                  return;
                }
                if(scope.cut) {
                  response.content = response.content.substring(0,parseInt(scope.cut));
                }
                var content = response.content;
                var content = response.content.split('\n')
                .map(function(text) {return '<p>' + text + '</p>';})
                .join('');

                element[0].querySelector('.sino-file-content').innerHTML = content;
                element[0].querySelector('.sino-file-link').style.display = 'block';
                if(scope.notShowButton) {
                  element[0].querySelector('.sino-file-link').style.display = 'none';
                }
                scope.fileType = response.fileType;
                scope.lastModifiedDate = response.lastModifiedDate;
                scope.$broadcast('$$sinodataloaded');
              });

            }
          });
        }
    };
});
