/**
 * Created by Thomas on 05/07/2015.
 */

(function () {
    'use strict';

    angular.module('tg.docs')
        .provider('settingsService', [settingsProvider]);

    /**
     * @ngdoc provider
     * @name settingsServiceProvider
     * @module tg.docs
     * @description A Provider to register app availaible in the application
     */
    function settingsProvider() {
        var provider = this;

        var settings = {language: 'en', apps: {}};
        var apps = [];
        var appId;

        /**
         * @ngdoc method
         * @name settingsProvider#setHandlerFunctions
         * @description Add an application to docs
         * @param {Object} appObject A new application {}
         * @return {Object} settingsProvider instance
         */
        provider.addApp = function (appObject) {
            appObject.children = [];
            apps.push(appObject);

            return provider;
        };

        provider.$get = ['$translate', 'gclient', settingsService];

        /**
         * @ngdoc service
         * @name settingsService
         * @module tg.docs
         * @description Setting service to manage the application properties
         * @requires $translate
         * @requires gclient
         */
        function settingsService($translate, gclient) {
            var languages = [
                {key:'en', name: 'English'},
                {key:'fr', name: 'Français'}
            ];

            return {
                /**
                 * @ngdoc property
                 * @name settingsService#settings
                 * @description  The app setting object
                 * @type {Object}
                 */
                datas: settings,
                /**
                 * @ngdoc property
                 * @name settingsService#apps
                 * @description The available apps
                 * @type {Array}
                 */
                apps: apps,
                /**
                 * @ngdoc property
                 * @name settingsService#languages
                 * @description  The available app language
                 * @type {Array}
                 */
                languages: languages,
                load: load,
                update: update
            };

            /**
             * @ngdoc method
             * @name settingsService#load
             * @description Load the app parameters from Google drive
             * @return {Object} Promise
             */
            function load() {

            /**
             * @ngdoc method
             * @name settingsService#update
             * @description Save the app parameters in Google drive
             * @return {Object} Promise
             */
            function update() {
                angular.forEach(apps, function (app) {
                    settings.apps[app.id] = app.enabled;
                });

                return gclient.drive.update(appId, settings);
            }
        }
    }

})();

                return gclient.request(
                    'drive.files.list',
                    {'q': 'properties has { key="docs" and value="docs" and visibility="PRIVATE" }'}
                )
                    .then(function (res) {
                        if (res.result.items.length > 0) {
                            appId = res.result.items[0].id;
                            return gclient.request('drive.files.get', {'fileId': appId, 'alt': 'media'});
                        } else {
                            var metadata = {
                                title: 'docs.json',
                                mimeType: 'application/json',
                                parents: [{id: 'appfolder'}],
                                properties: [{'key': 'docs', 'value': 'docs', 'visibility': 'PRIVATE'}]
                            };

                            angular.forEach(apps, function (app) {
                                settings.apps[app.name] = true;
                            });

                            return gclient.drive.insert(metadata, settings).then(function(res){
                                appId = res.result.id;
                                return {result: settings};
                            });
                        }
                    })
                    .then(function (res) {
                        settings = res.result;
                        angular.forEach(apps, function (app) {
                            app.enabled = settings.apps[app.id];
                        });

                        return $translate.use(settings.language);
                    });

            }
