define(['context', 'ui/dynamicComponent', 'ui/maths/calculator/core/board', 'core/pluginLoader', 'css!ui/maths/calculator/css/calculator.css'], function (context, dynamicComponent, calculatorBoardFactory, pluginLoaderFactory, calculator_css) { 'use strict';

    context = context && Object.prototype.hasOwnProperty.call(context, 'default') ? context['default'] : context;
    dynamicComponent = dynamicComponent && Object.prototype.hasOwnProperty.call(dynamicComponent, 'default') ? dynamicComponent['default'] : dynamicComponent;
    calculatorBoardFactory = calculatorBoardFactory && Object.prototype.hasOwnProperty.call(calculatorBoardFactory, 'default') ? calculatorBoardFactory['default'] : calculatorBoardFactory;
    pluginLoaderFactory = pluginLoaderFactory && Object.prototype.hasOwnProperty.call(pluginLoaderFactory, 'default') ? pluginLoaderFactory['default'] : pluginLoaderFactory;

    /**
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * as published by the Free Software Foundation; under version 2
     * of the License (non-upgradable).
     *
     * This program 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 General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     *
     * Copyright (c) 2018-2023 Open Assessment Technologies SA ;
     */

    /**
     * Default config values
     * @type {Object}
     */
    const defaultConfig = {
      preserveAspectRatio: false,
      width: 240,
      height: 360,
      minWidth: 190,
      minHeight: 240,
      alternativeTemplate: null
    };

    /**
     * Creates a dynamic panel containing a calculator.
     * @param {Object} config - Some config entries (@see ui/dynamicComponent)
     * @param {Object} [config.calculator] - Config for the calculator (@see ui/maths/calculator/core/board)
     * @param {Object} [config.loadedPlugins] - a collection of already loaded plugins
     * @param {Object} [config.dynamicPlugins] - a collection of plugins to load
     * @returns {dynamicComponent}
     */
    function calculatorComponentFactory(config) {
      let calculator;
      const api = {
        /**
         * Gets the nested calculator
         * @returns {calculator}
         */
        getCalculator() {
          return calculator;
        }
      };
      const calculatorComponent = dynamicComponent(api, defaultConfig).on('rendercontent', function onRenderContent($content) {
        const initialWidth = this.getElement().width();
        const initialHeight = this.getElement().height();
        return pluginLoaderFactory(this.getConfig().loadedPlugins).addList(this.getConfig().dynamicPlugins).load(context.bundle).then(loadedPlugins => {
          return new Promise(resolve => {
            calculator = calculatorBoardFactory($content, loadedPlugins, this.getConfig().calculator).on('ready', () => {
              var initialFontSize = parseInt(this.getCalculator().getElement().css('fontSize'), 10) || 10;
              this.on('resize', () => {
                if (this.getElement()) {
                  this.getCalculator().getElement().css('fontSize', initialFontSize * Math.min(this.getElement().width() / initialWidth, this.getElement().height() / initialHeight));
                }
              }).setContentSize(calculator.getElement().outerWidth(), calculator.getElement().outerHeight()).setState('ready').trigger('ready');
              resolve();
            });
          });
        });
      }).on('destroy', () => {
        return new Promise(resolve => {
          if (calculator) {
            calculator.after('destroy', () => {
              calculator = null;
              resolve();
            }).destroy();
          } else {
            resolve();
          }
        });
      });
      setTimeout(() => calculatorComponent.init(config), 0);
      return calculatorComponent;
    }

    return calculatorComponentFactory;

});
