var _ = require("underscore");
var Cookie = require("cookie");
var Util = require("util");

var agreePrefix = 'cmtagr';
var disagreePrefix = 'cmtdagr';

class Request {
	constructor(opts) {
		this.domain = opts.domain;
		this.url = opts.url;
	}

	getData(req, params, options = {}) {
		var REQ_URL = 'http://comment3.duowan.com/index.php';
		var location = window.location;

		if (!params) {
			params = {};
		}

		params.domain = options.domain || this.domain;
		params.url = options.url || this.url;
		
		var defer = $.Deferred();

		$.ajax({
			url: REQ_URL + '?r=' + req,
			data: params,
			dataType: 'jsonp'
		}).done(function() {
			defer.resolveWith(this, arguments);
		}).fail(function() {
			defer.rejectWith(this, arguments);
		});

		setTimeout(function() {
			defer.notify("timeout");
		}, 10000);

		return defer;
	}

	totaljson(uniqId) {
		return this.getData("comment/totaljson", uniqId ? {uniqid: uniqId} : null);
	}

	getLoginInfo() {
		return this.getData("default/login", null);
	}

	setNickname(nickname) {
		return this.getData("default/nickname", {nickname: nickname}, {domain: true});
	}

	addComment(uniqId, key, content, parentId) {
		var params = {uniqid: uniqId};
		params[key] = content;
		if (parentId) {
			params.parent_id = parentId;
		}
		params.title = document.title;
		return this.getData("comment/add", params);
	}

	preaudit(uniqId) {
		return this.getData("comment/peraudit", {uniqid: uniqId}, {domain: true, url: true});
	}

	comments(uniqId, order, num) {
		var params = {
			order: order || "time",
			noimg: true
		};
		if (uniqId) {
			params.uniqid = uniqId;
		}
		if (num) {
			params.num = num;
		}
		return this.getData("comment/comment", params).then(function(data) {
			_.each(data, function(d) {
				if (Cookie.get(agreePrefix + d.comment_id)) {
					d.agreed = true;
				}
				if (Cookie.get(disagreePrefix + d.comment_id)) {
					d.disagreed = true;
				}
			});

			return data;
		});
	}

	moreReplies(uniqId, parentId, createTimeStr, flagId) {
		return this.getData("comment/reply", {
			parent_id: parentId,
			uniqid: uniqId,
			create: createTimeStr,
			flag_id: flagId
		});
	}

	agree(uniqId, cid) {
		return this.getData("comment/support", {
			uniqid: uniqId,
			cid: cid
		}).then(function(data) {
			if (data.vote > 0) {
				Cookie.set(agreePrefix + cid, 1, {expires: Util.getDateTomorrow()});
			} else if (data.vote < 0) {
				Cookie.remove(agreePrefix + cid);
			}
			return data;
		});
	}

	disagree(uniqId, cid) {
		return this.getData("comment/oppose", {
			uniqid: uniqId,
			cid: cid
		}, {domain: true, url: true}).then(function(data) {
			if (data.vote > 0) {
				Cookie.set(disagreePrefix + cid, 1, {expires: Util.getDateTomorrow()});
			} else if (data.vote < 0) {
				Cookie.remove(disagreePrefix + cid);
			}
			return data;
		});
	}

	tuCao(uniqId) {
		return this.getData("comment/tucao", {uniqid: uniqId});
	}

	addTuCao(uniqId, contentKey, content) {
		var params = {
			uniqid: uniqId
		};
		params[contentKey] = content;
		params.title = document.title;
		return this.getData("comment/addtucao", params);
	}

	tuCaoVote(uniqId, tucaoId) {
		return this.getData("comment/tucaovote", {
			uniqid: uniqId,
			tucao_id: tucaoId
		}, {domain: true, url: true});
	}

	del(cid, uniqId) {
		return this.getData("default/delComment", {
			id: cid,
			uniqid: uniqId
		}, {domain: true, url: true});
	}
}

module.exports = Request;