/**
 * dat-gui JavaScript Controller Library
 * http://code.google.com/p/dat-gui
 *
 * Copyright 2011 Data Arts Team, Google Creative Lab
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 */

import Controller from './Controller';
import dom from '../dom/dom';
import common from '../utils/common';

/**
 * @class Provides a select input to alter the property of an object, using a
 * list of accepted values.
 *
 * @extends dat.controllers.Controller
 *
 * @param {Object} object The object to be manipulated
 * @param {string} property The name of the property to be manipulated
 * @param {Object|string[]} options A map of labels to acceptable values, or
 * a list of acceptable string values.
 *
 * @member dat.controllers
 */
class OptionController extends Controller {
	constructor(object, property, opts) {
		super(object, property);

		let options = opts;

		const _this = this;

		/**
		 * The drop down menu
		 * @ignore
		 */
		this.__select = document.createElement('select');

		if (common.isArray(options)) {
			const map = {};
			common.each(options, function(element) {
				map[element] = element;
			});
			options = map;
		}

		common.each(options, function(value, key) {
			const opt = document.createElement('option');
			opt.innerHTML = key;
			opt.setAttribute('value', value);
			_this.__select.appendChild(opt);
		});

		// Acknowledge original value
		this.updateDisplay();

		dom.bind(this.__select, 'change', function() {
			const desiredValue = this.options[this.selectedIndex].value;
			_this.setValue(desiredValue);
		});

		this.domElement.appendChild(this.__select);
	}

	setValue(v) {
		const toReturn = super.setValue(v);

		if (this.__onFinishChange) {
			this.__onFinishChange.call(this, this.getValue());
		}
		return toReturn;
	}

	updateDisplay() {
		this.__select.value = this.getValue();
		return super.updateDisplay();
	}
}

export default OptionController;
