//# sourceURL=widgets/Sensor.js
// Version 26158

import Widget from '../Widget.js';

var Sensor = function( elem ) {
    Widget.call( this, elem );
    this.name = "Sensor";
    this.entity_id = this.getData( 'sensor', this.entity_id );

    this.value = this.getData( 'value', 'entity.primary_value' );
    this.active_id = this.getData( 'active', false );
    this.active_entity = false;

    $(this.elem).addClass("widget-sensor");
};

Sensor.prototype = Object.create(Widget.prototype);
Sensor.prototype.constructor = Sensor;

Sensor.prototype.startXXX = function() {
    /* If our active state is tracked by a reference/expression, find that
       entity and make us an observer of it. */
    const self = this;
    if ( self.active_id ) {
        self.addObservedEntity( self.active_id, "active_entity" ).catch( function( e ) {
            self.log.warn("Unable to load active entity %1: %2", self.active_id, e);
        });
    }
};

Sensor.prototype.updateXXX = function() {
    let active = this.entity ? this.entity.getAttributeBoolean("state") : false;
    if ( this.active_entity ) {
        const val = this.resolve( this.active_id, this.createContextFromEntity( this.active_entity ) );
        active = this.active_entity.toBooleanState( val, false );
    }

    this.bindValues( this.createContextFromEntity( this.entity ) );

    if ( active ) {
        $(this.elem).addClass("active").removeClass('update-pending');
    } else {
        $(this.elem).removeClass("active update-pending");
    }
};

Sensor.prototype.post_update = async function() {
    const $target = $( '.text-size-elastic', $(this.elem) ).first();
    // console.log( 'Sensor.post_update() on', this, 'to', $target );
    const $body = $( '.widget-body', $(this.elem) );
    //const vpad = $body.height() - 2 * ( $( '.widget-title', $(this.elem) ).height() + 16 );
    const vpad = $body.height();
    const vwid = $body.width();
    $target.css( 'font-size', '' );
    //console.log( this, $target, vwid, vpad, $target.css( 'font-size' ) );
    let size = parseInt( $target.css( 'font-size' ), 10 );
    let high = size;
    let low = 8;
    let count = 0;
    // while ( size > 8 && ( $target.width() > vwid || $target.height() > vpad ) ) {
        // --size;
        // $target.css( 'font-size', `${size}px` );
        // console.log('size now',size,'width',$target.width(),'height',$target.height() );
    // }
    // Better... binary search to find acceptable size.
    while ( count++ < 16 ) {
        if ( $target.width() > vwid || $target.height() > vpad ) {
            // Too large
            //console.log("too large", size, low, high);
            high = size;
            size = Math.floor( (low + size) / 2 );
            if ( size === high || size < 8 ) break;
        } else {
            // Too small
            //console.log("too small", size, low, high);
            low = size;
            size = Math.floor( (high + size) / 2 );
            if ( size === low || size < 8 ) break;
        }
        $target.css( 'font-size', `${size}px` );
        //console.log('size now',size,'width',$target.width(),'height',$target.height() );
    }
}

export default Sensor;
