<commit_before>/**
 * https://github.com/WizardFactory/TodayWeather/issues/524
 * Created by aleckim on 2016. 4. 11..
 */

angular.module('controller.purchase', [])
    .factory('Purchase', function($rootScope, $http, $q, TwAds, Util) {
        var obj = {};
        obj.ACCOUNT_LEVEL_FREE = 'free';
        obj.ACCOUNT_LEVEL_PREMIUM = 'premium';
        obj.accountLevel;
        obj.productId;
        obj.expirationDate;
        obj.loaded = false;
        obj.products;

        obj.setAccountLevel = function (accountLevel) {
            if (obj.accountLevel != accountLevel) {
                console.log('set account level ='+accountLevel);
                //update accountLevel
                obj.accountLevel = accountLevel;
                if (accountLevel === obj.ACCOUNT_LEVEL_FREE) {
                    TwAds.setEnableAds(true);
                }
                else if (accountLevel === obj.ACCOUNT_LEVEL_PREMIUM) {
                    TwAds.setEnableAds(false);
                }
            }
            else {
                console.log('account level is already set level='+accountLevel);
            }
        };

        obj.checkReceiptValidation = function(storeReceipt, callback) {
            var url = Util.url  + '/check-purchase';
            $http({
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                url: url,
                data: storeReceipt,
                timeout: 10*1000
            })
                .then(function (result) {
                    callback(undefined, result.data);
                },
                function (err) {
                    callback(err);
                });
        };

        obj.saveStoreReceipt = function (storeReceipt) {
            localStorage.setItem("storeReceipt", JSON.stringify(storeReceipt));
        };

        obj.loadStoreReceipt = function () {
            return JSON.parse(localStorage.getItem("storeReceipt"));
        };

        obj.loadPurchaseInfo = function () {
            console.log('load purchase info');
            var purchaseInfo = JSON.parse(localStorage.getItem("purchaseInfo"));

            if (purchaseInfo != undefined) {
                console.log('load purchaseInfo='+JSON.stringify(purchaseInfo));
                obj.expirationDate = purchaseInfo.expirationDate;
                //check account date
                if ((new Date(purchaseInfo.expirationDate)).getTime() < Date.now()) {
                    console.log('account expired, please renewal or restore');
                    purchaseInfo.accountLevel = obj.ACCOUNT_LEVEL_FREE;
                }
                obj.setAccountLevel(purchaseInfo.accountLevel);
            }
            else {
                obj.accountLevel = obj.ACCOUNT_LEVEL_FREE;
            }
        };

        obj.savePurchaseInfo = function (accountLevel, expirationDate) {
            var purchaseInfo = {accountLevel: accountLevel, expirationDate: expirationDate};
            localStorage.setItem("purchaseInfo", JSON.stringify(purchaseInfo));

            if (purchaseInfo.accountLevel === obj.ACCOUNT_LEVEL_PREMIUM) {
                TwAds.saveTwAdsInfo(false);
            }
            else {
                TwAds.saveTwAdsInfo(true);
            }
        };

        obj.updatePurchaseInfo = function () {
            var restoreFunc = function () {
                if (ionic.Platform.isIOS()) {
                    return inAppPurchase.getReceipt().then(function (receipt) {
                        if (receipt == undefined) {
                            return undefined;
                        }
                        return  {type: 'ios', id: obj.productId, receipt: receipt};
                    });
                }
                else if (ionic.Platform.isAndroid()) {
                    return inAppPurchase.restorePurchases().then(function(data) {
                        console.log('Purchases INFO!!!');
                        console.log(JSON.stringify(data));
                        console.log('receipt count='+data.length);
                        data.forEach(function (purchase) {
                           var inReceipt = JSON.parse(purchase.receipt);
                            console.log('receipt: '+JSON.stringify(inReceipt));
                            console.log('purchaseTime='+new Date(inReceipt.purchaseTime));
                        });
                        if (data.length == 0) {
                            return undefined;
                        }
                        //if you have many product find by product id
                        return {type: 'android', id: obj.productId, receipt: data};
                    });<commit_msg>fixed error about receipt data format<commit_after>/**
 * https://github.com/WizardFactory/TodayWeather/issues/524
 * Created by aleckim on 2016. 4. 11..
 */

angular.module('controller.purchase', [])
    .factory('Purchase', function($rootScope, $http, $q, TwAds, Util) {
        var obj = {};
        obj.ACCOUNT_LEVEL_FREE = 'free';
        obj.ACCOUNT_LEVEL_PREMIUM = 'premium';
        obj.accountLevel;
        obj.productId;
        obj.expirationDate;
        obj.loaded = false;
        obj.products;

        obj.setAccountLevel = function (accountLevel) {
            if (obj.accountLevel != accountLevel) {
                console.log('set account level ='+accountLevel);
                //update accountLevel
                obj.accountLevel = accountLevel;
                if (accountLevel === obj.ACCOUNT_LEVEL_FREE) {
                    TwAds.setEnableAds(true);
                }
                else if (accountLevel === obj.ACCOUNT_LEVEL_PREMIUM) {
                    TwAds.setEnableAds(false);
                }
            }
            else {
                console.log('account level is already set level='+accountLevel);
            }
        };

        obj.checkReceiptValidation = function(storeReceipt, callback) {
            var url = Util.url  + '/check-purchase';
            $http({
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                url: url,
                data: storeReceipt,
                timeout: 10*1000
            })
                .then(function (result) {
                    callback(undefined, result.data);
                },
                function (err) {
                    callback(err);
                });
        };

        obj.saveStoreReceipt = function (storeReceipt) {
            localStorage.setItem("storeReceipt", JSON.stringify(storeReceipt));
        };

        obj.loadStoreReceipt = function () {
            return JSON.parse(localStorage.getItem("storeReceipt"));
        };

        obj.loadPurchaseInfo = function () {
            console.log('load purchase info');
            var purchaseInfo = JSON.parse(localStorage.getItem("purchaseInfo"));

            if (purchaseInfo != undefined) {
                console.log('load purchaseInfo='+JSON.stringify(purchaseInfo));
                obj.expirationDate = purchaseInfo.expirationDate;
                //check account date
                if ((new Date(purchaseInfo.expirationDate)).getTime() < Date.now()) {
                    console.log('account expired, please renewal or restore');
                    purchaseInfo.accountLevel = obj.ACCOUNT_LEVEL_FREE;
                }
                obj.setAccountLevel(purchaseInfo.accountLevel);
            }
            else {
                obj.accountLevel = obj.ACCOUNT_LEVEL_FREE;
            }
        };

        obj.savePurchaseInfo = function (accountLevel, expirationDate) {
            var purchaseInfo = {accountLevel: accountLevel, expirationDate: expirationDate};
            localStorage.setItem("purchaseInfo", JSON.stringify(purchaseInfo));

            if (purchaseInfo.accountLevel === obj.ACCOUNT_LEVEL_PREMIUM) {
                TwAds.saveTwAdsInfo(false);
            }
            else {
                TwAds.saveTwAdsInfo(true);
            }
        };

        obj.updatePurchaseInfo = function () {
            var restoreFunc = function () {
                if (ionic.Platform.isIOS()) {
                    return inAppPurchase.getReceipt().then(function (receipt) {
                        if (receipt == undefined) {
                            return undefined;
                        }
                        return  {type: 'ios', id: obj.productId, receipt: receipt};
                    });
                }
                else if (ionic.Platform.isAndroid()) {
                    return inAppPurchase.restorePurchases().then(function(data) {
                        console.log('Purchases INFO!!!');
                        console.log(JSON.stringify(data));
                        //if you have many product find by product id
                        return {type: 'android', id: obj.productId, receipt: data};
                    });
{'fraction_non_alphanumeric': 0.10647111509180475, 'fraction_numerical': 0.003694581280788177, 'mean_word_length': 1.4683614258082343, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5814028', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2151, 'n_words': 403}