<template>
    <csc-call-item
        v-if="item.type === 'call'"
        :call="item"
        :call-available="callAvailable"
        :block-incoming-label="blockIncomingLabel"
        :block-outgoing-label="blockOutgoingLabel"
        :block-both-label="blockBothLabel"
        :block-both-possible="unblockedBoth || blockedBoth"
        @start-call="startCall"
        @toggle-block-incoming="toggleBlockIncoming"
        @toggle-block-outgoing="toggleBlockOutgoing"
        @toggle-block-both="toggleBlockBoth"
        @add-to-phonebook="toggleAddToPhonebook"
    />
    <csc-fax-item
        v-else-if="item.type === 'fax'"
        :fax="item"
        :call-available="callAvailable"
        @download-fax="downloadFax"
        @start-call="startCall"
        @delete-fax="$emit('delete-fax', $event)"
    />
    <csc-voice-mail-item
        v-else-if="item.type === 'voicemail'"
        :voice-mail="item"
        :call-available="callAvailable"
        :block-incoming-label="blockIncomingLabel"
        :block-outgoing-label="blockOutgoingLabel"
        :block-both-label="blockBothLabel"
        :block-both-possible="unblockedBoth || blockedBoth"
        @download-voice-mail="downloadVoiceMail"
        @get-voicemail-transcript="getVoicemailTranscript"
        @play-voice-mail="playVoiceMail"
        @start-call="startCall"
        @toggle-block-incoming="toggleBlockIncoming"
        @toggle-block-outgoing="toggleBlockOutgoing"
        @toggle-block-both="toggleBlockBoth"
        @delete-voicemail="$emit('delete-voicemail', $event)"
    />
</template>

<script>
import CscCallItem from 'components/pages/Conversations/CscCallItem'
import CscFaxItem from 'components/pages/Conversations/CscFaxItem'
import CscVoiceMailItem from 'components/pages/Conversations/CscVoiceMailItem'
export default {
    name: 'CscConversationItem',
    components: {
        CscCallItem,
        CscFaxItem,
        CscVoiceMailItem
    },
    props: {
        item: {
            type: Object,
            default: null
        },
        callAvailable: {
            type: Boolean,
            default: false
        },
        blockedIncoming: {
            type: Boolean,
            default: false
        },
        blockedOutgoing: {
            type: Boolean,
            default: false
        }
    },
    emits: [
        'download-fax',
        'delete-voicemail',
        'get-voicemail-transcript',
        'toggle-block-both',
        'toggle-block-outgoing',
        'toggle-block-incoming',
        'start-call',
        'add-to-phonebook',
        'download-voice-mail',
        'play-voice-mail',
        'delete-fax'
    ],
    data () {
        return {}
    },
    computed: {
        number () {
            if (this.item.direction === 'out') {
                return this.item.callee
            }
            return this.item.caller
        },
        toggleActionIncoming () {
            return this.blockedIncoming ? 'unblock' : 'block'
        },
        toggleActionOutgoing () {
            return this.blockedOutgoing ? 'unblock' : 'block'
        },
        blockIncomingLabel () {
            if (this.blockedIncoming) {
                return this.$t('Unblock Incoming')
            }
            return this.$t('Block Incoming')
        },
        blockOutgoingLabel () {
            if (this.blockedOutgoing) {
                return this.$t('Unblock Outgoing')
            }
            return this.$t('Block Outgoing')
        },
        blockBothLabel () {
            if (this.blockedBoth) {
                return this.$t('Unblock Incoming/Outgoing')
            } else if (this.unblockedBoth) {
                return this.$t('Block Incoming/Outgoing')
            }
            return ''
        },
        blockedBoth () {
            return this.blockedIncoming && this.blockedOutgoing
        },
        unblockedBoth () {
            return !this.blockedIncoming && !this.blockedOutgoing
        }
    },
    methods: {
        startCall (number) {
            this.$emit('start-call', number)
        },
        downloadFax (fax) {
            this.$emit('download-fax', fax)
        },
        downloadVoiceMail (voiceMail) {
            this.$emit('download-voice-mail', voiceMail)
        },
        getVoicemailTranscript (voiceMail) {
            this.$emit('get-voicemail-transcript', voiceMail)
        },
        playVoiceMail (voiceMail) {
            this.$emit('play-voice-mail', voiceMail)
        },
        toggleBlockIncoming () {
            this.$emit('toggle-block-incoming', {
                number: this.number,
                type: this.toggleActionIncoming
            })
        },
        toggleBlockOutgoing () {
            this.$emit('toggle-block-outgoing', {
                number: this.number,
                type: this.toggleActionOutgoing
            })
        },
        toggleBlockBoth () {
            this.$emit('toggle-block-both', {
                number: this.number,
                type: this.toggleActionIncoming
            })
        },
        toggleAddToPhonebook (number) {
            this.$emit('add-to-phonebook', number)
        }
    }
}
</script>

<style lang="sass" rel="stylesheet/sass">
</style>
