(function (angular) {
    "use strict";
    angular
        .module('folderPluginWidget')
        .controller('tabata', ['$scope', '$sce', '$timeout', '$rootScope', 'Messaging', 'Utility',
            function ($scope, $sce, $timeout, $rootScope, Messaging, Utility) {
                $scope.work = 20;
                $scope.rest = 10;
                $scope.seconds = 20;
                $scope.restseconds = 10;
                $scope.paused = 0;
                $scope.fmessage = "Finish";

                $scope.setRounds = function () {
                    $scope.rounds = $scope.setrounds;
                }
                $scope.reset = function () {

                    var options = { milliseconds: 500 };
                    buildfire.notifications.vibrate(options, 1000);
                    $timeout.cancel($scope.rTimer);
                    $timeout.cancel($scope.Timer);
                    $timeout.cancel($scope.btimer);
                    $scope.work = 20;
                    $scope.rest = 10;
                    $scope.seconds = 20;
                    $scope.restseconds = 10;

                }
                $scope.bstart = function () {
                    var options = { milliseconds: 500 };
                    buildfire.notifications.vibrate(options, 1000);
                    $scope.cshow = true;
                    $scope.fshow = false;
                        $scope.btimer = function () {
                        $scope.bnewTime = new Date().getTime();
                        $scope.B = Math.round(($scope.boldTime - $scope.bnewTime) / 1000);
                        $scope.countdown = 3 + $scope.B;
                        $scope.bTimer = $timeout($scope.btimer, 100);
                        if ($scope.countdown === 0) {
                            $scope.cshow = false;
                            $scope.countdown = 3;
                            var sound = document.getElementById("long");
                            sound.play();
                            var options = { milliseconds: 500 };
                            buildfire.notifications.vibrate(options, 1000);
                            $timeout.cancel($scope.bTimer);
                            $scope.paused = 0;
                            $scope.start();
                        }
                    }
                    $scope.boldTime = new Date().getTime();

                    $scope.bTimer = $timeout($scope.btimer, 100);

                }

                
                $scope.start = function () {

                    if ($scope.paused === 1) {

                        $scope.work = $scope.pausedWork;
                        $timeout.cancel($scope.rTimer);
                        $scope.paused = 0;

                    }
                    if ($scope.paused === 2) {

                        $scope.rest = $scope.pausedRest;
                        $timeout.cancel($scope.Timer);
                        $scope.rstart();

                    }
                    if ($scope.paused === 0) {
                    $scope.timer = function () {
                    $scope.newTime = new Date().getTime();
                    $scope.D = Math.round(($scope.oldTime - $scope.newTime) / 1000);
                    $scope.d = $scope.D;
                    $scope.Timer = $timeout($scope.timer, 100);
                    $scope.seconds = $scope.work + $scope.D;
                    if ($scope.seconds === 0) {
                        var sound = document.getElementById("long");
                        var options = {milliseconds:1000};
                        buildfire.notifications.vibrate(options, 1000);
                        sound.play();
                        $timeout.cancel($scope.Timer);
                        $scope.reset();
                        $scope.rstart();
                    }
                    }
                    if ($scope.seconds === 0) {
                        var sound = document.getElementById("long");
                        var options = { milliseconds: 1000 };
                        buildfire.notifications.vibrate(options, 1000);
                        sound.play();
                        $timeout.cancel($scope.Timer);
                        $scope.reset();
                        $scope.rstart();
                    }
                    $scope.oldTime = new Date().getTime();
                    }
                $scope.Timer = $timeout($scope.timer, 100);

                    


            }
            $scope.rstart = function () {
                $scope.rtimer = function () {
                    $scope.rnewTime = new Date().getTime();
                    $scope.R = Math.round(($scope.roldTime - $scope.rnewTime) / 1000);
                    $scope.rTimer = $timeout($scope.rtimer, 100);
                    $scope.restseconds = $scope.rest + $scope.R;
                    if ($scope.restseconds === 0) {
                        var sound = document.getElementById("long");
                        sound.play();
                        var options = { milliseconds: 500 };
                        buildfire.notifications.vibrate(options, 1000);
                        $scope.rounds--;

                        $timeout.cancel($scope.rTimer);
                        $scope.rest = 10;
                        $scope.restseconds = 10;
                        $scope.paused = 0;
                        $scope.start();
                    }
                    if ($scope.rounds === 0) {
                        $scope.reset();
                        $scope.fshow = true;
                        }
                }
                $scope.roldTime = new Date().getTime();
                $scope.rTimer = $timeout($scope.rtimer, 100);


            }
            $scope.stop = function () {
                var options = { milliseconds: 500 };
                buildfire.notifications.vibrate(options, 1000);
                if ($scope.seconds === 20) {
                    $timeout.cancel($scope.rTimer);
                    $scope.pausedRest = $scope.restseconds;
                    $scope.paused = 2;
                }
                if($scope.restseconds ===10) {
                $timeout.cancel($scope.Timer);
                $scope.pausedWork = $scope.seconds;
                $scope.paused = 1;
                }
            }


            }]);
})(window.angular);