<template>
    <div class="chat-input-wrapper" :class="modeClass" @click.stop="focus">
        <div ref="editor" class="no-dark-content" :style="editorStyle" @click.stop="" @paste="handlePaste"></div>
        <div class="chat-input-toolbar" @click.stop="">
            <slot name="toolbarBefore"/>

            <EPopover
                v-model="showEmoji"
                :visibleArrow="false"
                placement="top"
                popperClass="chat-input-emoji-popover">
                <ETooltip slot="reference" ref="emojiTip" :disabled="!$isDesktop || showEmoji" placement="top" :content="$L('表情')">
                    <i class="taskfont" @click="onToolbar('emoji')">&#xe7ad;</i>
                </ETooltip>
                <ChatEmoji @on-select="onSelectEmoji"/>
            </EPopover>

            <ETooltip placement="top" :disabled="!$isDesktop" :content="$L('选择会员')">
                <i class="taskfont" @click="onToolbar('user')">&#xe78f;</i>
            </ETooltip>
            <ETooltip placement="top" :disabled="!$isDesktop" :content="$L('选择任务')">
                <i class="taskfont" @click="onToolbar('task')">&#xe7d6;</i>
            </ETooltip>

            <EPopover
                v-model="showMore"
                :visibleArrow="false"
                placement="top"
                popperClass="chat-input-more-popover">
                <ETooltip slot="reference" ref="moreTip" :disabled="!$isDesktop || showMore" placement="top" :content="$L('展开')">
                    <i class="taskfont" @click="onToolbar('more')">&#xe790;</i>
                </ETooltip>
                <div class="chat-input-popover-item" @click="onToolbar('image')">
                    <i class="taskfont">&#xe64a;</i>
                    {{$L('图片')}}
                </div>
                <div class="chat-input-popover-item" @click="onToolbar('file')">
                    <i class="taskfont">&#xe786;</i>
                    {{$L('文件')}}
                </div>
            </EPopover>

            <div class="toolbar-spacing"></div>

            <Loading v-if="loading"/>
            <ETooltip v-else placement="top" :disabled="!$isDesktop" :content="$L('发送')"><Icon :class="[value ? '' : 'disabled']" type="md-send" @click="send"/></ETooltip>

            <slot name="toolbarAfter"/>
        </div>
    </div>
</template>

<script>
import {mapGetters, mapState} from "vuex";

import Quill from 'quill';
import "quill-mention";
import ChatEmoji from "./emoji";

