﻿"use strict";

if (!Poseidon.Controls) {
    Poseidon.Controls = {};
}

Poseidon.Controls.MeasureArea = Ext.extend(Ext.Window, {
    //height: 200,
    autoHeight: true,
    width: 250,
    resizable: false,
    closable: true,
    closeAction: 'hide',
    resetText: 'Nulstil',
    measureLabel: 'Opmåling',

    //numberOfDecimalsMeters: 0,

    //numberOfDecimalsKiloMeters: 3,

    initComponent: function () {
        var measureItem = new Ext.BoxComponent({
            fieldLabel: this.measureLabel,
            anchor: '100%',
            style: 'color: #000000; text-align: right; border: 1px solid #000000; background-color: #f0f0f0; padding-right: 2px;',            
            html: '&nbsp;'
        });

        var config = {
            measureItem: measureItem,
            items:
            [
                {
                    xtype: 'container',
                    layout: 'form',
                    style: 'padding: 5px;',
                    items:
                    [
                        {
                            xtype: 'box',
                            html: this.description + '<br><br>',
                            anchor: '100%'
                        }, {
                            xtype: 'box',
                            html: '<hr>',
                            anchor: '100%'
                        },
                        measureItem
                    ]
                }
            ],
            buttons:
            [
                {
                    text: this.resetText,
                    handler: function () {
                        this.measureItem.update('&nbsp;');
                        this.control.cancel();
                        this.enable();
                    },
                    scope: this
                }
            ]
        };
        Ext.apply(this, Ext.apply(this.initialConfig, config));
        Poseidon.Controls.MeasureArea.superclass.initComponent.apply(this, arguments);
    },

    //function to handle the Measurements
    handleMeasurements: function (event) {
        //var that = Poseidon.MeasureTool;
        var units = event.units,
            order = event.order,
            measure = event.measure,
            currentNumberOfDecimals = 0,
            measurement = "";

        if (units === 'm')
            currentNumberOfDecimals = this.numberOfDecimalsMeters;
        else
            currentNumberOfDecimals = this.numberOfDecimalsKiloMeters;

        if (order === 1) {
            measurement += dot2comma(measure.toFixed(currentNumberOfDecimals)) + " " + units;
        } else {
            measurement += dot2comma(measure.toFixed(currentNumberOfDecimals)) + " " + units + "<sup>2</sup>";
        }
        this.measureItem.update(measurement);
    },

    enable: function () {
        this.control.events.register('measure', this, this.handleMeasurements);
        this.control.events.register('measurepartial', this, this.handleMeasurements);
    },

    disable: function () {
        if (this.control) {
            this.control.events.unregister('measure', this, this.handleMeasurements);
            this.control.events.unregister('measurepartial', this, this.handleMeasurements);
        }
    },

    hide: function () {
        // Call parent hide.
        Poseidon.Controls.MeasureArea.superclass.hide.call(this);

        // Reset the measurement.
        this.measureItem.update('&nbsp;');

        // Toggle toolbar.
        var button = Ext.getCmp(this.toolBarItem);
        if (button.pressed === true) {
            button.toggle();
        }
    }
});
