// Copyright 2018, University of Colorado Boulder

/**
 * Model for the 'Focus & Directrix' screen.
 *
 * @author Chris Malley (PixelZoom, Inc.)
 */
define( require => {
  'use strict';

  // modules
  const GQModel = require( 'GRAPHING_QUADRATICS/common/model/GQModel' );
  const graphingQuadratics = require( 'GRAPHING_QUADRATICS/graphingQuadratics' );
  const Quadratic = require( 'GRAPHING_QUADRATICS/common/model/Quadratic' );
  const Property = require( 'AXON/Property' );
  const RangeWithValue = require( 'DOT/RangeWithValue' );
  const Vector2 = require( 'DOT/Vector2' );

  class FocusAndDirectrixModel extends GQModel {

    /**
     * @param {Object} [options]
     */
    constructor( options ) {

      options = _.extend( {
        pRange: new RangeWithValue( -9, 9, 2 ),
        hRange: new RangeWithValue( -6, 6, 0 ),
        kRange: new RangeWithValue( -6, 6, 0 ),
        pointX: 5  // default x value for point on quadratic
      }, options );

      assert && assert( !options.quadratic, 'FocusAndDirectrixModel sets quadratic' );
      options.quadratic = Quadratic.createFromAlternateVertexForm(
        options.pRange.defaultValue, options.hRange.defaultValue, options.kRange.defaultValue, {
          color: 'black'
        } );

      super( options );

      // @public (read-only)
      this.pRange = options.pRange;
      this.hRange = options.hRange;
      this.kRange = options.kRange;

      // @public
      this.pointOnQuadraticProperty =
        new Property( new Vector2( options.pointX, this.quadraticProperty.value.solveY( options.pointX ) ) );

      // update the point
      this.quadraticProperty.lazyLink( ( quadratic, oldQuadratic ) => {

        assert && assert( quadratic.vertex, 'null quadratic.vertex not supported' );
        assert && assert( oldQuadratic.vertex, 'null oldQuadratic.vertex not supported' );

        const dx = quadratic.vertex.x - oldQuadratic.vertex.x;
        const x = this.pointOnQuadraticProperty.value.x + dx;
        this.pointOnQuadraticProperty.value = new Vector2( x, quadratic.solveY( x ) );
      } );
    }
  }

  return graphingQuadratics.register( 'FocusAndDirectrixModel', FocusAndDirectrixModel );
} );

// Copyright 2018, University of Colorado Boulder

/**
 * Model for the 'Focus & Directrix' screen.
 *
 * @author Chris Malley (PixelZoom, Inc.)
 */
define( require => {
  'use strict';

  // modules
  const GQModel = require( 'GRAPHING_QUADRATICS/common/model/GQModel' );
  const graphingQuadratics = require( 'GRAPHING_QUADRATICS/graphingQuadratics' );
  const Quadratic = require( 'GRAPHING_QUADRATICS/common/model/Quadratic' );
  const Property = require( 'AXON/Property' );
  const RangeWithValue = require( 'DOT/RangeWithValue' );
  const Vector2 = require( 'DOT/Vector2' );

  class FocusAndDirectrixModel extends GQModel {

    /**
     * @param {Object} [options]
     */
    constructor( options ) {

      options = _.extend( {
        pRange: new RangeWithValue( -9, 9, 2 ),
        hRange: new RangeWithValue( -6, 6, 0 ),
        kRange: new RangeWithValue( -6, 6, 0 ),
        pointX: 5  // default x value for point on quadratic
      }, options );

      assert && assert( !options.quadratic, 'FocusAndDirectrixModel sets quadratic' );
      options.quadratic = Quadratic.createFromAlternateVertexForm(
        options.pRange.defaultValue, options.hRange.defaultValue, options.kRange.defaultValue, {
          color: 'black'
        } );

      super( options );

      // @public (read-only)
      this.pRange = options.pRange;
      this.hRange = options.hRange;
      this.kRange = options.kRange;

      // @public
      this.pointOnQuadraticProperty =
        new Property( new Vector2( options.pointX, this.quadraticProperty.value.solveY( options.pointX ) ) );

      // update the point
      this.quadraticProperty.lazyLink( ( quadratic, oldQuadratic ) => {

        assert && assert( quadratic.vertex, 'null quadratic.vertex not supported' );
        assert && assert( oldQuadratic.vertex, 'null oldQuadratic.vertex not supported' );

        const dx = quadratic.vertex.x - oldQuadratic.vertex.x;
        const x = this.pointOnQuadraticProperty.value.x + dx;
        const y = quadratic.solveY( x );
        this.pointOnQuadraticProperty.value = new Vector2( x, y );
      } );
    }
  }

  return graphingQuadratics.register( 'FocusAndDirectrixModel', FocusAndDirectrixModel );
} );

clean up