angular.module('AdmissionAppServices', [
    'LocalStorageModule',
    'toaster',
    'ngLodash',
    'ngSanitize'
])
    .constant('TOASTTIMEOUT', 5000)
    .factory('admissionFormService', ['$http', 'localStorageService', '$window',
        'TOASTTIMEOUT', 'toaster', 'lodash', '$timeout', '$cacheFactory','$rootScope',
        function($http, localStorageService, $window, TOASTTIMEOUT, toaster, lodash, $timeout, $cacheFactory,$rootScope) {

        const endpoint = '../server/public/';


        function addAlert(ms, typ, title, timeout,
                          bodyOutputType, clickHandler, closeHtml, toastId, onShowCallback,
                          onHideCallback, directiveData, tapToDismiss) {
            var sel = $('#toast-container');
            if (sel) {
                sel.removeClass("toast-top-full-width");
                sel.addClass("toast-top-right");
            }
            var toast = {
                type: typ || 'warning',
                title: title || "Info",
                body: ms,
                timeout: timeout || TOASTTIMEOUT || 5000,
                bodyOutputType: bodyOutputType,
                clickHandler: clickHandler,
                showCloseButton: true,
                closeHtml: closeHtml,
                toastId: toastId,
                onShowCallback: onShowCallback,
                onHideCallback: onHideCallback,
                directiveData: directiveData,
                tapToDismiss: tapToDismiss
            };
            $timeout(function () {
                toaster.pop(toast);
            });
        }

        function login(email, password,onSuccess, onError){

            $http.get('../server/public/auth/login?email='+email+'&password='+password
            ).
            then(function(response) {
                if(response.data.success) {
                    onSuccess(response);
                }
                else{
                    onError(response);
                }

            }, function(response) {

                onError(response);

            });

        }

        function forgetPassword(email,onSuccess, onError) {

            $http.get('../server/public/settings/request/password/reset?email=' + email
            ).then(function (response) {
                if (response.data.success) {
                    onSuccess(response);
                }
                else {
                    onError(response);
                }

            }, function (response) {

                onError(response);

            });

        }

        function getIndex(onSuccess, onError) {

            $http.get('../server/public/admission-form/index')
                // $http.get('../server/public/admission-form/all')
                .then(function (response) {
                    if (response.data) {
                        onSuccess(response);
                    }
                    else {
                        onError(response);
                    }

                }, function (response) {

                    onError(response);

                });

        }

        function processPayment(payload,onSuccess, onError) {

            $http.post('../server/public/accounts/paystack-success',payload)
                // $http.get('../server/public/admission-form/all')
                .then(function (response) {
                    if (response.data && response.data.success) {
                        onSuccess(response);
                    }
                    else {
                        onError(response);
                    }

                }, function (response) {

                    onError(response);

                });

        }
        function paystackInit(payload,onSuccess, onError) {

            $http.get('../server/public/admission-form/paystack?'+payload)
                // $http.get('../server/public/post-entrance-form/all')
                .then(function (response) {
                    if (response.data && response.data.success) {
                        onSuccess(response);
                    }
                    else {
                        onError(response);
                    }

                }, function (response) {

                    onError(response);

                });

        }

        function processRavePayment(payload,onSuccess, onError) {
            $http.post('../server/public/accounts/rave-success',payload)
                .then(function (response) {
                    if (response.data && response.data.success) {
                        onSuccess(response);
                    }
                    else {
                        onError(response);
                    }

                }, function (response) {

                    onError(response);

                });

        }


        function raveInit(payload,others,onSuccess, onError) {
            var payloads = new FormData();
            payloads.append("first_data", JSON.stringify(payload));
            payloads.append("second_data", JSON.stringify(others));
            // console.log(payloads); return false;
            $http.post('../server/public/admission-form/rave',payloads,
                {
                    headers: { 'Content-Type': undefined},
                    transformRequest: angular.identity
                })
                .then(function (response) {
                    if (response.data && response.data.success) {
                        onSuccess(response);
                    }
                    else {
                        onError(response);
                    }

                }, function (response) {

                    onError(response);

                });

        }
        function offlineInit(payload, others, onSuccess, onError) {
            var payloads = new FormData();
            payloads.append("first_data", JSON.stringify(payload));
            payloads.append("second_data", JSON.stringify(others));
            $http.post('../server/public/admission-form/offline-payment',payloads,
                {
                    headers: { 'Content-Type': undefined},
                    transformRequest: angular.identity
                })
                .then(function (response) {
                    if (response.data && response.data.success) {
                        onSuccess(response);
                    }
                    else {
                        onError(response);
                    }

                }, function (response) {

                    onError(response);

                });

        }

        function sendMail(id,is_failed,onSuccess, onError) {
            var getparams = {
                is_failed:is_failed
            }
            $http.get('../server/public/admission-form/success/email/'+id,
                {params:getparams})
                // $http.get('../server/public/post-entrance-form/all')
                .then(function (response) {
                    if (response.data && response.data.success) {
                        onSuccess(response);
                    }
                    else {
                        onError(response);
                    }

                }, function (response) {

                    onError(response);

                });

        }

        function getDashboardData(loggedInUser, onSuccess, onError) {
            var params = {
                id:loggedInUser
            }
            $http.get('../server/public/admission-form/dashboard-user-data',
                {params:params})
                // $http.get('../server/public/post-entrance-form/all')
                .then(function (response) {
                    if (response.data && response.data.success) {
                        onSuccess(response);
                    }
                    else {
                        onError(response);
                    }

                }, function (response) {

                    onError(response);

                });

        }

        function prepareMyCredit(email,phone,amount,onSuccess, onError) {
            $http.get('../server/public/wallet-fund/init-credit-wallet/'+email+'/'+phone+'/'+amount)
                .then(function (response) {
                    // console.log("resp: ",response);
                    if (response.data) {
                        onSuccess(response);
                    }
                    else {
                        onError(response);
                    }

                }, function (response) {

                    onError(response);

                });
        }

        function paystackInitWallet(payload,onSuccess, onError) {

            $http.post('../server/public/accounts/paystack-init-wallet',payload)
                // $http.get('../server/public/admission-form/all')
                .then(function (response) {
                    if (response.data && response.data.success) {
                        onSuccess(response);
                    }
                    else {
                        onError(response);
                    }

                }, function (response) {

                    onError(response);

                });

        }

        function processWalletPayment(payload,onSuccess, onError) {

            $http.post('../server/public/accounts/paystack-success-wallet',payload)
                // $http.get('../server/public/admission-form/all')
                .then(function (response) {
                    if (response.data && response.data.success) {
                        onSuccess(response);
                    }
                    else {
                        onError(response);
                    }

                }, function (response) {

                    onError(response);

                });

        }
        function raveInitWallet(payload,onSuccess, onError) {

            $http.post('../server/public/accounts/rave-init-wallet',payload)
                // $http.get('../server/public/admission-form/all')
                .then(function (response) {
                    if (response.data && response.data.success) {
                        onSuccess(response);
                    }
                    else {
                        onError(response);
                    }

                }, function (response) {

                    onError(response);

                });

        }
        function processRaveWalletPayment(payload,onSuccess, onError) {

            $http.post('../server/public/accounts/rave-success-wallet',payload)
                // $http.get('../server/public/admission-form/all')
                .then(function (response) {
                    if (response.data && response.data.success) {
                        onSuccess(response);
                    }
                    else {
                        onError(response);
                    }

                }, function (response) {

                    onError(response);

                });

        }

        function saveWalletTransaction (payload) {
            var endpointacc = '../server/public/accounts/';
            return $http.post(endpointacc + 'wallet/payu-success', payload);
        }

        function saveWalletTransactionRave2 (payload) {
            var endpoint = '../server/public/walletmanagement/';
            return $http.post(endpoint + 'set-invoice-paid', payload);
        }



        function  fetchData(url, onSuccess, onError){
            $http.get(endpoint + url).
            then(function(response) {
                if(response.data && response.data.success) {
                    onSuccess(response);
                }
                else{
                    onError(response);
                }
            }, function(response) {
                onError(response);
            });
        }

        function  fetchDataWithParam(url, getParam, onSuccess, onError){
            $http.get(endpoint + url, {params:getParam}).
            then(function(response) {
                if(response.data && response.data.success) {
                    onSuccess(response);
                }
                else{
                    onError(response);
                }
            }, function(response) {
                onError(response);
            });
        }

        function sendData(url, data, onSuccess, onError) {
            $http.post(endpoint + url, data).then(function(response) {
                if(response.data && response.data.success) {
                    onSuccess(response);
                }
                else{
                    onError(response);
                }
            }, function(response) {
                onError(response);
            });
        }

        function sendFormData(url, data,onSuccess,onError) {
            $http.post(endpoint + url, data,
                {
                    headers: { 'Content-Type': undefined},
                    transformRequest: angular.identity
                })
                .then(function(response) {
                    if(response.data && response.data.success) {
                        onSuccess(response);
                    }
                    else{
                        onError(response);
                    }
                }, function(response) {
                    onError(response);
                });
        }


        return {
            fetchData:fetchData,
            fetchDataWithParam:fetchDataWithParam,
            sendData:sendData,
            sendFormData:sendFormData,

            addAlert:addAlert,


            sendMail:sendMail,
            processPayment:processPayment,
            getIndex:getIndex,
            paystackInit:paystackInit,
            login: login,
            forgetPassword:forgetPassword,
            raveInit:raveInit,
            offlineInit:offlineInit,
            processRavePayment:processRavePayment,
            getDashboardData:getDashboardData,
            prepareMyCredit:prepareMyCredit,
            paystackInitWallet:paystackInitWallet,
            processWalletPayment:processWalletPayment,
            raveInitWallet:raveInitWallet,
            processRaveWalletPayment:processRaveWalletPayment,
            saveWalletTransaction:saveWalletTransaction,
            saveWalletTransactionRave2:saveWalletTransactionRave2,

        }

    }]);
