///========================================
/// SoftPhone s.r.l
/// IWSConnector release 2.0.0.0
/// Date: 06/09/2017
///========================================

function networkError(message) {
    log.error(message);
}

function onIdentity(message) {
}

function onConnectedSession(message) {
    InteractionHelper.Clear();
}

function onDisconnectedSession(message) {
}

function onActivateSession(message) {
    if (existOptionInteractions())
        switchInteraction();
}

function onPostActivateSession(message) {
}

function onDeactivateSession(message) {
}

function onChannelStatus(message) {

}

function onEventAgentNotReady(message) {
    //to get the ActionCode
    //message.attachdata.ActionCode
}

function onEventAgentNotReadyAfterCallWork(message) {
}

function onEventAgentReady(message) {
}

function onEventAgentLogout(message) {
}

function onEventAgentLogin(message) {
}

//==================================================================
// Events MediaVoice
/*
* message.EVENT
* message.Place
* message.AgentID
* message.MediaType
* message.ConnectionID
* message.ANI
* message.DNIS
* message.CallType
* message.Duration
* message.TimeStamp
* message.attachdata
* message.EntrepriseLastInteractionEvent.PreviousConnID
*/
//==================================================================

function printAllAttachData(message) {
    log.debug("called printAttachData on Event:" + message.EVENT);
    for (var key in message.attachdata) {
        log.debugFormat("attach key[{0}] value[{1}] ", key, message.attachdata[key]);
    }
}

function onEventRingingInbound(message) {
    //printAllAttachData(message);
    //intChnPhone(message);

}


function onEventRingingInternal(message) {

}

function intChnPhone(message) {
    /*
        //SetAttachData Example
        var mycollection = createUserData();
        mycollection.put("paramtest", "value1");
        SetAttachdataById(message.ConnectionID, mycollection);
        */
    var windowName = 'Int_ID' + message.InteractionID;
    InteractionHelper.FlagInteractionChannel(message.InteractionID,
        InteractionHelper.InteractionChannel.phone, windowName, message);

    //todo: set message object to localStorage, interaction ID as key
    console.log("Inbound Message UUID - " + message.CallUuid);

    var f = $('#popupForm');
    f[0].reset();
    f.find("#idTypeCode").val(message.attachdata.u_idtype || message.attachdata.IDType); //  "1" = "NRIC"
    f.find("#idNo").val(message.attachdata.u_idnumber || message.attachdata.IDNo); // NRIC =  391118085574
    //f.find("#telNo").val(message.ANI); // OtherDN internal, message.attachdata.u_ani = outbound
    f.find("#telNo").val(message.CallType == "Inbound" ? message.ANI : message.DNIS);
    f.find("#skillSet").val(message.attachdata.u_skillset);
    f.find("#outboundid").val(message.attachdata.OutboundID);
    f.find("#aliran").val(message.CallType == "Inbound" ? "IN" : "OUT");
    f.find("#interactionID").val(message.InteractionID);
    f.find("#uuid").val(message.CallUuid);
    f.find("#channel").val(InteractionHelper.InteractionChannel.phone);
    f.find("#jeniskempenid").val(message.attachdata.JenisKempenID);
    f.find("#remsdataid").val(message.attachdata.ReMSDataID);
    f.find("#ismisscall").val(message.attachdata.u_missedcall);
    f.find("#isselfselectoutbound").val(message.attachdata.IsSelfSelectOutbound);
    f.attr("target", windowName);

    f.submit();



    //f.find("#skillSet").val(message.attachdata._facebookActorId);
    //f.find("#skillSet").val(message.attachdata._facebookActorName);
}

function intChnChat(message) {
    /*
        //SetAttachData Example
        var mycollection = createUserData();
        mycollection.put("paramtest", "value1");
        SetAttachdataById(message.ConnectionID, mycollection);
        */
    var windowName = 'Int_ID' + message.InteractionID;
    InteractionHelper.FlagInteractionChannel(message.InteractionID,
        InteractionHelper.InteractionChannel.chat, windowName, message);

    //todo: set message object to localStorage, interaction ID as key

    var f = $('#popupForm');
    f[0].reset();
    f.find("#idTypeCode").val(message.attachdata.u_idtype); //  "1" = "NRIC"
    //f.find("#idTypeCode").val("1"); //  "1" = "NRIC"
    f.find("#idNo").val(message.attachdata.u_idnumber); // NRIC =  391118085574
    f.find("#skillSet").val(message.attachdata.u_skillset);
    f.find("#aliran").val(message.CallType == "Inbound" ? "IN" : "OUT");
    f.find("#interactionID").val(message.InteractionID);
    f.find("#email").val(message.attachdata.EmailAddress);
    f.find("#channel").val(InteractionHelper.InteractionChannel.chat);
    f.attr("target", windowName);

    f.submit();
}

