const Lang = imports.lang;

// network requests
const Soup = imports.gi.Soup;
const Json = imports.gi.Json;

let DesktopperAdapter = new Lang.Class({
	Name: "DesktopperAdapter",
	/*
	 fetch a random image url from desktopper.cc
	 and call callback function with the URL of the image
	 */
	requestRandomImage: function (callback) {
		let session = new Soup.SessionAsync();
		let message = Soup.Message.new('GET', 'https://api.desktoppr.co/1/wallpapers/random');

		let parser = new Json.Parser();

		var _this = this;

		session.queue_message(message, function (session, message) {
			parser.load_from_data(message.response_body.data, -1);

			let data = parser.get_root().get_object();
			let response = data.get_object_member('response');
			let imageUrl = response.get_object_member('image').get_string_member('url');

			if (callback) {
				callback(imageUrl);
			}
		});
	}
});

let WallheavenAdapter = new Lang.Class({
	Name: "WallheavenAdapter",

	// query options
	options: {
		'q': '',
		'purity': '110', // SFW, sketchy
		'sorting': 'random',
		'category': '111', // General, Anime, People
		'resolutions': ['1920x1200', '2560x1440']
	},

	/*
	 fetch a random image url from wallheaven.cc with the given options
	 and call callback function with the URL of the image
	 */
	requestRandomImage: function (callback) {
		let session = new Soup.SessionAsync();

		let options = this.options;
		let optionsString = "";

		for (var key in options) {
			if (options.hasOwnProperty(key)) {
				if (Array.isArray(options[key])) {
					optionsString += key + "=" + options[key].join() + "&";
				} else {
					optionsString += key + "=" + options[key] + "&";
				}
			}
		}
		// remove last '&'
		optionsString = optionsString.slice(0, -1);

		let url = 'http://alpha.wallhaven.cc/search?' + optionsString;

		let message = Soup.Message.new('GET', url);

		var _this = this;

		session.queue_message(message, function (session, message) {
			let body = message.response_body.data;
			let urlArray = body.match(new RegExp(/http[s]*:\/\/alpha.wallhaven.cc\/wallpaper\/[0-9]+/g));

			// remove dublicates from array
			let uniqueUrlArray = urlArray.filter(function (item, pos) {
				return urlArray.indexOf(item) == pos;
			});

			// get a random entry from the array
			var url = uniqueUrlArray[Math.floor(Math.random() * uniqueUrlArray.length)];

			message = Soup.Message.new('GET', url);

			session.queue_message(message, function () {
				let body = message.response_body.data;
				let imageUrl = body.match(new RegExp(/\/\/wallpapers.wallhaven.cc\/wallpapers\/full\/.*?"/))[0];
				imageUrl = imageUrl.slice(0, -1);
				imageUrl = 'http:' + imageUrl;

				if (callback) {
					callback(imageUrl);
				}
			})


		});
	}
});

@@ -19,16 +19,80 @@
 ts.lang;
+%0Aconst Self = imports.misc.extensionUtils.getCurrentExtension();
 %0A%0A// net
@@ -166,16 +166,324 @@
 .Json;%0A%0A
+const LoggerModule = Self.imports.logger;%0A%0Alet BaseAdapter = new Lang.Class(%7B%0A%09Name: %22BaseAdapter%22,%0A%0A%09logge: null,%0A%0A%09_init: function () %7B%0A%09%09this.logger = new LoggerModule.Logger('RWG3', 'BaseAdapter');%0A%09%7D,%0A%0A%09requestRandomImage: function () %7B%0A%09%09this.logger.error(%22requestRandomImage not implemented%22)%0A%09%7D%0A%7D);%0A%0A
 let Desk
@@ -543,16 +543,39 @@
 apter%22,%0A
+%09Extends: BaseAdapter,%0A
 %09/*%0A%09 fe
@@ -1339,16 +1339,39 @@
 dapter%22,
+%0A%09Extends: BaseAdapter,
 %0A%0A%09// qu

Extend adapters from BaseAdapter