/*
 * Copyright 2007-2017 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
 * This file is part of Pydio.
 *
 * Pydio is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Pydio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Pydio.  If not, see <http://www.gnu.org/licenses/>.
 *
 * The latest code can be found at <https://pydio.com>.
 */

(function(global){

    let pydio = global.pydio;
    let MessageHash = global.pydio.MessageHash;

    class Model{

        constructor(){
            this.usage = '';
            this.total = '';
            this.startListening();

            pydio.observe('repository_list_refreshed', function(){
                if(!this.quotaEnabled() && this._pe){
                    this._pe.stop();
                }else{
                    this.loadQuota();
                }
            }.bind(this));
        }

        quotaEnabled(){
            return !! pydio.getController().getActionByName('monitor_quota');
        }

        getText(){
            if(!this.usage || !this.quotaEnabled()){
                return '';
            }
            return PathUtils.roundFileSize(this.usage, MessageHash["byte_unit_symbol"]) + "/"
                + PathUtils.roundFileSize(this.total, MessageHash["byte_unit_symbol"]);
        }

        getUsage(){
            return this.usage;
        }

        getTotal(){
            return this.total;
        }

        static getInstance(){
            if(!Model._INSTANCE){
                Model._INSTANCE = new Model();
            }
            return Model._INSTANCE;
        }

        loadQuota(){
            if(!this.quotaEnabled()) return;
            PydioApi.getClient().request({get_action:'monitor_quota'}, function(transport){
                if(!this.quotaEnabled()) return;
                const data = transport.responseJSON;
                this.usage = data.USAGE;
                this.total = data.TOTAL;
            }.bind(this));
        }

        startListening(){
            var configs = pydio.getPluginConfigs("mq");
            if(configs){
                pydio.observe("server_message", function(event){
                    var newValue = XMLUtils.XPathSelectSingleNode(event, "/tree/metaquota");
                    if(newValue){
                        this.usage = parseInt(newValue.getAttribute("usage"));
                        this.total = parseInt(newValue.getAttribute("total"));
                    }
                });
            }else{
                this._pe = new PeriodicalExecuter(function(pe){
                    if(!this.quotaEnabled()) {
                        pe.stop();
                        return;
                    }
                    this.loadQuota();
                }, 20);
            }
            this.loadQuota();
        }

    }

    class Callbacks{

        static computeQuota(manager, args){

        }
        
        

    }

    class Listeners {

        static init() {
            Model.getInstance();
        }

    }

    const QuotaPanel = React.createClass({

        render: function(){
            let model = Model.getInstance();
            return (
                <PydioWorkspaces.InfoPanelCard title={this.props.pydio.MessageHash['meta.quota.4']} icon="speedometer" iconColor="#1565c0">
                    <div>{model.getText()}</div>
                    <div style={{paddingBottom: 20}}>
                        <MaterialUI.LinearProgress mode="determinate" min={0} max={model.getTotal()} value={model.getUsage()}/>
                    </div>
                </PydioWorkspaces.InfoPanelCard>
            );
        }

    }) ;

    global.QuotaActions = {
        Callbacks: Callbacks,
        Listeners:Listeners,
        Model: Model,
        QuotaPanel: QuotaPanel
    };

})(window)
@@ -926,16 +926,35 @@
 ss Model
+ extends Observable
 %7B%0A%0A     
@@ -967,24 +967,45 @@
 structor()%7B%0A
+            super();%0A
             
@@ -2418,16 +2418,55 @@
 .TOTAL;%0A
+                this.notify('update');%0A
         
@@ -2948,24 +2948,71 @@
 (%22total%22));%0A
+                        this.notify('update');%0A
             
@@ -3369,32 +3369,216 @@
 a();%0A        %7D%0A%0A
+        getState()%7B%0A            return %7B%0A                text: this.getText(),%0A                total: this.getTotal(),%0A                usage: this.getUsage()%0A            %7D;%0A        %7D%0A%0A
     %7D%0A%0A    class
@@ -3774,20 +3774,20 @@
 %7D%0A%0A    c
-onst
+lass
  QuotaPa
@@ -3794,55 +3794,474 @@
 nel 
-= React.createClass(%7B%0A%0A        render: function
+extends React.Component%7B%0A%0A%0A        constructor(props)%7B%0A            super(props);%0A            const model = Model.getInstance();%0A            this.state = model.getState();%0A            this._observer = () =%3E %7B%0A                this.setState(model.getState());%0A            %7D;%0A            model.observe('update', this._observer);%0A        %7D%0A%0A        componentWillUnmount()%7B%0A            Model.getInstance().stopObserving('update', this._observer);%0A        %7D%0A%0A        render
 ()%7B%0A
@@ -4808,11 +4808,8 @@
    %7D
-) ;
 %0A%0A  

Fix quota not updating on workspace switch