define(['app'], function (app) {
    'use strict';

    app.factory('$favorite', function ($http, $rootScope, $window) {
        var DATA = [];

        return {
            loadFromRemote: function (params,successHandle,errorHandle) {
                /*** load from localstorage for test */
                if ($window.localStorage.listFavorite){
                    DATA = JSON.parse($window.localStorage.listFavorite);
                }else{
                    DATA = [];
                }
                return;
                /*** end test */
                
                // return format: { status:0|1 , data:{} }
                var httpObj = $http({
                    url: $rootScope.API_URL+'/apiv1/favorite',
                    method: 'GET',
                    params: params || []
                });
                if (typeof successHandle == 'undefined'){
                    successHandle = function(response){
                        if (response.status==1){
                            DATA = response.data;
                        }
                    }
                }
                httpObj.success(successHandle);
                
                if (typeof errorHandle != 'undefined'){
                    httpObj.error(errorHandle);
                }
            },
            addToFavorite: function(params,successHandle,errorHandle) {
                /*** load from localstorage for test */
                DATA.push(params.data);
                $window.localStorage.listFavorite = JSON.stringify(DATA);
                return;
                /*** end test */
                
                var httpObj = $http({
                    url: $rootScope.API_URL+'/apiv1/favorite',
                    method: 'POST',
                    params: params || []
                });
                if (typeof successHandle == 'undefined'){
                    successHandle = function(response){
                        if (response.status==1){
                            DATA = response.data;
                        }
                    }
                }
                httpObj.success(successHandle);
                
                if (typeof errorHandle != 'undefined'){
                    httpObj.error(errorHandle);
                }
            },
            removeFromFavorite: function(params,successHandle,errorHandle) {
                /*** load from localstorage for test */
                DATA.splice(params.idx,1);
                $window.localStorage.listFavorite = JSON.stringify(DATA);
                return;
                /*** end test */
                
                var httpObj = $http({
                    url: $rootScope.API_URL+'/apiv1/favorite',
                    method: 'POST',
                    params: params || []
                });
                if (typeof successHandle == 'undefined'){
                    successHandle = function(response){
                        if (response.status==1){
                            DATA = response.data;
                        }
                    }
                }
                httpObj.success(successHandle);
                
                if (typeof errorHandle != 'undefined'){
                    httpObj.error(errorHandle);
                }
            },
            getAll: function(){
                return DATA;
            }
        };
    });
});
