/*
 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*global define, console, tizen, window*/

/**
 * Sensor model module.
 *
 * @module models/sensor
 * @requires {@link core/event}
 * @namespace models/sensor
 * @memberof models
 */

define({
    name: 'models/sensor',
    requires: [
        'core/event'
    ],
    def: function modelsSensor(e) {
        'use strict';

        /**
         * Specifies the sensor type as ultraviolet.
         *
         * @private
         * @const {string}
         */
        var SENSOR_TYPE = 'ULTRAVIOLET',

            /**
             * Object representing Tizen sensor service.
             *
             * @private
             * @type {SensorService}
             */
            sensorService = null,

            /**
             * Ultraviolet sensor instance.
             *
             * @private
             * @type {Sensor}
             */
            sensor = null,

            /**
             * Current ultraviolet level.
             *
             * @private
             * @type {number}
             */
            currentUltraviolet = 0;

        /**
         * Performs an action on the start sensor success.
         *
         * @private
         * @fires models.sensor.start
         */
        function onSensorStartSuccess() {
            e.fire('start');
        }

        /**
         * Performs an action on the start sensor error.
         *
         * @private
         * @fires models.sensor.error
         */
        function onSensorStartError(e) {
            console.error('SENSOR: onSensorStartSuccess()', e);
            e.fire('error');
        }

        /**
         * Performs an action on sensor change.
         *
         * @private
         * @param {object} data
         * @fires models.sensor.change
         */
        function onSensorChange(data) {
            currentUltraviolet = data.ultravioletLevel;
            e.fire('change', {ultravioletLevel: data.ultravioletLevel});
        }

        /**
         * Starts sensor.
         *
         * @memberof models/sensor
         * @public
         */
        function start() {
            if (sensor) {
                sensor.start(onSensorStartSuccess, onSensorStartError);
            }
        }

        /**
         * Sets sensor change listener.
         *
         * @memberof models/sensor
         * @public
         */
        function setChangeListener() {
            if (sensor) {
                sensor.setChangeListener(onSensorChange);
            }
        }

        /**
         * Returns sensor value.
         *
         * @memberof models/sensor
         * @public
         * @returns {number}
         */
        function getSensorValue() {
            return currentUltraviolet;
        }

        /**
         * Sets current ultraviolet value.
         *
         * @private
         * @param {object} data
         */
        function setCurrentUltravioletValue(data) {
            currentUltraviolet = data.ultravioletLevel;
        }

        /**
         * Initializes module.
         *
         * @memberof models/sensor
         * @public
         * @fires models.sensor.not.supported
         */
        function init() {
            sensorService = tizen.sensorservice ||
                (window.webapis && window.webapis.sensorservice) ||
                null;

            try {
                sensor = sensorService.getDefaultSensor(SENSOR_TYPE);
            } catch (error) {
                console.error('API is not supported!', error.message);
                e.fire('not.supported');
                return;
            }

            sensor.getUltravioletSensorData(setCurrentUltravioletValue);
        }

        return {
            init: init,
            start: start,
            setChangeListener: setChangeListener,
            getSensorValue: getSensorValue
        };
    }

});
