var FilmDirective = function () {
    return {
        restrict: "E",
        scope: {
            "film": "=",
            "ajouterFavoris": "&",
            "supprimerFavoris": "&",
            "visionner": "&"
        },
        template: `<ion-item class="item-thumbnail-left" 
	                ng-click="afficherDetails(film.imdbID)">
		            <img ng-src="{{film.Poster}}">
                    <ion-delete-button ng-if="supprimerFavoris" ng-click="supprimerFavoris(film)" class="ion-trash-a"></ion-delete-button>
		            {{film.Title}}
		            <ion-option-button ng-if="ajouterFavoris" ng-click="ajouterFavoris(film)" class="button-energized icon-left ion-star">Favoris</ion-option-button>
		            <ion-option-button ng-if="visionner" class="button-balanced icon-left ion-play" ng-click="ouvrirModalPlannification()">
			            Ma liste
		            </ion-option-button>
	                </ion-item>`,
        controller: function ($scope, $state, $ionicPopup, $ionicModal, FavorisSvc) {
            $ionicModal.fromTemplateUrl('templates/planning-film-modal.html', {
                scope: $scope
            }).then(function (modal) {
                $scope.modal = modal;
            });

            $scope.ouvrirModalPlannification = function () {
                $scope.modal.show();
            };

            $scope.enregistrerPlanification = function () {
                // Traitement métier à implémenter
                $scope.modal.hide();
            };

            $scope.fermerModale = function () {
                $scope.modal.hide();
            };

            $scope.$on('$destroy', function () {
                $scope.modal.remove();
            });

            $scope.ajouterFavoris = function (film) {
                $ionicPopup.confirm({
                    title: 'Ajouter aux favoris',
                    template: 'Ajouter ce film aux favoris'
                })
                    .then(function (ok) {
                        if (ok) {
                            FavorisSvc.ajouter(film);
                        }
                    });
            };

            $scope.afficherDetails = function (idFilm) {
                $state.go('tabs.detailsFilm', { idFilm: idFilm });
            };
        }
    }
};