/**
 * Created by rburson on 4/14/16.
 */

import * as React from 'react'
import {CvState, CvProps, CvBaseMixin, CvContext, CvEvent, CvEventRegistry, CvEventType, CvLogoutResult, CvSessionManager} from '../core/catreact-core'
import {Try, Future, VoidResult, AppContext, SessionContext} from 'catavolt-sdk'

export interface CvLogoutState extends CvState {
}

export interface CvLogoutProps extends CvProps {
    logoutListeners?:Array<(event:CvEvent<CvLogoutResult>)=>void>
    renderer?:(cvContext:CvContext, callback:CvLogoutCallback)=>{};
}

export interface CvLogoutCallback {

    isLoggedIn():boolean;
    logout():void;
    
}

/*
 ***************************************************
 * Provide Logout Support to other components
 ***************************************************
 */
export var CvLogout = React.createClass<CvLogoutProps, CvLogoutState>({

    mixins: [CvBaseMixin],

    getChildContext: function() {
        return this.getDefaultChildContext();
    },

    getDefaultProps: function () {
        return { renderer:null, logoutListeners: [] }
    },

    render: function () {
        if(this.props.renderer) {
            return this.props.renderer(this.getChildContext().cvContext, this._getCallbackObject());
        } else {
            return null;
        }
    },

    _getCallbackObject: function():CvLogoutCallback {

        return {

            isLoggedIn: ():boolean => {
                return this.catavolt().isLoggedIn;
            },
            
            logout: ():void => {
                let tenantId = null;
                if(this.catavolt().sessionContextTry.isSuccess){
                    tenantId = this.catavolt().sessionContextTry.success.tenantId;
                } else {
                    const sessionContext:SessionContext = CvSessionManager.getSession();
                    if(sessionContext) {
                        tenantId = sessionContext.tenantId;
                    }
                }
                (CvLogout as any).performLogout(this.catavolt(), this.eventRegistry(), tenantId, this.props.logoutListeners);
            }
        }

    },

    statics : {
       
        performLogout: function (catavolt:AppContext, eventRegistry:CvEventRegistry,
                                 tenantId:string, logoutListeners?:Array<(event:CvEvent<CvLogoutResult>)=>void>):void {
            if(catavolt.isLoggedIn) {
                catavolt.logout().onComplete((result:Try<VoidResult>) => {
                    (CvLogout as any).postLogout(eventRegistry, tenantId, logoutListeners);
                });
            } else {
                (CvLogout as any).postLogout(eventRegistry, tenantId, logoutListeners);
            }
            
        },
        
        postLogout: function (eventRegistry:CvEventRegistry, tenantId:string, logoutListeners?:Array<(event:CvEvent<CvLogoutResult>)=>void>):void {
            CvSessionManager.removeSession();
            const event:CvEvent<CvLogoutResult> = {
                type: CvEventType.LOGOUT,
                eventObj: {tenantId: tenantId}
            };
            eventRegistry.publish<CvLogoutResult>(event, false);
            if(logoutListeners) {
                logoutListeners.forEach((listener)=> {
                    listener(event)
                });
            }
            eventRegistry.clearAll();
        }
    }

});