function intChnCFF(message) {
    /*
        //SetAttachData Example
        var mycollection = createUserData();
        mycollection.put("paramtest", "value1");
        SetAttachdataById(message.ConnectionID, mycollection);
        */
    var windowName = 'Int_ID' + message.InteractionID;
    InteractionHelper.FlagInteractionChannel(message.InteractionID,
        InteractionHelper.InteractionChannel.cff, windowName, message);

    var skillset = "";
    if (message.attachdata.u_skillset) {
        skillset = "?ss=" + message.attachdata.u_skillset;
    }

    if (message.attachdata.activityid != null) {
        window.open('/Activity/Profile/Detail/' + message.attachdata.activityid + (skillset || ""), windowName);
    }
}

function intChnFacebook(message) {
    /*
        //SetAttachData Example
        var mycollection = createUserData();
        mycollection.put("paramtest", "value1");
        SetAttachdataById(message.ConnectionID, mycollection);
        */
    var windowName = 'Int_ID' + message.InteractionID;
    InteractionHelper.FlagInteractionChannel(message.InteractionID,
        InteractionHelper.InteractionChannel.socialmedia, windowName, message);


    var f = $('#popupForm');
    f[0].reset();
    //f.find("#idTypeCode").val(message.attachdata.u_idtype || message.attachdata.IDType); //  "6" = "FBId" 
    f.find("#idTypeCode").val("6"); //  "6" = "FBId" 
    f.find("#idNo").val(message.attachdata._facebookActorId || message.attachdata.IDNo); // NRIC =  391118085574
    f.find("#telNo").val(message.ANI); // OtherDN internal, message.attachdata.u_ani = outbound
    f.find("#skillSet").val(message.attachdata.u_skillset);
    f.find("#aliran").val(message.CallType == "Inbound" ? "IN" : "OUT");
    f.find("#interactionID").val(message.InteractionID);
    f.find("#uuid").val(message.CallUuid);
    f.find("#fbtwdisplayname").val(message.attachdata._facebookActorName);
    f.find("#channel").val(InteractionHelper.InteractionChannel.socialmedia);
    f.attr("target", windowName);
    f.submit();
}

function intChnTwitter(message) {
    /*
        //SetAttachData Example
        var mycollection = createUserData();
        mycollection.put("paramtest", "value1");
        SetAttachdataById(message.ConnectionID, mycollection);
        */
    var windowName = 'Int_ID' + message.InteractionID;
    InteractionHelper.FlagInteractionChannel(message.InteractionID,
        InteractionHelper.InteractionChannel.socialmedia, windowName, message);


    var f = $('#popupForm');
    f[0].reset();
    //f.find("#idTypeCode").val(message.attachdata.u_idtype || message.attachdata.IDType); //  "6" = "FBId"
    f.find("#idTypeCode").val("7"); //  "7" = "TWId" temporary hardcode
    f.find("#idNo").val(message.attachdata._twitterUserId || message.attachdata.IDNo); // NRIC =  391118085574
    f.find("#telNo").val(message.ANI); // OtherDN internal, message.attachdata.u_ani = outbound
    f.find("#skillSet").val(message.attachdata.u_skillset);
    f.find("#aliran").val(message.CallType == "Inbound" ? "IN" : "OUT");
    f.find("#interactionID").val(message.InteractionID);
    f.find("#uuid").val(message.CallUuid);
    f.find("#fbtwdisplayname").val(message.attachdata._twitterUserScreenName);
    f.find("#channel").val(InteractionHelper.InteractionChannel.socialmedia);
    f.attr("target", windowName);
    f.submit();
}

function openActivity(message) {

    var windowName = 'Int_ID' + message.InteractionID;
    InteractionHelper.FlagInteractionChannel(message.InteractionID,
        0, windowName, message);

    var skillset = "";
    if (message.attachdata.u_skillset) {
        skillset = "?ss=" + message.attachdata.u_skillset;
    }
	
	var activityid = "";
	if(message.MediaType == "email")
		activityid = message.attachdata.activityid;
	else
		activityid = message.attachdata.ACTIVITY_ID;
	
    if (activityid != null) {
        window.open('/Activity/Profile/Detail/' + activityid + (skillset || ""), windowName);
    }
}

