/*
 * 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*/

/**
 * Widget storage module
 * @requires {@link core/window}
 * @requires {@link core/widget}
 * @namespace core/storage/widget
 * @memberof core
 */

define({
    name: 'core/storage/widgetstorage',
    requires: [
        'core/window',
        'core/widget'
    ],
    def: function coreWidgetStorage(window, widget) {
        'use strict';

        // Widget Storage Area
        // http://www.w3.org/TR/widgets-apis/#widget-storage-area
        var widgetStorage = widget.preferences,
            JSON = window.JSON;

        /**
         * Gets value for given key from widget storage.
         * @memberof core/storage/widgetstorage
         * @param {string} key Key.
         * @return {null|object} Json object.
         */
        function get(key) {
            var storageStr = widgetStorage.getItem(key);
            if (storageStr) {
                try {
                    return JSON.parse(storageStr);
                } catch (error) {
                    console.warn(
                        'core/storage/widgetstorage',
                        'Parse error:',
                        error
                    );
                    return null;
                }
            }
            return null;
        }

        /**
         * Sets value for given key to widget storage.
         * @memberof core/storage/widgetstorage
         * @param {string} key Key.
         * @param {object} val Value object.
         * @return {boolean}
         */
        function set(key, val) {
            var json = null;
            try {
                json = JSON.stringify(val);
            } catch (error) {
                console.warn('Stringify error', error);
            }

            if (json !== null) {
                widgetStorage.setItem(key, json);
                return true;
            }

            return false;
        }

        /**
         * Removes value for given key from widget storage.
         * @param {string} key Key name.
         */
        function removeItem(key) {
            widgetStorage.removeItem(key);
        }

        /**
         * Removes keys from given array.
         * @param {string[]} keys Key array.
         */
        function removeItems(keys) {
            keys.forEach(function forEach(key) {
                removeItem(key);
            });
        }

        /**
         * Removes value for given context.
         * @memberof core/storage/widgetstorage
         * @param {string|array} context Key name or keys array.
         */
        function remove(context) {
            if (typeof context === 'string') {
                removeItem(context);
            } else if (Array.isArray(context)) {
                removeItems(context);
            }
        }

        return {
            get: get,
            /**
             * Alias for {@link core/storage/widgetstorage.set}
             * @memberof core/storage/widgetstorage
             * @function
             * @see {@link core/storage/widgetstorage.set}
             */
            add: set,
            set: set,
            remove: remove
        };
    }
});
