'use strict';

var gulp = require('gulp');
var through = require('through2');
var path = require('path');
var _ = require('lodash');
var glob = require('glob');
var fs = require('fs');
var pkg = JSON.parse(fs.readFileSync(path.join(__dirname,'../package.json')));
var getFileHeader = require('./getFileHeader');

var $ = require('gulp-load-plugins')({
	pattern: ['gulp-*', 'del']
});


module.exports = function(options) {
	function templating(files,folder,templateDir,init='',data={},main=false){
		return function tpl(){
			var template = String(fs.readFileSync(templateDir));

			var footer = _.template(String(fs.readFileSync('src/partials/footer.tpl')))({
				version:pkg.version,
				init,
			});

			var header = _.template(String(fs.readFileSync('src/partials/header.tpl')))({
				init,
				version:pkg.version,
				home:main,
			});

			return gulp.src(files)
			.pipe(through.obj(function (file, enc, callback) {
				var postHeader = ''
				var content = String(file.contents);

				var data = getFileHeader(content);

				content =
				(data.title ? `<h1>${data.title}</h1>` : '')+`
				`+(data.header ? '<%= postHeader %>' : '')+`
				`+content;

				if(data.header){
					postHeader = _.template(String(fs.readFileSync('src/partials/post-header.tpl')))({
						data,
					});
				}

				content = _.template(content)({
					data,
					postHeader,
				})
				.replace(/<!-- header\n(.|\n)*?\nheader -->/g,'')

				var newContent = _.template(template)({
					content,
					header,
					init,
					data,
					footer,
					version:pkg.version,
				});
				file.contents = new Buffer(newContent);

				callback(null,file);
			}))
			.pipe($.if(main, $.replace('<base href="../../../">', '')))
			.pipe($.rename(function (path) {
				path.extname = ".html"
				if(main) path.basename = "index"
			}))
			.pipe(gulp.dest(folder));
		}
	}

	var getPosts = function(dir=''){
		return _.map(glob.sync(dir),function(file){
			// var p = path.parse(file)
			// String(fs.readFileSync())


			// return p.name;
		});
	}

	// console.log(getPosts('src/portfolio-posts/*.md'))

	gulp.task('clean:siteTmp', function (done) {
		return $.del([
			// dist+'/',
			options.tmp + '/site/portfolio-posts',
			options.tmp + '/site/posts',
		],{force:true});
	});

	_.each([
		{
			dest:'.',
			name:':dist',
			init:'',
			template:options.tmp + '/site/injected.tpl'
		},
		{
			dest:options.tmp + '/site',
			init:'',
			name:'',
			template:options.tmp + '/site/injected.tpl'
		},
	],function(val,i){
		gulp.task('template:portfolio'+val.name,gulp.series(templating(
			options.tmp + '/site/portfolio-posts/**/*.html',
			val.dest+'/portfolio-posts/',
			val.template,
			{

			},
			val.init
		)));

		gulp.task('template:posts'+val.name,gulp.series(templating(
			options.tmp + '/site/posts/**/*.html',
			val.dest+'/posts/',
			val.template,
			{
				// posts:
			},
			val.init
		)));

		gulp.task('template:mainPage'+val.name,gulp.series(templating(
			options.tmp + '/site/partials/main.html',
			val.dest+'/',
			val.template,
			val.init,
			true
		)));

		gulp.task('template'+val.name,gulp.series(
			'clean:siteTmp',
			'markdown',
			'template:mainPage'+val.name,
			'template:portfolio'+val.name,
			'template:posts'+val.name
		));
	})

};

@@ -454,28 +454,8 @@
 der,
-templateDir,init='',
 data
@@ -541,19 +541,42 @@
 ync(
-templateDir
+options.tmp + '/site/injected.tpl'
 ));%0A
@@ -682,26 +682,16 @@
 ersion,%0A
-%09%09%09%09init,%0A
 %09%09%09%7D);%0A%0A
@@ -771,26 +771,16 @@
 l')))(%7B%0A
-%09%09%09%09init,%0A
 %09%09%09%09vers
@@ -1473,19 +1473,8 @@
 er,%0A
-%09%09%09%09%09init,%0A
 %09%09%09%09
@@ -2270,175 +2270,57 @@
 ,%0A%09%09
-%09init:'',%0A%09%09%09template:options.tmp + '/site/injected.tpl'%0A%09%09%7D,%0A%09%09%7B%0A%09%09%09dest:options.tmp + '/site',%0A%09%09%09init:'',%0A%09%09%09name:'',%0A%09%09%09template:options.tmp + '/site/injected.tpl'
+%7D,%0A%09%09%7B%0A%09%09%09dest:options.tmp + '/site',%0A%09%09%09name:'',
 %0A%09%09%7D
@@ -2499,45 +2499,15 @@
 %0A%09%09%09
-val.template,%0A%09%09%09%7B%0A%0A%09%09%09%7D,%0A%09%09%09val.init
+%7B%0A%0A%09%09%09%7D
 %0A%09%09)
@@ -2638,33 +2638,16 @@
 osts/',%0A
-%09%09%09val.template,%0A
 %09%09%09%7B%0A%09%09%09
@@ -2661,29 +2661,16 @@
 ts:%0A%09%09%09%7D
-,%0A%09%09%09val.init
 %0A%09%09)));%0A
@@ -2801,38 +2801,8 @@
 /',%0A
-%09%09%09val.template,%0A%09%09%09val.init,%0A
 %09%09%09t

simplify templating