function onEventRingingConsult(message) {
    /*
	 	This event is received only by the second Agent (in the consult transfer scenario)
		usually open the popup without create the activity
	*/
}
function onEventRingingOutbound(message) {
    printAllAttachData(message);
    intChnPhone(message);
}

//EventEstablished
function onEventEstablishedInbound(message) {
    /*
		open the ticket only if is not a transfer...
		just check an attach-data
	*/

    printAllAttachData(message);
    intChnPhone(message);
}
function onEventEstablishedInternal(message) {
    
}
function onEventEstablishedConsult(message) {
    //open the ticket without create the activity
}
function onEventEstablishedOutbound(message) {
    //printAllAttachData(message);
    //intChnPhone(message);
}
//EventHeld
function onEventHeldInbound(message) {
    InteractionHelper.FlagInteractionEnded(message.ConnID);
}
function onEventHeldInternal(message) {
}
function onEventHeldConsult(message) {
}
function onEventHeldOutbound(message) {
    InteractionHelper.FlagInteractionEnded(message.ConnID);
}
//EventRetrieved	
function onEventRetrievedInbound(message) {
}
function onEventRetrievedInternal(message) {
}
function onEventRetrievedConsult(message) {
}
function onEventRetrievedOutbound(message) {
}
//EventAttachedDataChanged
function onEventAttachedDataChangedInbound(message) {
}
function onEventAttachedDataChangedInternal(message) {
}
function onEventAttachedDataChangedConsult(message) {
}
function onEventAttachedDataChangedOutbound(message) {
}
//EventReleased
function onEventReleasedInbound(message) {
}
function onEventReleasedInternal(message) {
    InteractionHelper.FlagInteractionEnded(message.ConnID);
}
function onEventReleasedConsult(message) {
}
function onEventReleasedOutbound(message) {
}
//EventDialing
function onEventDialingInternal(message) {
}
function onEventDialingConsult(message) {
}
function onEventDialingOutbound(message) {
    printAllAttachData(message);
    intChnPhone(message);
}

//==================================================================
//Events MediaEmail
//==================================================================
function onEmailEventRingingInbound(message) {
}

function onEmailEventEstablishedInbound(message) {
    printAllAttachData(message);
    intChnCFF(message);
}

function onEmailEventReleasedInbound(message) {
    InteractionHelper.FlagInteractionEnded(message.InteractionID);
}

function onEmailEventReplyEstablishedOutbound(message) {
}

function onEmailEventReplyReleased(message) {
}

function onEmailEventReplyCancelled(message) {
}

function onEmailEventSessionInfo(message) {
}

//==================================================================
//Events MediaWorkbin
//==================================================================

function onWorkbinTakenOut(message) {
    //printAllAttachData(message);
    //openActivity(message);
}

function onEmailEventOpenedInbound(message) {
    printAllAttachData(message);
    openActivity(message);
    
}

function onTwitterEventOpenedInbound(message) {
    printAllAttachData(message);
    openActivity(message)    
}

//Add By Henry
function onTwitterdirectEventOpenedInbound(message) {
    printAllAttachData(message);
    openActivity(message)
}

function onTwitterdirectEventOpenedOutbound(message) {
    printAllAttachData(message);
    openActivity(message);
}

function onTwitterEventOpenedOutbound(message) {
    printAllAttachData(message);
    openActivity(message);
}
//Add by henry

function onFacebooksessionEventOpenedInbound(message) {
    printAllAttachData(message);
    openActivity(message);
}

//Add By Henry
function onFacebooksessionEventOpenedOutbound(message) {
    printAllAttachData(message);
    openActivity(message);
}

function onFacebookEventOpenedInbound(message) {
    printAllAttachData(message);
    openActivity(message);    
}

function onFacebookEventOpenedOutbound(message) {
    printAllAttachData(message);
    openActivity(message);
}
//Add By Henry

//Added On 10-07-2019 By Henry
//function onPreFacebooksessionEventOpenedInbound(message) {
//    printAllAttachData(message);
//    openActivity(message);
//}

//function onPreFacebooksessionEventOpenedOutbound(message) {
//    printAllAttachData(message);
//    openActivity(message);
//}

//function onPreFacebookEventOpenedInbound(message) {
//    printAllAttachData(message);
//    openActivity(message);
//}

//function onPreFacebookEventOpenedOutbound(message) {
//    printAllAttachData(message);
//    openActivity(message);
//}