export default {
    name: 'ChatInput',
    components: {ChatEmoji},
    props: {
        dialogId: {
            type: Number,
            default: 0
        },
        taskId: {
            type: Number,
            default: 0
        },
        value: {
            type: [String, Number],
            default: ''
        },
        placeholder: {
            type: String,
            default: ''
        },
        disabled: {
            type: Boolean,
            default: false
        },
        loading: {
            type: Boolean,
            default: false
        },
        enterSend: {
            type: Boolean,
            default: true
        },
        options: {
            type: Object,
            default: () => ({})
        },
        maxlength: {
            type: Number
        },
        defaultMenuOrientation: {
            type: String,
            default: "top"
        },
    },
    data() {
        return {
            quill: null,
            _content: '',
            _options: {},

            modeClass: '',

            userList: null,
            taskList: null,

            showMore: false,
            showEmoji: false,

            observer: null,
            wrapperWidth: 0,
            editorHeight: 0,

            timerScroll: null,
            isSpecVersion: this.checkIOSVersion(),
        };
    },
    mounted() {
        this.init();
        //
        this.observer = new ResizeObserver(entries => {
            entries.some(({target, contentRect}) => {
                if (target === this.$el) {
                    this.wrapperWidth = contentRect.width;
                } else if (target === this.$refs.editor) {
                    this.editorHeight = contentRect.height;
                }
            })
        });
        this.observer.observe(this.$el);
        this.observer.observe(this.$refs.editor);
    },
    beforeDestroy() {
        this.inputCache(this.dialogId, this.value);
        if (this.quill) {
            this.quill = null
        }
        if (this.observer) {
            this.observer.disconnect()
            this.observer = null
        }
    },
    computed: {
        ...mapState(['dialogInputCache', 'cacheProjects', 'cacheTasks', 'cacheUserBasic', 'userId']),

        editorStyle() {
            const {wrapperWidth, editorHeight} = this;
            if (wrapperWidth > 0
                && editorHeight > 0
                && (wrapperWidth < 280 || editorHeight > 40)) {
                return {
                    width: '100%'
                };
            } else {
                return {};
            }
        }
    },
    watch: {
        // Watch content change
        value(newVal) {
            if (this.quill) {
                if (newVal && newVal !== this._content) {
                    this._content = newVal
                    this.setContent(newVal)
                } else if(!newVal) {
                    this.quill.setText('')
                }
            }
        },

        // Watch disabled change
        disabled(newVal) {
            if (this.quill) {
                this.quill.enable(!newVal)
            }
        },

        // Reset lists
        dialogId(id1, id2) {
            this.userList = null;
            this.taskList = null;
            this.inputCache(id2, this.value)
            this.$emit('input', this.inputCache(id1))
        },
        taskId() {
            this.userList = null;
            this.taskList = null;
        },

        showEmoji(val) {
            if (!val && this.$refs.emojiTip) {
                this.$refs.emojiTip.updatePopper()
            }
        },

        showMore(val) {
            if (!val && this.$refs.moreTip) {
                this.$refs.moreTip.updatePopper()
            }
        },

        dialogInputCache() {
            this.$emit('input', this.inputCache(this.dialogId))
        }
    },
    methods: {
        init() {
            // Options
            this._options = Object.assign({
                theme: 'bubble',
                readOnly: false,
                placeholder: this.placeholder,
                modules: {
                    toolbar: [
                        ['bold', 'strike', 'italic', 'underline', {'list': 'ordered'}, {'list': 'bullet'}, 'blockquote', 'code-block']
                    ],
                    keyboard: {
                        bindings: {
                            'short enter': {
                                key: 13,
                                shortKey: true,
                                handler: _ => {
                                    if (!this.enterSend) {
                                        this.send();
                                        return false;
                                    }
                                    return true;
                                }
                            },
                            'enter': {
                                key: 13,
                                shiftKey: false,
                                handler: _ => {
                                    if (this.enterSend) {
                                        this.send();
                                        return false;
                                    }
                                    return true;
                                }
                            }
                        }
                    },
                    mention: {
                        allowedChars: /^\S*$/,
                        mentionDenotationChars: ["@", "#"],
                        defaultMenuOrientation: this.defaultMenuOrientation,
                        isolateCharacter: true,
                        renderItem: (data) => {
                            if (data.disabled === true) {
                                return `<div class="mention-item-disabled">${data.value}</div>`;
                            }
                            if (data.id === 0) {
                                return `<div class="mention-item-at">@</div><div class="mention-item-name">${data.value}</div><div class="mention-item-tip">${this.$L('提示所有成员')}</div>`;
                            }
                            if (data.avatar) {
                                return `<div class="mention-item-img${data.online ? ' online' : ''}"><img src="${data.avatar}"/><em></em></div><div class="mention-item-name">${data.value}</div>`;
                            }
                            return `<div class="mention-item-name" title="${data.value}">${data.value}</div>`;
                        },
                        renderLoading: () => {
                            return "Loading...";
                        },
                        source: (searchTerm, renderList, mentionChar) => {
                            this.getSource(mentionChar).then(array => {
                                let values = [];
                                array.some(item => {
                                    let list = item.list;
                                    if (searchTerm && !item.ignoreSearch) {
                                        list = list.filter(({value}) => $A.strExists(value, searchTerm));
                                    }
                                    if (list.length > 0 || item.ignoreSearch) {
                                        item.label && values.push(...item.label)
                                        list.length > 0 && values.push(...list)
                                    }
                                })
                                renderList(values, searchTerm);
                            })
                        }
                    }
                }
            }, this.options)

            // Instance
            this.quill = new Quill(this.$refs.editor, this._options)
            this.quill.enable(false)

            // Set editor content
            if (this.value) {
                this.setContent(this.value)
            } else {
                this.$emit('input', this.inputCache(this.dialogId))
            }

            // Disabled editor
            if (!this.disabled) {
                this.quill.enable(true)
            }

            // Mark model as touched if editor lost focus
            this.quill.on('selection-change', range => {
                if (this.timerScroll) {
                    clearInterval(this.timerScroll);
                }
                if (!range) {
                    this.$emit('on-blur', this.quill)
                } else {
                    this.$emit('on-focus', this.quill)
                    this.showEmoji = false
                    this.showMore = false
                    if (this.isSpecVersion) {
                        // ios11.0-11.3 对scrollTop及scrolIntoView解释有bug
                        // 直接执行会导致输入框滚到底部被遮挡
                    } else {
                        setTimeout(() => {
                            $A.scrollToView(this.$refs.editor, true)
                            this.timerScroll = setInterval(() => {
                                $A.scrollToView(this.$refs.editor, true)
                            }, 300);
                        }, 300);
                    }
                }
            })

            // Update model if text changes
            this.quill.on('text-change', _ => {
                if (this.maxlength > 0 && this.quill.getLength() > this.maxlength) {
                    this.quill.deleteText(this.maxlength, this.quill.getLength());
                }
                let html = this.$refs.editor.children[0].innerHTML
                html = html.replace(/^(<p><br><\/p>)+|(<p><br><\/p>)+$/gi, '')
                const quill = this.quill
                const text = this.quill.getText()
                this._content = html
                this.$emit('input', this._content)
                this.$emit('on-change', { html, text, quill })
            })

            // Emit ready event
            this.$emit('on-ready', this.quill)
        },

        setText(value) {
            if (this.quill) {
                this.quill.setText(value)
            }
        },

        setContent(value) {
            if (this.quill) {
                this.quill.setContents(this.quill.clipboard.convert(value))
            }
        },

        inputCache(key, cache) {
            if (cache === undefined) {
                const item = this.dialogInputCache.find(item => item.key == key);
                return item ? item.cache : '';
            }
            const index = this.dialogInputCache.findIndex(item => item.key == key);
            const data = {key, cache}
            if (index > -1) {
                this.$store.state.dialogInputCache.splice(index, 1, data)
            } else {
                this.$store.state.dialogInputCache.push(data)
            }
            setTimeout(_ => {
                $A.setStorage("cacheDialogInput", this.$store.state.dialogInputCache);
            })
        },

        focus() {
            this.$nextTick(() => {
                this.quill && this.quill.focus()
            })
        },

        blur() {
            this.$nextTick(() => {
                this.quill && this.quill.blur()
            })
        },

        send() {
            this.$emit('on-send')
        },

        onSelectEmoji(item) {
            if (!this.quill) {
                return;
            }
            if (item.type === 'emoji') {
                let element = document.createElement('span');
                element.innerHTML = item.html;
                this.quill.insertText(this.quill.getSelection(true).index, element.innerHTML);
                element = null;
            } else if (item.type === 'emoticon') {
                this.$emit('on-send', `<img class="emoticon" data-asset="${item.asset}" data-name="${item.name}" src="${item.src}"/>`)
            }
            this.showEmoji = false;
        },

        onToolbar(action) {
            if (action !== 'emoji') {
                this.showEmoji = false;
            }
            if (action !== 'more') {
                this.showMore = false;
            }
            switch (action) {
                case 'user':
                    this.openMenu("@");
                    break;

                case 'task':
                    this.openMenu("#");
                    break;

                case 'image':
                case 'file':
                    this.$emit('on-more', action)
                    break;
            }
        },

        onMoreVisibleChange(v) {
            this.showMore = v;
        },

        openMenu(char) {
            if (!this.quill) {
                return;
            }
            if (this.value.length === 0 || this.value.endsWith("<p><br></p>")) {
                this.quill.getModule("mention").openMenu(char);
            } else {
                let str = this.value.replace(/<[^>]+>/g,"");
                if (str.length === 0 || str.endsWith(" ")) {
                    this.quill.getModule("mention").openMenu(char);
                } else {
                    this.quill.getModule("mention").openMenu(` ${char}`);
                }
            }
        },

        getProjectId() {
            let object = null;
            if (this.dialogId > 0) {
                object = this.cacheProjects.find(({dialog_id}) => dialog_id == this.dialogId);
                if (object) {
                    return object.id;
                }
                object = this.cacheTasks.find(({dialog_id}) => dialog_id == this.dialogId);
                if (object) {
                    return object.project_id;
                }
            } else if (this.taskId > 0) {
                object = this.cacheTasks.find(({id}) => id == this.taskId);
                if (object) {
                    return object.project_id;
                }
            }
            return 0;
        },

        getSource(mentionChar) {
            return new Promise(resolve => {
                switch (mentionChar) {
                    case "@": // @成员
                        this.modeClass = "user-mention";
                        if (this.userList !== null) {
                            resolve(this.userList)
                            return;
                        }
                        const atCallback = (list) => {
                            if (list.length > 2) {
                                this.userList = [{
                                    ignoreSearch: true,
                                    label: null,
                                    list: [{id: 0, value: this.$L('所有人')}]
                                }, {
                                    ignoreSearch: false,
                                    label: [{id: 0, value: this.$L('会话内成员'), disabled: true}],
                                    list,
                                }]
                            } else {
                                this.userList = [{
                                    ignoreSearch: false,
                                    label: null,
                                    list
                                }]
                            }
                            resolve(this.userList)
                        }
                        let array = [];
                        if (this.dialogId > 0) {
                            // 根据会话ID获取成员
                            this.$store.dispatch("call", {
                                url: 'dialog/group/user',
                                data: {
                                    dialog_id: this.dialogId,
                                    getuser: 1
                                }
                            }).then(({data}) => {
                                if (data.length > 0) {
                                    array.push(...data.map(item => {
                                        return {
                                            id: item.userid,
                                            value: item.nickname,
                                            avatar: item.userimg,
                                            online: item.online,
                                        }
                                    }))
                                }
                                atCallback(array)
                            }).catch(_ => {
                                atCallback(array)
                            });
                        } else if (this.taskId > 0) {
                            // 根据任务ID获取成员
                            const task = this.cacheTasks.find(({id}) => id == this.taskId)
                            if (task && $A.isArray(task.task_user)) {
                                task.task_user.some(tmp => {
                                    let item = this.cacheUserBasic.find(({userid}) => userid == tmp.userid);
                                    if (item) {
                                        array.push({
                                            id: item.userid,
                                            value: item.nickname,
                                            avatar: item.userimg,
                                            online: item.online,
                                        })
                                    }
                                })
                            }
                            atCallback(array)
                        }
                        break;

                    case "#": // #任务
                        this.modeClass = "task-mention";
                        if (this.taskList !== null) {
                            resolve(this.taskList)
                            return;
                        }
                        const taskCallback = (list) => {
                            this.taskList = [];
                            // 项目任务
                            if (list.length > 0) {
                                list = list.map(item => {
                                    return {
                                        id: item.id,
                                        value: item.name
                                    }
                                })
                                this.taskList.push({
                                    ignoreSearch: false,
                                    label: [{id: 0, value: this.$L('项目未完成任务'), disabled: true}],
                                    list,
                                })
                            }
                            // 待完成任务
                            let data = this.$store.getters.transforTasks(this.$store.getters.dashboardTask['all']);
                            if (data.length > 0) {
                                data = data.sort((a, b) => {
                                    return $A.Date(a.end_at || "2099-12-31 23:59:59") - $A.Date(b.end_at || "2099-12-31 23:59:59");
                                })
                                this.taskList.push({
                                    ignoreSearch: false,
                                    label: [{id: 0, value: this.$L('我的待完成任务'), disabled: true}],
                                    list: data.map(item => {
                                        return {
                                            id: item.id,
                                            value: item.name
                                        }
                                    }),
                                })
                            }
                            resolve(this.taskList)
                        }
                        //
                        const projectId = this.getProjectId();
                        if (projectId > 0) {
                            this.$store.dispatch("getTaskForProject", projectId).then(_ => {
                                let tasks = this.cacheTasks.filter(task => {
                                    if (task.archived_at) {
                                        return false;
                                    }
                                    return task.project_id == projectId
                                        && task.parent_id === 0
                                        && !task.archived_at
                                        && !task.complete_at
                                })
                                if (tasks.length > 0) {
                                    taskCallback(tasks);
                                } else {
                                    taskCallback([])
                                }
                            }).catch(_ => {
                                taskCallback([])
                            })
                            return;
                        }
                        taskCallback([])
                        break;

                    default:
                        resolve([])
                        break;
                }
            })
        },

        checkIOSVersion() {
            let ua = window && window.navigator && window.navigator.userAgent;
            let match = ua.match(/OS ((\d+_?){2,3})\s/i);
            let IOSVersion = match ? match[1].replace(/_/g, ".") : "unknown";
            const iosVsn = IOSVersion.split(".");
            return +iosVsn[0] == 11 && +iosVsn[1] >= 0 && +iosVsn[1] < 3;
        },

        handlePaste(e) {
            const {files} = e.clipboardData;
            const postFiles = Array.prototype.slice.call(files).filter(file => !$A.leftExists(file.type, 'image/'));
            if (postFiles.length > 0) {
                e.preventDefault()
                this.$emit('on-file', postFiles)
            }
        }
    }
}
</script>
