EXPORTED_SYMBOLS = ["module"];

var module = function (app, common) { 

    function tplUrlID(url) {
        return function(id) {
            return url.replace('{ID}', id);
        };
    }
    function urlFriends(url, urlReq) {
        return function(p) {
            return (p = "req") ? urlReq : url;
        };
    }
    app.config = {
        useClickStatistics: true,
        observeBranding: true,
        uniqueWidget: true,
        navigateUrl: {
            main: "http://www.facebook.com/",
            profile: tplUrlID("http://www.facebook.com/profile.php?id={ID}"),
            news: "http://www.facebook.com/home.php?sk=nf",
            wall: tplUrlID("http://www.facebook.com/profile.php?id={ID}&sk=wall"),
            friends: urlFriends("http://www.facebook.com/friends/",
                                "http://www.facebook.com/friends/edit/?sk=requests"),
            messages: "http://www.facebook.com/messages/",
            message_thread: tplUrlID("http://www.facebook.com/messages/{ID}"),
            notifications: "http://www.facebook.com/notifications.php",
            notification: "http://www.facebook.com/notifications.php",
            photos: "http://www.facebook.com/home.php?sk=media",
            videos: "http://www.facebook.com/home.php?sk=video",
            media: "http://www.facebook.com/home.php?sk=media",
            notes: "http://www.facebook.com/notes/",
            groups: "http://www.facebook.com/groups/",
            events: "http://www.facebook.com/events/"
        },
        FBdomain: {
            api: 'https://api.facebook.com/',
            api_read: 'https://api-read.facebook.com/',
            cdn: 'http://static.ak.fbcdn.net/',
            https_cdn: 'https://s-static.ak.fbcdn.net/',
            graph: 'https://graph.facebook.com/',
            staticfb: 'http://static.ak.facebook.com/',
            https_staticfb: 'https://s-static.ak.facebook.com/',
            www: 'http://www.facebook.com/',
            https_www: 'https://www.facebook.com/'
        },
        credentials: {
            client_id: "247264655307036",
            redirect_uri: "http://yandex.com/ECC39565E4D14833A5194891B2815746/BA3F557F7135430187C7EEFC829B5ECE"
        },
        branding: {
        //*
            yandex: {
                credentials: {
                    client_id: "132922660054643",
                    redirect_uri: "http://yandex.ru/ECC39565E4D14833A5194891B2815746/BA3F557F7135430187C7EEFC829B5ECE"
                }
            }
        //*/
        }
    };
    
    var timer = null;

    // -------------------------------------------------------
    app.init = function() {
        this.log('*** init');
        this.fb = this.importModule('api');
        this.oauth = this.importModule('oauth');
        this.fb.init(this);
        this.oauth.init(this);

        timer = common.timers.create(this.api.Settings.getValue('update-interval')*60*1000, updateData, this);
        this.api.Settings.observeChanges(this);
    };
    app.finalize = function() {
        this.log('*** finalize');
        this.api.Settings.ignoreChanges();
    };
    // -------------------------------------------------------

    // handlers
    app.onSettingChange = function (key, value, instanceId) {
        if (key == 'update-interval') {
            timer.setInterval(value*60*1000);
        } else {
            if (/^show-/.test(key)) {
                common.observerService.notify('showitems');
            }
        }
    };
    app.onLogin = function() {
        this.fb.setCredentials(this.oauth.credentials);
        timer.start();
        this.fb.update();
    };
    app.onLogout = function() {
        timer.stop();
        this.fb.clearData();
        common.observerService.notify("throbber");
        common.observerService.notify("display");
    };

    app.onUpdate = function() {
        this.logObj(this.fb.userData, 'onUpdate: ');
        common.observerService.notify("throbber");
        common.observerService.notify("display");
    };
    app.onUpdateError = function(status, text) {
        this.log('onUpdateError: ' + status + ' ' + text);
        if ((status == 401) && (text == "*token*")) {
            this.logr("token expired, relogin");
            this.oauth.login();
        } else {
            common.observerService.notify("throbber");
            if ((status == 403) && (text == "*denied*")) {
                this.logr("access denied, widget logout");
                this.oauth.logout(true);
            }
        }
    };

    app.uiCommands = {
        "auth": function(command, eventData) {
            if (!this.isAuth()) {
                this.oauth.login(true);
            }
        },
        "logout": function() {
            common.observerService.notify("throbber");
            this.oauth.logout();
        },
        "update": function(command, eventData) {
            if (this.fb.updating) {
                return;
            }
            common.observerService.notify("throbber", eventData.param);
            updateData();
        },
        "share": function() {
            this.oauth.showSharePage(common.ui.getCurrentURL());
        },
        "post": function() {
            this.oauth.showPostPage();
        }
    };

    // -------------------------------------------------------
    function updateData() {
        app.oauth.testCredentials();
    }
    app.isAuth = function() {
        return this.oauth.credentials;
    };
    app.getUserData = function() {
        return this.isAuth() ? this.fb.userData : null;
    };
};