(function () {
    'use strict';

    const ds = require('data-service');

    /**
     * @param {Base} Base
     * @param {JQuery} $element
     * @param {$state} $state
     * @param {$location} $location
     * @param {User} user
     * @param {$rootScope.Scope} $scope
     * @param {IPollCreate} createPoll
     * @param {Waves} waves
     * @return {DexCtrl}
     */
    const controller = function (Base, $element, $state, $location, user, $scope, createPoll, waves) {

        class DexCtrl extends Base {

            /**
             * @type {string}
             */
            _titleTxt;

            constructor() {
                super($scope);
                /**
                 * @type {boolean}
                 */
                this.ready = false;
                /**
                 * @type {boolean}
                 */
                this.isLogined = !!user.address;
                /**
                 * @type {boolean}
                 * @private
                 */
                this._leftHidden = false;
                /**
                 * @type {boolean}
                 * @private
                 */
                this._rightHidden = false;
                /**
                 * @type {{price: string, amount: string}}
                 * @private
                 */
                this._assetIdPair = null;


                this.syncSettings({
                    tab: 'dex.layout.bottomleft.tab',
                    _leftHidden: 'dex.layout.leftColumnState',
                    _rightHidden: 'dex.layout.rightColumnState',
                    _assetIdPair: 'dex.assetIdPair'
                });

                if (!this.isLogined) {
                    this.tab = 'tradeHistory';
                }

                const matcherSign = () => {
                    return user.address ? user.addMatcherSign() : Promise.resolve();
                };

                matcherSign()
                    .catch(() => Promise.resolve())
                    .then(() => {
                        this._initializePair().then(() => {
                            this.ready = true;
                            $scope.$apply();
                        });
                    });

                createPoll(this, this._getLastPrice, '_titleTxt', 1000);

                this.observe('_assetIdPair', this._onChangePair);
                this.observe('_titleTxt', this._setTitle);
                this.observe(['_leftHidden', '_rightHidden'], this._onChangeProperty);
            }

            $onDestroy() {
                super.$onDestroy();
                window.document.title = 'Waves Client';
            }

            // hide and show graph to force its resize
            toggleColumn(column) {
                this[`_${column}Hidden`] = !this[`_${column}Hidden`];
            }

            /**
             * @private
             */
            async _onChangePair() {
                const pair = await this._getPair();
                $location.search('assetId2', pair.amountAsset.id);
                $location.search('assetId1', pair.priceAsset.id);
            }

            /**
             * @return {Promise}
             * @private
             */
            _getLastPrice() {
                return this._getPair().then(pair => {
                    return waves.matcher.getLastPrice(pair).then(data => {
                        const pairText = `| ${pair.amountAsset.displayName}/${pair.priceAsset.displayName}`;
                        const priceText = data.price.isNaN() ? '-' : data.price.toFormat();
                        return `${priceText} ${pairText}`;
                    });
                });
            }

            /**
             * @return {Promise}
             * @private
             */
            _initializePair() {
                return new Promise((resolve) => {
                    setTimeout(() => {
                        const urlPair = this._getPairFromState();
                        if (urlPair) {
                            return this._getPair(urlPair)
                                .catch(() => this._getPair({
                                    amount: WavesApp.defaultAssets.WAVES,
                                    price: WavesApp.defaultAssets.BTC
                                }))
                                .then((pair) => {
                                    const activeTab = user.getSetting('dex.watchlist.activeTab');

                                    if (activeTab !== 'all' &&
                                        activeTab !== pair.amountAsset.id &&
                                        activeTab !== pair.priceAsset.id) {
                                        user.setSetting('dex.watchlist.activeTab', 'all');
                                    }

                                    this._assetIdPair = {
                                        amount: pair.amountAsset.id,
                                        price: pair.priceAsset.id
                                    };
                                })
                                .then(resolve);
                        } else {
                            this._onChangePair();
                            resolve();
                        }
                    }, 200);
                });
            }

            /**
             * @private
             */
            _getPair(pair = this._assetIdPair) {
                if (pair) {
                    return ds.api.pairs.get(pair.amount, pair.price);
                } else {
                    return ds.api.pairs.get(WavesApp.defaultAssets.WAVES, WavesApp.defaultAssets.BTC);
                }
            }

            /**
             * @private
             */
            _getPairFromState() {
                if (!($state.params.assetId1 && $state.params.assetId2)) {
                    return null;
                }

                return {
                    amount: $state.params.assetId1,
                    price: $state.params.assetId2
                };
            }

            /**
             * @private
             */
            _onChangeProperty() {
                const $graphWrapper = $element.find('.graph-wrapper');
                $graphWrapper.hide();
                setTimeout(() => $graphWrapper.show(), 100);
            }

            /**
             * @private
             */
            _setTitle() {
                window.document.title = this._titleTxt;
            }

        }

        return new DexCtrl();
    };


    controller.$inject = ['Base', '$element', '$state', '$location', 'user', '$scope', 'createPoll', 'waves'];

    angular.module('app.dex')
        .controller('DexCtrl', controller);
})();

@@ -3295,87 +3295,8 @@
 %3E %7B%0A
-                    return waves.matcher.getLastPrice(pair).then(data =%3E %7B%0A    
     
@@ -3392,24 +3392,99 @@
 playName%7D%60;%0A
+                    return waves.matcher.getLastPrice(pair).then(data =%3E %7B%0A
             
@@ -3524,16 +3524,28 @@
 a.price.
+getTokens().
 isNaN() 
@@ -3630,24 +3630,103 @@
 pairText%7D%60;%0A
+                    %7D, () =%3E %7B%0A                        return %60- $%7BpairText%7D%60;%0A
             

fix title