/*****************************************************************************
 * Open MCT Web, Copyright (c) 2014-2015, United States Government
 * as represented by the Administrator of the National Aeronautics and Space
 * Administration. All rights reserved.
 *
 * Open MCT Web is 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.
 *
 * Open MCT Web includes source code licensed under additional open source
 * licenses. See the Open Source Licenses file (LICENSES.md) included with
 * this source code distribution or the Licensing information page available
 * at runtime from the About dialog for additional information.
 *****************************************************************************/
/*global define*/

define(
    [],
    function () {
        "use strict";

        /**
         * Allows launching of notification messages for the purposes of
         * demonstration and testing. Also demonstrates use of
         * the NotificationService. Notifications are non-blocking messages that
         * appear at the bottom of the screen to inform the user of events
         * in a non-intrusive way. For more information see the
         * {@link NotificationService}
         * @param $scope
         * @param $timeout
         * @param $log
         * @param notificationService
         * @constructor
         */
        function NotificationLaunchController($scope, $timeout, $log, notificationService) {
            var messageCounter = 1;

            function getExampleActionText() {
                var actionTexts = [
                    "Adipiscing turpis mauris in enim elementu hac, enim aliquam etiam.",
                    "Eros turpis, pulvinar turpis eros eu",
                    "Lundium nascetur a, lectus montes ac, parturient in natoque, duis risus risus pulvinar pid rhoncus, habitasse auctor natoque!"
                ];
                return actionTexts[Math.floor(Math.random()*3)];
            }

            function getExampleActions() {
                var actions = [
                    {
                        label: "Try Again",
                        callback: function () {
                            $log.debug("Try Again pressed");
                        }
                    },
                    {
                        label: "Remove",
                        callback: function () {
                            $log.debug("Remove pressed");
                        }
                    },
                    {
                        label: "Cancel",
                        callback: function () {
                            $log.debug("Cancel pressed");
                        }
                    }
                ];

                // Randomly remove some actions off the top; leave at least one
                actions.splice(0,Math.floor(Math.random() * actions.length));

                return actions;
            }

            function getExampleSeverity() {
                var severities = [
                    "info",
                    "alert",
                    "error"
                ];
                return severities[Math.floor(Math.random() * severities.length)];
            }

            /**
             * Launch a new notification with a severity level of 'Error'.
             */
            $scope.newError = function(){

                notificationService.notify({
                    title: "Example error notification " + messageCounter++,
                    hint: "An error has occurred",
                    severity: "error",
                    primaryOption: {
                        label: 'Retry',
                        callback: function() {
                            $log.info('Retry clicked');
                        }
                    },
                    options: getExampleActions()});
            };
            /**
             * Launch a new notification with a severity of 'Alert'.
             */
            $scope.newAlert = function(){

                notificationService.notify({
                    title: "Alert notification " + (messageCounter++),
                    hint: "This is an alert message",
                    severity: "alert",
                    primaryOption: {
                        label: 'Retry',
                        callback: function() {
                            $log.info('Retry clicked');
                        }
                    },
                    options: getExampleActions()});
            };


            /**
             * Launch a new notification with a progress bar that is updated
             * periodically, tracking an ongoing process.
             */
            $scope.newProgress = function(){

                var notificationModel = {
                    title: "Progress notification example",
                    severity: "info",
                    progress: 0,
                    actionText: getExampleActionText(),
                    unknownProgress: false
                };

                /**
                 * Simulate an ongoing process and update the progress bar.
                 * @param notification
                 */
                function incrementProgress(notificationModel) {
                    notificationModel.progress = Math.min(100, Math.floor(notificationModel.progress + Math.random() * 30));
                    notificationModel.progressText = ["Estimated time" +
                    " remaining:" +
                    " about ", 60 - Math.floor((notificationModel.progress / 100) * 60), " seconds"].join(" ");
                    if (notificationModel.progress < 100) {
                        $timeout(function(){incrementProgress(notificationModel);}, 1000);
                    }
                }

                notificationService.notify(notificationModel);
                incrementProgress(notificationModel);
            };

            /**
             * Launch a new notification with severity level of INFO.
             */
            $scope.newInfo = function(){

                notificationService.info({
                    title: "Example Info notification " + messageCounter++
                });
            };

        }
        return NotificationLaunchController;
    }
);
