/* Copyright 2009 Palm, Inc.  All rights reserved. */

FirstuseAssistant = Class.create({
    initialize: function() {
    },
    setup: function() {
        this.controller.serviceRequest('palm://com.palm.accounts/', {
            method: 'getAllAccountTypesForService',
            parameters: {
                service: 'CONTACTS'
            },
            onSuccess: this.renderExistingAccountTypes.bind(this)
        });
        this.accountTypeModel = {
            items: []
        }
        this.accountTypeAttrs = {
            itemTemplate: "firstuse/accountTypeRow"
        }
        this.listAttrs = {
            itemTemplate: "firstuse/existingAccount"
        }
        this.listModel = {
            listTitle: "Accounts",
            items: []
        }
        this.controller.setupWidget("fwa_accts_list", this.listAttrs, this.listModel);
        this.accountsCallbackBound = this.accountsCallback.bind(this)
        AppAssistant.accountsService.getAllAccountsForService(this.controller, AccountsService.CONTACTS, false, this.accountsCallbackBound);
        
        this.controller.setupWidget("f_acctstypes_picker", this.accountTypeAttrs, this.accountTypeModel);
        this.controller.get("f_acctstypes_picker").observe(Mojo.Event.listTap, this.addAccount.bind(this));
        this.controller.get("f_nothanks_btn").observe(Mojo.Event.tap, this.goToList.bind(this));
        this.controller.get("f_nothanks_btn_done").observe(Mojo.Event.tap, this.goToList.bind(this));
        this.controller.get('fwa_yes_btn').observe(Mojo.Event.tap, this.goToFirstUseAcctTypePicker.bindAsEventListener(this));
        
    },
    
    accountsCallback: function(resp) {
        if (resp.list.length > 0) {
            this.controller.get("body-with-accounts").show();
            this.controller.get("body-without-accounts").hide();
        } else {
            this.controller.get("body-without-accounts").show();
            this.controller.get("body-with-accounts").hide();
        }
        
        this.listModel.items = resp.list;
        // Per HI, for EAS accounts show DOMAIN (EMAIL ADDRESS)
        this.listModel.items.each(function(acct) {
            acct.iconPath = Utilities.iconJSONToIconPath(acct.accountSpecificIcons);
            acct.loginDisplayName = acct.login;
            if (acct.domain == 'eas') {
                if (acct.originalLogin) {
                    acct.loginDisplayName = acct.originalLogin;
                }
            }
        });
        this.controller.modelChanged(this.listModel);
    },
    
    goToFirstUseAcctTypePicker: function() {
        this.controller.stageController.pushScene('choosetransport', "firstuse", true);
    },
    
    addAccount: function(e) {
        if (e.item.domain == "eas") {
            var args = {
                appId: "com.palm.app.email",
                name: "wizard"
            };
            
            var params = {
                edit: false,
                id: undefined,
                type: "contacts",
                title: $L("Add An Account"),
                protocol: "EAS"
            };
            
            this.controller.stageController.pushScene(args, params);
        } else {
            this.controller.stageController.pushScene("accountlogin", 0, undefined, e.item.id, e.item.domain, e.item.name, "firstuse", e.item.iconClass);
        }
    },
    
    renderExistingAccountTypes: function(resp) {
        resp.list.each(function(acct) {
            acct.iconPath = Utilities.iconJSONToIconPath(acct.icons);
        })
        this.accountTypeModel.items = resp.list;
        this.controller.modelChanged(this.accountTypeModel);
    },
    
    goToList: function() {
        AppAssistant.contactsService.setFirstUseToFalse();
        this.controller.stageController.popScenesTo();
        this.controller.stageController.pushScene("list");
    },
    
    handleCommand: function(event) {
        // if (event.type == Mojo.Event.back) {
        //   this.goToList();
        //   event.stopPropagation();
        //   event.preventDefault();
        //   return;
        // }   
    }
})
