/*
 * 提交官网代码到svn,便于发布
 * */
var path = require('path');
var settings = require('../../conf/settings');
var _ = require('../../lib/util');
var jsdom = require('jsdom');
module.exports = function (hoo, root, media, title,releaseSource) {

  var projectFiles = hoo.get('project.files');
  projectFiles.pop();
  projectFiles.pop();
  projectFiles.pop();
  projectFiles.push('/opening/*.html');
  hoo.set('project.files', projectFiles);

  var hooMatches = hoo.media(media).match('**', {
    useHash: !!releaseSource ? false : true,
    useCache: false
  })
    .match('/opening/*.html', {
      isInline: false,
      release: '$0'
    })
    .match('/css/main/**.{css,scss}', {
      packTo: '/static/bundle.css'
    })
    .match('/css/app/**.{css,scss}', {
      useSprite: true
    })
    .match('/static/bundle.css', {
      useSprite: true
    })
    .match('**.{png,jpg,mp4}', {
      domain: "http://" + path.join(settings.wwwDeployDomain, root).replace(/\\/g, '/')
    })
    .match('/opening/**', {
      domain: false
    })
    .match('/opening/**/(*.{css,scss})', {
      domain: '/static',
      release: path.join('css', root, '1/') + '$1$2'
    })
    .match('/opening/**/(*.{png,jpg})', {
      domain: '/static',
      release: path.join('images', root, '1/') + '$1$2'
    })
    .match('/css/images/extras/**', {
      useHash: false
    })
    .match('::package', {
      packager: hoo.plugin('map', {
        useTrack: false// 是否输出路径信息,默认为 true
      }),
      // 分析 __RESOURCE_MAP__ 结构，来解决资源加载问题
      postpackager: hoo.plugin('loader', {
        resourceType: 'amd',
        useInlineMap: true
      }, 'prepend')
    })
    .match('**', {
      deploy: [
        hoo.plugin('skip-packed', {
          // 配置项
          skipPackedToPkg: true,//是否过滤掉被打包的文件
          skipPackedToAIO: true,//是否过滤掉被 AllInOne 打包的文件
          skipPackedToCssSprite: true,//是否过滤掉被 css sprite 合并的文件。
          ignore: []//即便设置了过滤，但是如果目标文件 ignore 列表中，还是不会被过滤。glob 语法，数组或者字符串。
        }),
        function (options, modified, total, next) {
          var i = modified.length - 1;
          var file;
          while ((file = modified[i--])) {
            if (file.isHtmlLike) {
              modified.splice(i + 1, 1)
            }
          }
          next();
        },
        function (options, modified, total, next) {

          var img1Files = [];
          var openingFiles = [path.join('/', settings.openingTemplatePath, root, 'index.htm')];

          for (var key in modified) {
            if (modified[key].id.indexOf('opening') === 0 && !modified[key].isHtmlLike) {
              if (modified[key].map && modified[key].map.cssspritePkg) {

              } else {
                openingFiles.push(modified[key].getHashRelease());
              }
            } else if (!modified[key].isHtmlLike) {
              if (modified[key].map && modified[key].map.cssspritePkg) {

              } else {
                img1Files.push(modified[key].getHashRelease());
              }
            }
          }

          var img1Dir = [];
          var openingDir = [];

          for (var i = 0; i < img1Files.length; i++) {
            _.insertDir(img1Dir, path.dirname(img1Files[i]).substring(1));
          }

          for (var i = 0; i < openingFiles.length; i++) {
            _.insertDir(openingDir, path.dirname(openingFiles[i]).substring(1));
          }

          img1Dir = _.pull(_.uniq(img1Dir), '.');
          openingDir = _.pull(_.uniq(openingDir), '.');

          var toAddImg1Files = img1Dir.map(function (file) {
            return {
              path: path.join(root, file), dir: true
            }
          }).concat(img1Files.map(function (file) {
              return {
                path: path.join(root, file), dir: false
              }
            }
          ));

          var toAddOpeningFiles = openingDir.map(function (file) {
            return {
              path: file, dir: true
            }
          }).concat(openingFiles.map(function (file) {
              return {
                path: file.substring(1), dir: false
              }
            }
          ));

          require('../../lib/wwwSubmit')(hoo, toAddImg1Files, toAddOpeningFiles, root, title).then(function () {
            console.log('-----------------结束分割线------------------');
          });
        }
      ]
    });

  //不混淆和压缩代码，提供清晰格式的代码给合作平台开发者使用
  if (!releaseSource) {
    hooMatches.match('*.{css,scss}', {
      optimizer: hoo.plugin('clean-css', {})
    })
      .match('*.js', {
        optimizer: hoo.plugin('uglify-js')
      })
  }
};
