import _ from "underscore";
import Cookie from "cookie";

export default class{
	constructor(opts) {
		this.apiURL = `//bbs.duowan.com`;
    }

    getData(url, params) {
		if (!params) {
			params = {};
		}

		let defer = $.Deferred();

		$.get({
			url: url,
			data: params,
		}).done(function() {
			defer.resolveWith(this, arguments);
		}).fail(function() {
			defer.rejectWith(this, arguments);
		});

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

		return defer;
	}

	postData(url, params) {
		if (!params) {
			params = {};
		}
		url = this.apiURL + url
		let defer = $.Deferred();

		$.ajax({
			url: url,
			dataType:"jsonp",
			crossDomain: true,
			xhrFields: { withCredentials: true },
			data: params,
		}).done(function() {
			defer.resolveWith(this, arguments);
		}).fail(function() {
			defer.rejectWith(this, arguments);
		});

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

		return defer;
	}

	getJsonp(url, jsonp) {
		if(!jsonp){
			jsonp = 'jsonp'
		}
		let params = {};

		let defer = $.Deferred();

		$.ajax({
			jsonpCallback: jsonp,
			dataType: 'jsonp',
			url: url,
			data: params,
		}).done(function() { 
			defer.resolveWith(this, arguments);
		}).fail(function() {
			defer.rejectWith(this, arguments);
		});

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

		return defer;
	}

	getPrimaryData(){
		let url = '//www.duowan.com/s/bbs/api.json'
		return this.getJsonp(url)
	}

	getNewGameData(){
		let url = '//newgame.duowan.com/s/bbs/api.json'
		return this.getJsonp(url,'cb')
	}

	getKaData(){
		let url = '//ka.duowan.com/open/newGifts'
		return this.getData(url)
	}
	
	getSyData() {
		let url = '//sy.duowan.com/open/getNewGame'
		let params = {time:parseInt(new Date().getTime() / 1000)}
		return this.getData(url,params)
		
	}


	jsonp(url, param) {
		if(!param){
			param = {}
		}

		let defer = $.Deferred();

		$.ajax({
			dataType: 'jsonp',
			url: url,
			data: param,
		}).done(function() { 
			defer.resolveWith(this, arguments);
		}).fail(function() {
			defer.rejectWith(this, arguments);
		});

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

		return defer;
	}
	


}