//function onPreTwitterdirectEventOpenedOutbound(message) {
//    printAllAttachData(message);
//    openActivity(message);
//}

//function onPreTwitterdirectEventOpenedInbound(message) {
//    printAllAttachData(message);
//    openActivity(message);
//}
//Added On 10-07-2019 By Henry


function onWorkbinPlacedIn(message) {

}

function onWorkbinContent(message) {

}


//==================================================================
//Events MediaChat
//==================================================================
function onChatEventRingingInbound(message) {
}

function onChatEventRingingConsult(message) {
}

function onChatEventEstablishedInbound(message) {
    printAllAttachData(message);
    intChnChat(message);
}

function onChatEventEstablishedConsult(message) {
}

function onChatEventReleasedInbound(message) {
    InteractionHelper.FlagInteractionEnded(message.InteractionID);
}
function onChatEventReleasedConsult(message) {
}

function onChatEventTranscriptLink(message) {
    //message.TranscriptPath
}

function onChatEventPartyRemovedInbound(message) {
}

function onChatEventPartyAddedInbound(message) {
}

function onChatEventPartyChangedInbound(message) {
}

//==================================================================
//Events SMS
//==================================================================
function onSMSEventRingingInbound(message) {
}

function onSMSEventEstablishedInbound(message) {
}

function onSMSEventReleasedInbound(message) {
}

function onSMSEventSendMessage(message) {
    //message.SmsMessage
}

//==================================================================
//Events WorkItem
//==================================================================
function onWorkitemEventRingingInbound(message) {
}

function onWorkitemEventEstablishedInbound(message) {
}

function onWorkitemEventReleasedInbound(message) {
}

//==================================================================
//Events Facebook
//==================================================================
function onFacebookEventRingingInbound(message) {
    printAllAttachData(message);
}

function onFacebookEventEstablishedInbound(message) {
    printAllAttachData(message);
    intChnFacebook(message);
}

function onFacebookEventReleasedInbound(message) {
}

function onFacebookEventSessionInfo(message) {
}


function onFacebooksessionEventEstablishedInbound(message) {
    printAllAttachData(message);
    intChnFacebook(message);
}

function onFacebookPrivateMessageEventEstablishedOutbound(message) {
    printAllAttachData(message);
    intChnFacebook(message);
}

//==================================================================
//Events Twitter
//==================================================================
function onTwitterEventRingingInbound(message) {
    printAllAttachData(message);
}

function onTwitterEventEstablishedInbound(message) {
    printAllAttachData(message);
    intChnTwitter(message);
}

function onTwitterdirectEventEstablishedInbound(message) {
    printAllAttachData(message);
    intChnTwitter(message);
}

function onTwitterEventSessionInfo(message) {
}

function onTwitterEventReleasedInbound(message) {
}

function onTwitterEventReplyOutbound(message) {
    //alert("onTwitterEventReplyOutbound");
}

function onTwitterEventRetweetOutbound(message) {
}

function onTwitterEventDirectMessageOutbound(message) {
    printAllAttachData(message);
    intChnTwitter(message);
}

//==================================================================
//Events UserEvent
//==================================================================
function onEventUserEvent(message) {
}

function onPreviewRecord(message) {
    //printAllAttachData(message);
    //intChnPhone(message);
}

function onChainedRecord(message) {
}

function onRecordRejectAcknowledge(message) {
}

function onRecordProcessedAcknowledge(message) {
}

//==================================================================
//Events DelegateCommand
//==================================================================
function onDelegateCommand(message) {
    log.debug("======= onDelegateCommand ==========");
    if (message.Parameters.Device) {
        log.debugFormat("Device Name: {0}", message.Parameters.Device.Name);
    }
    /*
	//to authorize the command on IWS
	log.debugFormat("===== Execute message: {0}", message.ID);
	ExecuteDelegatedCommand(message.ID);
	// OR
	RemoveDelegatedCommand(message.ID);
	*/
}

//==================================================================
//Events InhibitCommand
//==================================================================
function onInhibitCommand(message) {
    log.debug("======= onInhibitCommand ==========");
    if (message.Parameters.Device) {
        log.debugFormat("Device Name: {0}", message.Parameters.Device.Name);
    }
}

//==================================================================
//Generic Events 
//==================================================================
function onSwitchInteraction(message) {
    log.debug("Called onSwitchInteraction: " + message);
}

function onWdeSwitchInteraction(message) {
    log.debug("Called onWdeSwitchInteraction: " + message);
}

