angular.module('odoo')

    /**
     *
     */
    .controller('OpportunitiesController', function (
        $scope,
        $ionicActionSheet,
        $ionicPopup,
        crmStagesDataFactory,
        opportunitiesDataFactory,
        ionicToast
    ) {

        $scope.loading = false;
        $scope.stages = [];
        $scope.opportunities = [];
        $scope.currentStage = {};
        $scope.stageToMove = {};
        $scope.selected = -1;

        /**
         *
         */
        $scope.$on("$ionicSlides.sliderInitialized", function (event, data) {
            $scope.slider = data.slider;
        });

        /**
         *
         */
        $scope.$on("$ionicSlides.slideChangeEnd", function (event, data) {
            $scope.stageChanged(data.slider.activeIndex);
        });

        /**
         *
         */
        $scope.$on("$ionicView.enter", function () {
            $scope.initialize();
        });

        /**
         *
         */
        $scope.initialize = function () {
            $scope.loading = true;

            $scope.getStages(function (stages) {

                $scope.slider.updateLoop();
                $scope.getOpportunities(function (opportunities) {
                    console.log(opportunities);

                    $scope.loading = false;
                    $scope.$apply();
                }, function (err) {
                    $scope.loading = false;
                });

            }, function (err) {
                $scope.loading = false;
            });
        }

        /**
         *
         */
        $scope.getStages = function (success, error) {
            crmStagesDataFactory.sync(function (stages) {
                $scope.stages = stages;
                $scope.stageChanged(0);

                success(stages);
            }, function (syncErr) {
                crmStagesDataFactory.getAll(function (stages) {
                    $scope.stages = stages;
                    $scope.stageChanged(0);

                    success(stages);
                }, function (getAllErr) {
                    error(getAllErr);
                });
            });
        }

        /**
         *
         */
        $scope.getOpportunities = function (success, error) {
            opportunitiesDataFactory.sync(function (opportunities) {
                $scope.opportunities = opportunities;

                success(opportunities);
            }, function (syncErr) {
                opportunitiesDataFactory.getAll(function (opportunities) {
                    $scope.opportunities = opportunities;

                    success(opportunities);
                }, function (getAllErr) {
                    error(getAllErr);
                });
            });
        }

        /**
         * Change the state
         */
        $scope.changeStage = function (mode) {
            $scope.loading = true;
            if (mode) {
                $scope.slider.slideNext();
            } else {
                $scope.slider.slidePrev();
            }
            $scope.loading = false;
        }

        /**
         * Change stage name on title
         */
        $scope.stageChanged = function (index) {
            if (!$scope.stages.length) {
                return;
            }

            $scope.currentStage = $scope.stages[index];

            if (!$scope.$$phase) {
                $scope.$apply();
            }
        }

        /**
         * Show availables stages to move
         */
        $scope.showStagesToMove = function () {
            $ionicPopup.show({
                scope: $scope,
                title: 'Mover a',
                templateUrl: 'templates/sales/crm-stages.html',
                buttons: [
                    {
                        text: 'Mover',
                        type: 'button-positive',
                        onTap: function (e) {
                            // console.log(e);
                            $scope.moveOpportunity();
                        }
                    },
                    {
                        text: 'Cancelar',
                        type: 'button-default',
                        onTap: function (e) {
                            // e.preventDefault();
                        }
                    }
                ]
            });
        }

        /**
         *
         */
        $scope.selectStageToMove = function (index) {
            $scope.stageToMove = $scope.stages[index];
        }

        /**
         * Move opportunity to stage
         */
        $scope.moveOpportunity = function () {
            console.log($scope.stageToMove);
            ionicToast.show('Se movió la oportunidad ', 'bottom', false, 1500);
        }

        /**
         * Open the actionsheet action options
         */
        $scope.openOptions = function (index) {
            $scope.selected = index;

            $ionicActionSheet.show({
                titleText: 'Acciones',
                buttons: [
                    {
                        text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
                    },
                    {
                        text: '<i class="icon ion-forward positive"></i> Mover a'
                    }
                ],
                destructiveText: '<i class="icon ion-trash-a assertive"></i> Eliminar',
                cancel: function () {
                    $scope.customer = {};
                    $scope.selected = -1;
                },
                buttonClicked: function (index) {
                    switch (index) {
                        case 0:
                            $scope.show();
                            break;
                        case 1:
                            $scope.showStagesToMove();
                            break;
                        default:
                            $scope.show();
                    }
                    return true;
                },
                destructiveButtonClicked: function () {
                    $scope.delete();

                    return true;
                }
            });
        }
    });
