goog.provide('Syncer');
goog.require('api.Bitbucket');
goog.require('utils.parser');



/**
 * @constructor
 */
var Syncer = function() {
	this._url = '';
	this._api = {
		bitbucket: new api.Bitbucket
	};

	this._init();
};


/**
 * @param {string} url
 */
Syncer.prototype.setUrl = function(url) {
	this._url = url;
	this._redmineTicket = utils.parser.getTicket(url);
};


/**
 * @return {IThenable.<string>}
 */
Syncer.prototype.getRedMineTicketUrl = function() {
	return new Promise(function(resolve, reject) {
		chrome.tabs.getSelected(function(tab) {
			var tabUrl = tab.url;
			var ticket = utils.parser.findTicket(tabUrl);
			var ticketUrl = utils.parser.joinUrl('https://dev.ifaced.ru/issues', ticket);
			resolve(ticketUrl);
		});
	});
};


/**
 * @return {Array.<models.bitbucket.Branch>}
 */
Syncer.prototype.getBitbucketBranches = function() {
	return this._api.bitbucket.getBranches();
};


/**
 * @return {IThenable.<string>}
 */
Syncer.prototype.getBitbucketPullRequestUrl = function() {
	return this._api.bitbucket
		.getPullRequests()
		.then(function(pulls) {
			var pullUrl = pulls.filter(function(pull) {
				pull = /** @type {models.bitbucket.PullRequest} */(pull);
				return pull.title.indexOf(this._redmineTicket) !== -1 ||
					pull.source.branch.name.indexOf(this._redmineTicket) !== -1;
			}, this);
			return pullUrl[0].links.html.href;
		}.bind(this));
};


/**
 * @param {string} token
 */
Syncer.prototype.setBitbucketToken = function(token) {
	this._token.bitbucket = token;
};


/**
 * @return {Promise.<string>}
 */
Syncer.prototype.getRedmineProjectId = function() {
	return utils.parser.redmine.getProjectId();
};


Syncer.prototype._init = function() {
	chrome.storage.sync.get(null, function(items) {
		var settings = JSON.parse(items.settings);

		Object.keys(settings).forEach(function(el, i) {
			this._api.bitbucket.setOwner(el);

			for (var prop in settings[el]) {
				this._api.bitbucket.setRepo(settings[el][prop]);
			}
		}, this);
	}.bind(this));
};


/**
 * @type {string}
 */
Syncer.prototype._url;


/**
 * @type {string}
 */
Syncer.prototype._redmineTicket;


/**
 * @type {{
 *      bitbucket: string,
 *      redmine: string
 * }}
 */
Syncer.prototype._token;


/**
 * @type {{
 *      bitbucket: api.Bitbucket
 * }}
 */
Syncer.prototype._api;

@@ -211,32 +211,152 @@
 s._init();%0A%7D;%0A%0A%0A
+/**%0A */%0ASyncer.prototype.goToRedmineTicket = function() %7B%0A%09this.getRedMineTicketUrl().then(this._goto.bind(this));%0A%7D;%0A%0A%0A
 /**%0A * @param %7Bs
@@ -366,16 +366,16 @@
 ng%7D url%0A
-
  */%0ASync
@@ -1492,32 +1492,523 @@
 nd(this));%0A%7D;%0A%0A%0A
+/**%0A * @return %7BIThenable.%3CArray.%3Cmodels.bitbucket.PullRequest%3E%3E%7D%0A */%0ASyncer.prototype.getBitbucketPullRequests = function() %7B%0A%09return this._api.bitbucket%0A%09%09.getPullRequests()%0A%09%09.then(function(pulls) %7B%0A%09%09%09return pulls.filter(function(pull) %7B%0A%09%09%09%09var isTargetPullRequest = pull.title.indexOf(this._redmineTicket) !== -1;%0A%09%09%09%09var isTargetBranch = pull.source.branch.name.indexOf(this._redmineTicket) !== -1;%0A%09%09%09%09return isTargetPullRequest %7C%7C isTargetBranch;%0A%09%09%09%7D, this);%0A%09%09%7D.bind(this));%0A%7D;%0A%0A%0A
 /**%0A * @param %7Bs
@@ -2583,16 +2583,16 @@
  this);%0A
-
 %09%7D.bind(
@@ -2596,32 +2596,142 @@
 nd(this));%0A%7D;%0A%0A%0A
+/**%0A * @param %7Bstring%7D url%0A * @protected%0A */%0ASyncer.prototype._goto = function(url) %7B%0A%09window.open(url);%0A%7D;%0A%0A%0A
 /**%0A * @type %7Bst

add Syncer.prototype.goToRedmineTicket(), Syncer.prototype.getBitbucketPullRequests(), Syncer.prototype._goto()