var fs = require('fs-extra');
var uuid = require('uuid');
var path = require('path');
var cloudscraper = require('cloudscraper');
var readChunk = require('read-chunk');
var fileType = require('file-type');
var request = require('request');
var ytdl = require('ytdl-core');
var Readable = require('stream').Readable;

module.exports = (lib) => {
	lib.trigger.add({
		name: 'parser',
		call: /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi,
		level: 0,
		active: 1,
		func: (e, userlevel) => {
			if(e.channel.getName() === lib.cfg.main.debugchannel || e.message.match(/(!|-)f0ck/i)) {
				if(!e.message.match(/(!|-)ignore/)) {
					var tmp = e.message.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links
					tmp.forEach((entry,i,a) => {
						if(!entry.match(/f0ck\.me/i) && !entry.match(/\.onion/i)) {
							getLink(entry, ((e.message.match(/(!|-)force/i) && userlevel >= 100)?true:false), userlevel, (cb) => {
								if(cb.success === true) {
									fs.move(cb.file, cb.file + '.' + cb.info.ext, (err) => {
										if(!err) {
											lib.bot.whois(e.user.getNick(), e.network, (err, cbgu) => {
												lib.sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`,`thumb`) values (?,?,?,?,?,?,?,?,?,?,?)", [
													entry,
													cb.file + '.' + cb.info.ext,
													cb.info.mime,
													cb.size,
													cb.checksum,
													cbgu['nick'],
													e.channel.getName(),
													e.network,
													Math.floor(new Date() / 1000),
													0,
													cb.info.thumb ? cb.info.thumb : ''
												]).on('result', (result) => {
													lib.generateThumbs();
													e.reply(lib.cfg.main.url+"/"+result.insertId+" - "+cb.info.title+" ("+cb.info.mime+", ~"+lib.formatSize(cb.size)+") by "+cbgu['nick']+" ("+cbgu['username']+"@"+cbgu['hostname']+")");
												}).on('error', (msg) => {
													e.reply(msg);
												});
											});
										}
									});
								}
								else {
									fs.stat('./b/' + cb.file, (err, stat) => {
										if(cb.msg !== '')
											e.reply(cb.msg);
										if(!err && stat.isFile())
											fs.unlinkSync('./b/' + cb.file);
									});
								}
							});
						}
					});
				}
			}
		},
		desc: 'muh'
	});
	
	var getLink = (url, force, userlevel, cb) => {
		var yt = /https?:\/\/(www\.)?youtu(\.be\/|be\.com\/)((.+\/)?(watch(\?v=|.+&v=))?(v=)?)([\w_-]{11})(&.+)?/gi;
		var sc = /https?:\/\/(www\.)?(soundcloud\.com|snd\.sc)(\/\S*)(\/\S*)/gi;
		lib.checkRepost(url, (cbcr) => {
			var tmpdest = uuid.v1().split('-')[0];
			if(cbcr === true) {
				var dat = fs.createWriteStream('./b/' + tmpdest);
				var info;
				if(url.match(yt)) { // ytdl
					ytdl.getInfo(url, (err, inf) => {
						if(!err) {
							var title = inf.title;
							var iurl = JSON.parse(inf.player_response).videoDetails.thumbnail.thumbnails[0].url.split('?')[0];
							try {
								dlformat = { filter: (format) => { return format.container === 'webm'; } };
								ytdl.downloadFromInfo(inf, dlformat)
									.on('response', (res) => {
										if( ( res.headers['content-length'] <= lib.cfg.main.maxFileSize ) || force || ( userlevel >= 60 && res.headers['content-length'] <= (lib.cfg.main.maxFileSize * 2) ) ) {
											info = {
												type: 'youtube',
												title: title,
												mime: 'video/webm',
												ext: 'webm',
												thumb: iurl
											};
										}
										else {
											res.destroy();
											dat.end();
											cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(res.headers['content-length'])+'), max '+lib.formatSize( ( userlevel >= 60?(lib.cfg.main.maxFileSize*2):lib.cfg.main.maxFileSize ) )+' allowed' });
										}
									})
									.on('error', (err) => {
										dat.end();
										cb({ success: false, file: tmpdest, msg: err.message });
									})
									.pipe(dat);
							}
							catch(ex) {
								dat.end();
								cb({ success: false, file: tmpdest, msg: ex.message });
							}
						}
					});
				}
				else if(url.match(sc)) { // scdl
					request('https://api.soundcloud.com/resolve.json?client_id=' + lib.cfg.main.scclientid + '&url=' + url, (err, res, body) => {
						if(!err && res.statusCode === 200) {
							var data = JSON.parse(body);
							request(data.stream_url + ((data.stream_url.indexOf('?') === -1)?'?':'&') + 'client_id=' + lib.cfg.main.scclientid)
								.pipe(dat);
							info = {
								type: 'soundcloud',
								title: data.title,
								mime: 'audio/mpeg',
								ext: 'mp3',
								thumb: (data.artwork_url !== null)?data.artwork_url.replace('large.jpg', 't300x300.jpg'):null
							};
						}
						else {
							dat.end();
							cb({ success: false, file: tmpdest, msg: 'f0ck sc-api' });
						}
					});
				}
				else { // various
					cloudscraper.request({
						method: 'GET',
						url: url,
						encoding: null,
					},
					(err, res, data) => {
						if(!err) {
							var type = res.headers['content-type'].split(";")[0];
							lib.log('MimeType: '+type);
							if(lib.cfg.main.allowedMimes.hasOwnProperty(type)) {
								if( ( data.length <= lib.cfg.main.maxFileSize ) || force || ( userlevel >= 60 && data.length <= (lib.cfg.main.maxFileSize * 2) ) ) {
									var s = new Readable;
									s.push(data);
									s.push(null);
									s.pipe(dat);
									info = {
										type: 'other',
										title: path.parse(url).base,
										mime: type,
										ext: lib.cfg.main.allowedMimes[type],
										thumb: null
									};
								}
								else {
									dat.end();
									cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(data.length)+'), max '+lib.formatSize( ( userlevel >= 60?(lib.cfg.main.maxFileSize*2):lib.cfg.main.maxFileSize ) )+' allowed' });
								}
							}
							else {
								dat.end();
								cb({ success: false, file: tmpdest, msg: '' });
							}
						}
						else {
							dat.end();
							cb({ success: false, file: tmpdest, msg: err });
						}
					});
				}
				
				dat
					.on('finish', () => {
						var size = dat.bytesWritten;
						dat.end();
						if( ( size <= lib.cfg.main.maxFileSize ) || force || ( userlevel >= 60 && size <= (lib.cfg.main.maxFileSize * 2) ) ) {
							fs.stat('./b/' + tmpdest, (err, stat) => {
								if(!err && stat.isFile() && stat.size > 300) {
									lib.log('Datei '+tmpdest+' existiert');
									lib.getCheckSum('./b/' + tmpdest, (cbcs) => {
										lib.checkRepostCheckSum(cbcs, (cbcrcs) => {
											if(cbcrcs === true) {
												var mime = fileType(readChunk.sync('./b/' + tmpdest, 0, 262));
												info.ext = mime.ext;
												info.mime = mime.mime;
												if(lib.cfg.main.allowedMimes.hasOwnProperty(mime.mime) || info.type === 'soundcloud')
													cb({ success: true, info: info, size: size, file: './b/' + tmpdest, checksum: cbcs });
												else
													cb({ success: false, file: tmpdest, msg: 'lol, go f0ck yourself ('+mime.mime+')' });
											}
											else
												cb({ success: false, file: tmpdest, msg: 'repost motherf0cker: '+lib.cfg.main.url+'/'+cbcrcs });
										});
									});
								}
							});
						}
						else {
							cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(size)+'), max '+lib.formatSize( ( userlevel >= 60?(lib.cfg.main.maxFileSize*2):lib.cfg.main.maxFileSize ) )+' allowed' });
						}
					})
					.on('error', (err) => {
						cb({ success: false, file: tmpdest, msg: err });
					});
			}
			else
				cb({ success: false, file: tmpdest, msg: 'repost motherf0cker: '+lib.cfg.main.url+'/'+cbcr });
		});
	};
};
