const fs = require("fs")
const constants = require("../utils/constants")
const path = require("path")
const cacheManager = require("../eirtubeMods/cache-manager")

let maxVideoCacheSize = constants.server_setup.video_cache_max_size
let targetVideoCacheSize = 0.75 * maxVideoCacheSize;
let maxJsonCacheSize = constants.server_setup.cache_json_max_size
let targetJsonCacheSize = 0.75 * maxJsonCacheSize;

function scanFolder(folder) {
	let totalSize = 0
	let files = []
	for (let file of fs.readdirSync(folder)) {
		if (!fs.existsSync(path.join(folder, file)))
			continue

		let stats = fs.lstatSync(path.join(folder, file))
		if (stats.isDirectory())
			continue
		files.push({name: file, size: stats.size, age: stats.atimeMs})
		totalSize += stats.size
	}

	return { totalSize, files }
}



let scanCachesTimeout
function scanCaches() {
	// Video cache
	let vidCacheData = scanFolder(constants.server_setup.video_dl_path)
	vidCacheData.files = vidCacheData.files.filter(f => {
		const nameInQueue = f.name.split(".mp4")[0].replace("-temp", "").replace("-audio", "")
		const dlStatus = cacheManager.read("download")[nameInQueue]
		return (dlStatus == undefined || dlStatus != 2)
	})

	if (vidCacheData.totalSize > maxVideoCacheSize) {
		// vidCacheData.files = vidCacheData.files.sort((a, b) => b.size - a.size)
		// vidCacheData.files = vidCacheData.files.sort((a, b) => (b.size - b.age) - (a.size - a.age))		//vidCacheData.files = vidCacheData.files.sort((a, b) => b.age - a.age)
		vidCacheData.files = vidCacheData.files.sort((a, b) => a.age - b.age)

		let i = 0;
		let filesToRemove = []
		while (vidCacheData.totalSize >= targetVideoCacheSize) {
			let file = vidCacheData.files[i];
			filesToRemove.push(path.join(constants.server_setup.video_dl_path, file.name));
			vidCacheData.totalSize -= file.size
			i++;
		}

		console.log(`Removing ${filesToRemove.length} files from video cache:`);
		console.log(filesToRemove);

		for (let file of filesToRemove)
			fs.unlinkSync(file)
	}

	// Json cache files
	let jsonCacheData = scanFolder(constants.server_setup.json_cache_path)

	if (jsonCacheData.totalSize > maxJsonCacheSize) {
		jsonCacheData.files = jsonCacheData.files.sort((a, b) => b.size - a.size)

		let i = 0;
		let cachesToClear = []
		while (jsonCacheData.totalSize >= targetJsonCacheSize) {
			let file = jsonCacheData.files[i];
			cachesToClear.push(path.join(constants.server_setup.json_cache_path, file.name));
			jsonCacheData.totalSize -= file.size
			i++;
		}

		console.log(`Emptying ${cachesToClear.length} json files in json cache:`);
		console.log(cachesToClear);

		for (let cache of cachesToClear)
			cacheManager.clean(cache)
	}

	// Repeat cleanup later
	clearTimeout(scanCachesTimeout)
	scanCachesTimeout = setTimeout(scanCaches, constants.server_setup.time_between_cache_cleanup)
}
scanCaches()

// Clear ogg cache on startup
for (let file of fs.readdirSync(constants.server_setup.ogg_dl_path)) {
	if (!fs.existsSync(path.join(constants.server_setup.ogg_dl_path, file)))
		continue

	let stats = fs.lstatSync(path.join(constants.server_setup.ogg_dl_path, file))
	if (stats.isDirectory())
		continue

	if (file.endsWith(".ogg"))
		fs.unlinkSync(path.join(constants.server_setup.ogg_dl_path, file))
}

module.exports = {
	scanCaches
}
