/**
 * Created by xubt on 6/17/16.
 */
kanbanApp.directive('card', function ($uibModal) {
    return {
        restrict: 'E',
        templateUrl: 'component/card/partials/card.html',
        replace: true,
        transclude: true,
        scope: {
            card: '='
        },
        controller: ['$scope', 'localStorageService', 'assignmentServices', 'cardsServices', 'acceptanceCriteriaService', 'usersService', function ($scope, localStorageService, assignmentServices, cardsServices, acceptanceCriteriaService, usersService) {
            var acceptanceCriterias = acceptanceCriteriaService.loadAcceptanceCriterias($scope.card._links.acceptanceCriterias.href);
            acceptanceCriterias.then(function (_acceptanceCriteriasResponse) {
                var acceptanceCriterias = _acceptanceCriteriasResponse.acceptanceCriterias;
                var finishedAcceptanceCriteriasCount = 0;
                for (var index in acceptanceCriterias) {
                    if (acceptanceCriterias[index].finished === true) {
                        finishedAcceptanceCriteriasCount++;
                    }
                }
                $scope.card.finishedAcceptanceCriteriasCount = finishedAcceptanceCriteriasCount;
                $scope.card.totalAcceptanceCriteriasCount = acceptanceCriterias.length;
            });

            $scope.assign = function (_card) {
                var thisScope = $scope;
                $uibModal.open({
                    animation: false,
                    templateUrl: 'component/card/partials/assignment-confirm.html',
                    controller: function ($scope, $uibModalInstance) {
                        $scope.title = '确认信息';
                        if (thisScope.isIamAssigned) {
                            $scope.message = "你确定不再做该卡片吗?";
                            $scope.ok = function () {
                                var userName = usersService.getCurrentUser().userName;
                                var myAssignmentLink;
                                angular.forEach(thisScope.assignments, function (_assignment) {
                                    if (userName === _assignment.assignee) {
                                        myAssignmentLink = _assignment._links.self.href;

                                    }
                                });
                                var assignmentPromise = assignmentServices.giveUp(myAssignmentLink);
                                assignmentPromise.then(function (_data) {
                                    thisScope.loadAssignments();
                                });
                                $uibModalInstance.close();
                            };
                        }
                        else {
                            $scope.message = "你确定要认领该卡片吗?";
                            $scope.ok = function () {
                                var assignment = {
                                    cardId: _card.id,
                                    assignee: usersService.getCurrentUser().userName,
                                    assigner: usersService.getCurrentUser().userName
                                };

                                var assignmentPromise = assignmentServices.assign(assignment, _card._links.assignments.href);
                                assignmentPromise.then(function (_data) {
                                    thisScope.loadAssignments();
                                });
                                $uibModalInstance.close();
                            };
                        }
                        $scope.cancel = function () {
                            $uibModalInstance.dismiss('cancel');
                        };
                    },
                    size: 'sm'
                });
            };

            var cardScope = $scope;
            $scope.openCardConfiguration = function () {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'component/card/partials/card-configuration.html',
                    controller: ['$scope', 'teamsService', 'timerMessageService', '$uibModalInstance',
                        function ($scope, teamsService, timerMessageService, $uibModalInstance) {
                            $scope.card = cardScope.card;
                            $scope.cardSaveButton = "保存";
                            $scope.saveCard = function () {
                                $scope.cardSaveButton = "保存中..";
                                $scope.isDisableCardSaveButton = true;
                                var cardPromise = cardsServices.update($scope.card);
                                cardPromise.then(function (_card) {
                                    timerMessageService.message("配置已经更新");
                                    cardScope.card = _card;
                                    $scope.card = _card;

                                }).finally(function () {
                                    $scope.cardSaveButton = "保存";
                                    $scope.isDisableCardSaveButton = false;
                                });
                            };
                            $scope.cancel = function () {
                                $uibModalInstance.dismiss('cancel');
                            };
                        }],
                    size: 'mid',
                    backdrop: "static"
                });
            };
            $scope.updateCard = function (_summary, _card) {
                if (_summary === "") {
                    return "卡片描述不能为空";
                }
                var card = _card;
                card.summary = _summary;
                var cardPromise = cardsServices.update(card);
                cardPromise.then(function () {
                });
            };
            $scope.openDeleteModal = function (_message, _link) {
                var currentScope = $scope;
                $uibModal.open({
                    animation: false,
                    templateUrl: 'foundation/modal/partials/confirm-dialog.html',
                    controller: function ($scope, $uibModalInstance) {
                        $scope.title = '警告';
                        $scope.message = "确定要删除" + _message + "吗?";
                        $scope.ok = function () {
                            var _cardDeletePromise = cardsServices.deleteByLink(_link);
                            _cardDeletePromise.then(function () {
                                currentScope.$parent.loadCards();
                            });
                            $uibModalInstance.close();
                        };
                        $scope.cancel = function () {
                            $uibModalInstance.dismiss('cancel');
                        };
                    },
                    size: 'sm',
                    backdrop: "static"
                });
            };

            $scope.isAssigned = function () {
                var userName = usersService.getCurrentUser().userName;
                $scope.isIamAssigned = false;
                angular.forEach($scope.assignments, function (_assignment) {
                    if (userName === _assignment.assignee) {
                        $scope.isIamAssigned = true;

                    }
                });
                $scope.assignTip = $scope.isIamAssigned === true ? "我不做了" : "认领";
            };
            $scope.mouseover = function () {
                $scope.isShowCardOperationMenu = true;
            };

            $scope.onMouseLeave = function () {
                $scope.isShowCardOperationMenu = false;
            };
        }]
    };
});



@@ -4822,16 +4822,86 @@
 %E5%B7%B2%E7%BB%8F%E6%9B%B4%E6%96%B0%22);%0A
+                                    _card.tags = cardScope.card.tags;%0A
         

Fix tags lost bug.