
app.controller('ActivityController',
    ['$anchorScroll', '$location','$scope', '$http', '$timeout', 'auth', 'rms', 'common','alertService',
    function($anchorScroll, $location,$scope, $http, $timeout, auth, rms, common, alertService){

//    common.checkLogin();


    $scope.replaceUrl = function(url){
        return url;
    };

    $scope.menus = ['所有动态', '班级动态', '项目动态'];

    $scope.alertService = alertService.create();
    console.log("ActivityController load");

    $scope.page = rms.get('page') || 0;
    $scope.activities = rms.get("activities") || [];
    $scope.has_more = rms.get("has_more");

    $scope.course_page = rms.get('course_page') || 0;
    $scope.course_activities = rms.get("course_activities") || [];
    $scope.course_has_more = rms.get("course_has_more");

    $scope.project_page = rms.get('project_page') || 0;
    $scope.project_activities = rms.get("project_activities") || [];
    $scope.project_has_more = rms.get("project_has_more");

    $scope.loadActData = function(index,page){
        container_type = "All";
        switch(index){
            case 1:
                container_type = "All";
                $scope.page = page;
                break;
            case 2:
                container_type = "Course";
                $scope.course_page = page;
                break;
            case 3:
                container_type = "Project";
                $scope.project_page = page;
                break;
        }

        $http({
            method: 'POST',
            url: apiUrl+ "activities",
            data: {token: auth.token(), page: page, container_type: container_type}
        }).then(function successCallback(response) {
            if(response.data.page >0) {
                switch(response.data.container_type){
                    case "All":
                        $scope.activities = $scope.activities.concat(response.data.data);
                        break;
                    case "Course":
                        $scope.course_activities = $scope.course_activities.concat(response.data.data);
                        break;
                    case "Project":
                        $scope.project_activities = $scope.project_activities.concat(response.data.data);
                        break;
                    case 3:
                }
            } else {
                switch(response.data.container_type){
                    case "All":
                        $scope.activities = response.data.data;
                        break;
                    case "Course":
                        $scope.course_activities = response.data.data;
                        break;
                    case "Project":
                        $scope.project_activities = response.data.data;
                        break;
                    case 3:
                }
            }

            switch(response.data.container_type){
                case "All":
                    rms.save("activities", $scope.activities);
                    $scope.has_more = (response.data.count + response.data.page * 10) < response.data.all_count;
                    rms.save('has_more', $scope.has_more);
                    rms.save('page', response.data.page);
                    break;
                case "Course":
                    rms.save("course_activities", $scope.course_activities);
                    $scope.course_has_more = (response.data.count + response.data.page * 10) < response.data.all_count;
                    rms.save('course_has_more', $scope.course_has_more);
                    rms.save('course_page', response.data.page);
                    break;
                case "Project":
                    rms.save("project_activities", $scope.project_activities);
                    $scope.project_has_more = (response.data.count + response.data.page * 10) < response.data.all_count;
                    rms.save('project_has_more', $scope.project_has_more);
                    rms.save('project_page', response.data.page);
                    break;
                case 3:
            }
            console.log(response.data);

        }, function errorCallback(response) {
        });
    };

    $scope.tab = function(index){
        $scope.currentTab = index;
        switch(index){
            //ALL
            case 1:
                if($scope.activities.length<=0){
                    $scope.loadActData(index,0);
                }
                break;
            //Course
            case 2:
                if($scope.course_activities.length<=0){
                    $scope.loadActData(index,0);
                }
                break;
            //Project
            case 3:
                if($scope.project_activities.length<=0){
                    $scope.loadActData(index,0);
                }
                break;
        }
        rms.save("activity_tab_num",$scope.currentTab);
    }


    $scope.currentTab = rms.get('activity_tab_num') || 1;

    if($scope.activities.length<=0){
        $scope.loadActData($scope.currentTab,0);
    } else {
        $timeout(function(){
            window.scrollTo(0, rms.get("yoffset"));
        });
    }

    //跳到详情页
    $scope.goDetail = function(type, act_id,id){
        rms.save("yoffset", document.documentElement.scrollTop || document.body.scrollTop);
        rms.save("activities",$scope.activities);
        rms.save("course_activities",$scope.course_activities);
        rms.save("project_activities",$scope.project_activities);
        $location.path("/"+type).search({id: act_id});
    };

    $scope.addPraise = function(act){
        for(var i in $scope.activities){
            if($scope.activities[i].act_id == act.act_id){
                $scope.activities[i].praise_count += 1;
                $scope.activities[i].has_praise = true;
                break;
            }
        }
        for(var i in $scope.course_activities){
            if($scope.course_activities[i].act_id == act.act_id){
                $scope.course_activities[i].praise_count += 1;
                $scope.course_activities[i].has_praise = true;
                break;
            }
        }
        for(var i in $scope.project_activities){
            if($scope.project_activities[i].act_id == act.act_id){
                $scope.project_activities[i].praise_count += 1;
                $scope.project_activities[i].has_praise = true;
                break;
            }
        }

        common.addCommonPraise(act);
    };

    $scope.decreasePraise = function(act){
        for(var i in $scope.activities){
            if($scope.activities[i].act_id == act.act_id){
                $scope.activities[i].praise_count -= 1;
                $scope.activities[i].has_praise = false;
                break;
            }
        }
        for(var i in $scope.course_activities){
            if($scope.course_activities[i].act_id == act.act_id){
                $scope.course_activities[i].praise_count -= 1;
                $scope.course_activities[i].has_praise = false;
                break;
            }
        }
        for(var i in $scope.project_activities){
            if($scope.project_activities[i].act_id == act.act_id){
                $scope.project_activities[i].praise_count -= 1;
                $scope.project_activities[i].has_praise = false;
                break;
            }
        }

        common.decreaseCommonPraise(act);
    };


    $scope.goClass = function(id){
        rms.save("activities",[]);
        rms.save("course_activities",[]);
        rms.save("project_activities",[]);

        rms.save('course_activities_page',0);
        rms.save("course_has_more",false);
        rms.save("course",null);
        rms.save("tab_num",null);

        rms.save("has_more",false);
        rms.save("project_has_more",false);

        $location.path("/class").search({id: id});
    };

    $scope.goProject = function(id){
        rms.save("activities",[]);
        rms.save("course_activities",[]);
        rms.save("project_activities",[]);
        rms.save('project_activities_page',0);
        rms.save("project_has_more",false);
        rms.save("project",null);
        rms.save("project_master_members",[]);
        rms.save("project_develop_members",[]);
        rms.save("project_report_members",[]);
        rms.save("review_master_members",[]);
        rms.save("review_develop_members",[]);
        rms.save('tab_num',null);
        rms.save("has_more",false);
        rms.save("course_has_more",false);

        $location.path("/project").search({id: id});
    };
}]);