
// NOTE : localStorage Event will not trigger on the same browser that perform the change
var InteractionHelper = {
    ////
    InteractionChannel: {
        chat: 1,
        phone: 5,
        socialmedia: 7,
        cff: 8
    },
    FlagInteractionEnded: function (interactionId) {
        localStorage.removeItem("Int_ID" + interactionId);
        localStorage.removeItem("Int_ID_Disp" + interactionId);
        localStorage.removeItem("CrmApp.Int_ID" + interactionId);
        localStorage.removeItem("Out_Call" + interactionId);
        localStorage.removeItem("Int_ID_Attach" + interactionId);
    },
    FlagInteractionChannel: function (interactionId, channelId, windowName, message) {
        switch (channelId) {
            case this.InteractionChannel.chat:
                localStorage.setItem("crmApp.Int_ID" + interactionId, JSON.stringify({
                    ChannelID: channelId,
                    IDType: message.attachdata.idtype,
                    IDNo: message.attachdata.u_idnumber,
                    Email: message.attachdata.EmailAddress,
                    Aliran: message.CallType == "Inbound" ? "IN" : "OUT",
                    Skillset: message.attachdata.u_skillset,
                    WindowNames: [windowName]
                }));
                break;

            case this.InteractionChannel.cff:
                localStorage.setItem("crmApp.Int_ID" + interactionId, JSON.stringify({
                    ChannelID: channelId,
                    Email: message.attachdata.EmailAddress,
                    Aliran: message.CallType == "Inbound" ? "IN" : "OUT",
                    WindowNames: [windowName]
                }));
                break;

            case this.InteractionChannel.phone:
                localStorage.setItem("crmApp.Int_ID" + interactionId, JSON.stringify({
                    ChannelID: channelId,
                    IDType: message.attachdata.u_idtype,
                    IDNo: message.attachdata.u_idnumber,
                    PhoneNo: (message.CallType == "Inbound" ? message.ANI : message.DNIS), //message.ANI,
                    Aliran: message.CallType == "Inbound" ? "IN" : "OUT",
                    Skillset: message.attachdata.u_skillset,
                    WindowNames: [windowName]
                }));
                break;

            case this.InteractionChannel.socialmedia:
                localStorage.setItem("crmApp.Int_ID" + interactionId, JSON.stringify({
                    ChannelID: channelId,
                    IDType: message.attachdata.u_idtype,
                    IDNo: ((message.attachdata.u_idtype == 6) ? message.attachdata._facebookActorId : message.attachdata._twitterUserId),
                    FBTWDisplayName: ((message.attachdata.u_idtype == 6) ? message.attachdata._facebookActorName : message.attachdata._twitterUserScreenName),
                    Aliran: message.CallType == "Inbound" ? "IN" : "OUT",
                    Skillset: message.attachdata.u_skillset,
                    WindowNames: [windowName]
                }));
                break;

        }
    },
    CheckInteractionStatus: function (onChangeEvent, interactionId) {
        if (onChangeEvent.key == "Int_ID" + interactionId && onChangeEvent.newValue != null && onChangeEvent.newValue.length > 0) {
            var actChannelID = parseInt(onChangeEvent.newValue);
            return isNaN(actChannelID) ? null : actChannelID;
        } else return null;
    },
    CreateOutboundCall: function (callProps) {
        localStorage.setItem("Out_Call" + callProps.ID, JSON.stringify(callProps));
    },
    ManageOutboundCall: function (onChangeEvent) {
        if (onChangeEvent.key.startsWith("Out_Call") && onChangeEvent.newValue != null && onChangeEvent.newValue.length > 0) {
            return JSON.parse(onChangeEvent.newValue);
        }
    },
    FlagOutboundCallDone: function (Out_Call_Key) {
        localStorage.removeItem(Out_Call_Key);
    },
    /////
    UpdateDisposition: function (interactionId, dispositionId) {
        localStorage.setItem("Int_ID_Disp" + interactionId, dispositionId);
    },
    CheckDispositionStatus: function (onChangeEvent) {
        if (onChangeEvent.key.startsWith("Int_ID_Disp") && onChangeEvent.newValue != null && onChangeEvent.newValue.length > 0) {
            var intID = onChangeEvent.key.split("Int_ID_Disp");
            if (intID.length > 1) {
                return {
                    interactionId: intID[1],
                    dispositionId: onChangeEvent.newValue
                }
            } else console.warn("Failed to extract disposition code");

            console.log(onChangeEvent.newValue, " - Set Disposition Code");
        }
    },
    ////
    UpdateAttachData: function (interactionId, params) {
        localStorage.setItem("Int_ID_Attach" + interactionId, params);
    },
    CheckAttachDataStatus: function (onChangeEvent) {
        if (onChangeEvent.key.startsWith("Int_ID_Attach") && onChangeEvent.newValue != null && onChangeEvent.newValue.length > 0) {
            var intID = onChangeEvent.key.split("Int_ID_Attach");
            var param = onChangeEvent.newValue.split("|");
            var attachdata = createUserData();
            if (param.length > 1) {
                attachdata.put("GSW_CALL_RESULT", param[0]);
                attachdata.put("ACTIVITY_ID", param[1]);
            }

            if (intID.length > 1) {
                return {
                    interactionId: intID[1],
                    params: attachdata
                }
            } else console.warn("Failed to extract attach data");

            console.log(onChangeEvent.newValue, " - Set Attach Data");
        }
    },
    Clear: function () {
        // Clear previous un-cleared Interaction tracking;
        var intKeyPrefix = "Int_ID" // Int_ID1234123412341234;
        for (var key in localStorage) {
            if (object.hasOwnProperty(key) &&
                key.toString().startsWith(intKeyPrefix)) {
                localStorage.removeItem(key);
            }
        }

    }
};