Promise = require 'bluebird'
fs = Promise.promisifyAll require 'fs-extra'
path = require 'path'
pug = require 'pug'
coffee = require 'coffee-script'
SimplyImport = require 'simplyimport'
ignoreList = require './src/ignores'


fs.readdirAsync("#{__dirname}/src").then (files)->
	files = files.filter (file)-> fs.statSync("#{__dirname}/src/#{file}").isDirectory()
	latestSimplyBind = '0'
	libraries = []
	files = sortFilesByVersion(files).filter (file)-> not ignoreList.includes(file) and not file.includes('node_modules')

	for file in files then do (file=file)->
		directory = title = path.basename(file)
		split = title.split '@'
		title = split[0]
		name = split[0].toLowerCase().replace(/\s+/g, '-')
		version = split[1] or 'N/A'
		location = "#{directory}/runner.html"

		latestSimplyBind = version if name is 'simplybind' and version > latestSimplyBind
		libraries.push {title, version, location}
		
		# ==== Build Test Runners =================================================================================
		fs.readFileAsync("#{__dirname}/src/#{directory}/deps.json", {'encoding':'utf8'}).then (content)->
			deps = JSON.parse(content)

			fs.readFileAsync("#{__dirname}/src/#{directory}/test.coffee", {'encoding':'utf8'}).then (content)->
				content = SimplyImport(content, null, {isStream:true, isCoffee:true})
				compiledJS = coffee.compile content, 'bare':true
				compiledHTML = pug.renderFile "#{__dirname}/src/runner.jade", {title, name, version, deps, pretty:true}

				fs.outputFileAsync "#{__dirname}/dist/#{directory}/test.js", compiledJS
				fs.outputFileAsync "#{__dirname}/dist/#{directory}/runner.html", compiledHTML





	# ==== Build Index =================================================================================
	indexHTML = pug.renderFile "#{__dirname}/src/index.jade", {libraries, latestSimplyBind, ignoreList, pretty:true}

	fs.outputFileAsync "#{__dirname}/dist/index.html", indexHTML




sortFilesByVersion = (files)->
	files.slice().sort (a,b)-> switch
		when parseName(a) isnt parseName(b) then switch
			when a > b then 1
			when a < b then -1
			else 0

		else switch
			when parseVersion(a,'major') > parseVersion(b,'major') then 1
			when parseVersion(a,'major') < parseVersion(b,'major') then -1
			else switch
				when parseVersion(a,'minor') > parseVersion(b,'minor') then 1
				when parseVersion(a,'minor') < parseVersion(b,'minor') then -1
				else switch
					when parseVersion(a,'patch') > parseVersion(b,'patch') then 1
					when parseVersion(a,'patch') < parseVersion(b,'patch') then -1
					when /\w/.test(a) or /\w/.test(b) then switch
						when parseVersion(a,'patch-word') > parseVersion(b,'patch-word') then 1
						when parseVersion(a,'patch-word') < parseVersion(b,'patch-word') then -1
						else 0
					else 0




parseName = (libraryString)->
	libraryString.split('@')[0]

parseVersion = (libraryString, level)->
	versionString = libraryString.split('@')[1] or ''
	versionBreakdown = versionString.split('.')

	switch level
		when 'major' then parseFloat(versionBreakdown[0]) or 0
		when 'minor' then parseFloat(versionBreakdown[1]) or 0
		when 'patch' then parseFloat(versionBreakdown[2]) or 0
		when 'patch-word' then versionBreakdown[2].split(/^\d+/)[1]



