<template>
    <div
        v-if="dialogData && dialogData.id"
        class="dialog-wrapper"
        @drop.prevent="chatPasteDrag($event, 'drag')"
        @dragover.prevent="chatDragOver(true, $event)"
        @dragleave.prevent="chatDragOver(false, $event)">
        <slot name="head">
            <div class="dialog-nav" :class="{completed:$A.dialogCompleted(dialogData)}">
                <div class="dialog-avatar">
                    <template v-if="dialogData.type=='group'">
                        <i v-if="dialogData.group_type=='project'" class="taskfont icon-avatar project">&#xe6f9;</i>
                        <i v-else-if="dialogData.group_type=='task'" class="taskfont icon-avatar task">&#xe6f4;</i>
                        <Icon v-else class="icon-avatar" type="ios-people" />
                    </template>
                    <div v-else-if="dialogData.dialog_user" class="user-avatar"><UserAvatar :userid="dialogData.dialog_user.userid" :size="42"/></div>
                    <Icon v-else class="icon-avatar" type="md-person" />
                </div>
                <div class="dialog-title">
                    <div class="main-title">
                        <template v-for="tag in $A.dialogTags(dialogData)" v-if="tag.color != 'success'">
                            <Tag :color="tag.color" :fade="false">{{$L(tag.text)}}</Tag>
                        </template>
                        <h2>{{dialogData.name}}</h2>
                        <em v-if="peopleNum > 0">({{peopleNum}})</em>
                        <label v-if="dialogData.top_at" class="top-text">{{$L('置顶')}}</label>
                    </div>
                    <template v-if="dialogData.type === 'group'">
                        <div v-if="dialogData.group_type === 'project'" class="sub-title pointer" @click="openProject">
                            {{$L('项目聊天室')}} {{$L('打开项目管理')}}
                        </div>
                        <div v-else-if="dialogData.group_type === 'task'" class="sub-title pointer" @click="openTask">
                            {{$L('任务聊天室')}} {{$L('查看任务详情')}}
                        </div>
                    </template>
                </div>
                <template v-if="dialogData.type === 'group'">
                    <ETooltip v-if="dialogData.group_type === 'user'" placement="top" :openDelay="600" :content="$L('群设置')">
                        <i class="taskfont dialog-create" @click="groupInfoShow = true">&#xe6e9;</i>
                    </ETooltip>
                </template>
                <ETooltip v-else-if="dialogData.type === 'user'" placement="top" :content="$L('创建群组')">
                    <i class="taskfont dialog-create" @click="openCreateGroup">&#xe646;</i>
                </ETooltip>
            </div>
        </slot>
        <ScrollerY
            ref="scroller"
            class="dialog-scroller overlay-y"
            :style="{opacity: visible ? 1 : 0}"
            :auto-bottom="isAutoBottom"
            @on-scroll="chatScroll"
            static>
            <div ref="manageList" class="dialog-list">
                <ul>
                    <li v-if="dialogData.hasMorePages" class="history" @click="loadNextPage">{{$L('加载历史消息')}}</li>
                    <li v-else-if="dialogData.loading > 0 && dialogMsgList.length === 0" class="loading"><Loading/></li>
                    <li v-else-if="dialogMsgList.length === 0" class="nothing">{{$L('暂无消息')}}</li>
                    <li
                        v-for="item in dialogMsgList"
                        :id="'view_' + item.id"
                        :key="item.id"
                        :class="{self:item.userid == userId, 'history-tip': topId == item.id}">
                        <em v-if="topId == item.id" class="history-text">{{$L('历史消息')}}</em>
                        <div class="dialog-avatar">
                            <UserAvatar :userid="item.userid" :tooltipDisabled="item.userid == userId" :size="30"/>
                        </div>
                        <DialogView :msg-data="item" :dialog-type="dialogData.type"/>
                    </li>
                    <li
                        v-for="item in tempMsgList"
                        :id="'tmp_' + item.id"
                        :key="'tmp_' + item.id"
                        :class="{self:item.userid == userId}">
                        <div class="dialog-avatar">
                            <UserAvatar :userid="item.userid" :tooltipDisabled="item.userid == userId" :size="30"/>
                        </div>
                        <DialogView :msg-data="item" :dialog-type="dialogData.type"/>
                    </li>
                </ul>
            </div>
        </ScrollerY>
        <div :class="['dialog-footer', msgNew > 0 && dialogMsgList.length > 0 ? 'newmsg' : '']" @click="onActive">
            <div class="dialog-newmsg" @click="onToBottom">{{$L('有' + msgNew + '条新消息')}}</div>
            <div class="dialog-input">
                <slot name="inputBefore"/>
                <ChatInput
                    ref="input"
                    :dialog-id="dialogId"
                    v-model="msgText"
                    :maxlength="20000"
                    @on-focus="onEventFocus"
                    @on-blur="onEventBlur"
                    @on-more="onEventMore"
                    @on-send="sendMsg"
                    :placeholder="$L('输入消息...')"/>
                <slot name="inputAfter"/>
            </div>
            <DialogUpload
                ref="chatUpload"
                class="chat-upload"
                :dialog-id="dialogId"
                @on-progress="chatFile('progress', $event)"
                @on-success="chatFile('success', $event)"
                @on-error="chatFile('error', $event)"/>
        </div>
        <div v-if="dialogDrag" class="drag-over" @click="dialogDrag=false">
            <div class="drag-text">{{$L('拖动到这里发送')}}</div>
        </div>

        <!--拖动发送提示-->
        <Modal
            v-model="pasteShow"
            :title="$L(pasteTitle)"
            :cancel-text="$L('取消')"
            :ok-text="$L('发送')"
            :enter-ok="true"
            @on-ok="pasteSend">
            <ul class="dialog-wrapper-paste" :class="pasteWrapperClass">
                <li v-for="item in pasteItem">
                    <img v-if="item.type == 'image'" :src="item.result"/>
                    <div v-else>{{$L('文件')}}: {{item.name}} ({{$A.bytesToSize(item.size)}})</div>
                </li>
            </ul>
        </Modal>

        <!--创建群组-->
        <Modal
            v-model="createGroupShow"
            :title="$L('创建群组')"
            :mask-closable="false">
            <Form :model="createGroupData" label-width="auto" @submit.native.prevent>
                <FormItem prop="userids" :label="$L('群成员')">
                    <UserInput v-model="createGroupData.userids" :uncancelable="createGroupData.uncancelable" :multiple-max="100" :placeholder="$L('选择项目成员')"/>
                </FormItem>
                <FormItem prop="chat_name" :label="$L('群名称')">
                    <Input v-model="createGroupData.chat_name" :placeholder="$L('输入群名称（选填）')"/>
                </FormItem>
            </Form>
            <div slot="footer" class="adaption">
                <Button type="default" @click="createGroupShow=false">{{$L('取消')}}</Button>
                <Button type="primary" :loading="createGroupLoad > 0" @click="onCreateGroup">{{$L('创建')}}</Button>
            </div>
        </Modal>

        <!--群设置-->
        <DrawerOverlay
            v-model="groupInfoShow"
            placement="right"
            :size="380">
            <DialogGroupInfo v-if="groupInfoShow" :dialogId="dialogId"/>
        </DrawerOverlay>
    </div>
</template>

<script>
import ScrollerY from "../../../components/ScrollerY";
import {mapState} from "vuex";
import DialogView from "./DialogView";
import DialogUpload from "./DialogUpload";
import {Store} from "le5le-store";
import UserInput from "../../../components/UserInput";
import DrawerOverlay from "../../../components/DrawerOverlay";
import DialogGroupInfo from "./DialogGroupInfo";
import ChatInput from "./ChatInput";

export default {
    name: "DialogWrapper",
    components: {ChatInput, DialogGroupInfo, DrawerOverlay, UserInput, DialogUpload, DialogView, ScrollerY},
    props: {
        dialogId: {
            type: Number,
            default: 0
        },
    },

    data() {
        return {
            visible: true,
            autoBottom: true,
            autoInterval: null,

            dialogDrag: false,
            inputFocus: false,

            msgText: '',
            msgNew: 0,
            topId: 0,

            tempMsgs: [],

            dialogMsgSubscribe: null,

            pasteShow: false,
            pasteFile: [],
            pasteItem: [],

            createGroupShow: false,
            createGroupData: {},
            createGroupLoad: 0,

            groupInfoShow: false,
        }
    },

    mounted() {
        this.dialogMsgSubscribe = Store.subscribe('dialogMsgPush', this.addDialogMsg);
    },

    beforeDestroy() {
        if (this.dialogMsgSubscribe) {
            this.dialogMsgSubscribe.unsubscribe();
            this.dialogMsgSubscribe = null;
        }
    },

    computed: {
        ...mapState([
            'isDesktop',
            'userId',
            'cacheDialogs',
            'dialogMsgs',
            'wsOpenNum',
        ]),

        dialogData() {
            return this.cacheDialogs.find(({id}) => id == this.dialogId) || {};
        },

        dialogMsgList() {
            if (!this.dialogId) {
                return [];
            }
            return $A.cloneJSON(this.dialogMsgs.filter(({dialog_id}) => {
                return dialog_id == this.dialogId;
            })).sort((a, b) => {
                return a.id - b.id;
            });
        },

        isAutoBottom() {
            if (this.inputFocus && !this.isDesktop) {
                return false;
            }
            return this.autoBottom
        },

        tempMsgList() {
            if (!this.dialogId) {
                return [];
            }
            return $A.cloneJSON(this.tempMsgs.filter(({dialog_id}) => {
                return dialog_id == this.dialogId;
            }));
        },

        peopleNum() {
            return this.dialogData.type === 'group' ? $A.runNum(this.dialogData.people) : 0;
        },

        pasteTitle() {
            const {pasteItem} = this;
            let hasImage = pasteItem.find(({type}) => type == 'image')
            let hasFile = pasteItem.find(({type}) => type != 'image')
            if (hasImage && hasFile) {
                return '发送文件/图片'
            } else if (hasImage) {
                return '发送图片'
            }
            return '发送文件'
        },

        pasteWrapperClass() {
            if (this.pasteItem.find(({type}) => type !== 'image')) {
                return ['multiple'];
            }
            return [];
        }
    },

    watch: {
        '$route': {
            handler (route) {
                if ($A.isJson(window.__sendDialogMsg) && window.__sendDialogMsg.time > $A.Time()) {
                    const {msgFile, msgText} = window.__sendDialogMsg;
                    window.__sendDialogMsg = null;
                    this.$nextTick(() => {
                        if ($A.isArray(msgFile) && msgFile.length > 0) {
                            this.sendFileMsg(msgFile);
                        } else if (msgText) {
                            this.sendMsg(msgText);
                        }
                    });
                }
                if (route.query && route.query._) {
                    let query = $A.cloneJSON(route.query);
                    delete query._;
                    this.goForward({query}, true);
                }
            },
            immediate: true
        },

        dialogId: {
            handler(id) {
                if (id) {
                    this.msgNew = 0;
                    this.topId = -1;
                    this.visible = false;
                    if (this.dialogMsgList.length > 0) {
                        setTimeout(_ => {
                            this.onToBottom();
                            this.visible = true;
                        }, 10);
                    }
                    let startTime = new Date().getTime();
                    this.$store.dispatch("getDialogMsgs", id).then(_ => {
                        setTimeout(_ => {
                            this.onToBottom();
                            this.visible = true;
                        }, Math.max(0, 100 - (new Date().getTime() - startTime)));
                    });
                }
            },
            immediate: true
        },

        wsOpenNum(num) {
            if (num <= 1) return
            this.$store.dispatch("getDialogMsgs", this.dialogId).catch(_ => {});
        }
    },

    methods: {
        sendMsg(text) {
            let msgText;
            if (typeof text === "string" && text) {
                msgText = text;
            } else {
                msgText = this.msgText;
                this.msgText = '';
            }
            if (msgText == '' && this.isDesktop) {
                this.$refs.input.focus();
                return;
            }
            msgText = msgText.replace(/<\/span> <\/p>$/, "</span></p>")
            //
            let tempId = $A.randomString(16);
            this.tempMsgs.push({
                id: tempId,
                dialog_id: this.dialogData.id,
                type: 'text',
                userid: this.userId,
                msg: {
                    text: msgText,
                },
            });
            if (!this.isDesktop) {
                this.$refs.input.blur();
            }
            this.onToBottom();
            this.onActive();
            //
            this.$store.dispatch("call", {
                url: 'dialog/msg/sendtext',
                data: {
                    dialog_id: this.dialogId,
                    text: msgText,
                },
                method: 'post'
            }).then(({data}) => {
                this.tempMsgs = this.tempMsgs.filter(({id}) => id != tempId)
                this.sendSuccess(data);
            }).catch(({msg}) => {
                $A.modalError(msg);
                this.tempMsgs = this.tempMsgs.filter(({id}) => id != tempId)
            });
        },

        sendFileMsg(files) {
            if (files.length > 0) {
                this.pasteFile = [];
                this.pasteItem = [];
                files.some(file => {
                    let reader = new FileReader();
                    reader.readAsDataURL(file);
                    reader.onload = ({target}) => {
                        this.pasteFile.push(file)
                        this.pasteItem.push({
                            type: $A.getMiddle(file.type, null, '/'),
                            name: file.name,
                            size: file.size,
                            result: target.result
                        })
                        this.pasteShow = true
                    }
                });
            }
        },

        chatPasteDrag(e, type) {
            this.dialogDrag = false;
            const files = type === 'drag' ? e.dataTransfer.files : e.clipboardData.files;
            const postFiles = Array.prototype.slice.call(files);
            if (postFiles.length > 0) {
                e.preventDefault();
                this.sendFileMsg(postFiles);
            }
        },

        chatDragOver(show, e) {
            let random = (this.__dialogDrag = $A.randomString(8));
            if (!show) {
                setTimeout(() => {
                    if (random === this.__dialogDrag) {
                        this.dialogDrag = show;
                    }
                }, 150);
            } else {
                if (e.dataTransfer.effectAllowed === 'move') {
                    return;
                }
                this.dialogDrag = true;
            }
        },

        pasteSend() {
            this.pasteFile.some(file => {
                this.$refs.chatUpload.upload(file)
            });
        },

        chatFile(type, file) {
            switch (type) {
                case 'progress':
                    this.tempMsgs.push({
                        id: file.tempId,
                        dialog_id: this.dialogData.id,
                        type: 'loading',
                        userid: this.userId,
                        msg: { },
                    });
                    if (!this.isDesktop) {
                        this.$refs.input.blur();
                    }
                    this.onToBottom();
                    this.onActive();
                    break;

                case 'error':
                    this.tempMsgs = this.tempMsgs.filter(({id}) => id != file.tempId)
                    break;

                case 'success':
                    this.tempMsgs = this.tempMsgs.filter(({id}) => id != file.tempId)
                    this.sendSuccess(file.data)
                    break;
            }
        },

        sendSuccess(data) {
            if ($A.isArray(data)) {
                data.some(item => {
                    this.sendSuccess(item)
                })
                return;
            }
            this.$store.dispatch("saveDialogMsg", data);
            this.$store.dispatch("increaseTaskMsgNum", this.dialogId);
            this.$store.dispatch("updateDialogLastMsg", data);
            this.onActive();
        },

        chatScroll(res) {
            switch (res.directionreal) {
                case 'up':
                    if (res.scrollE < 10) {
                        this.msgNew = 0;
                        this.autoBottom = true;
                    }
                    break;
                case 'down':
                    this.autoBottom = false;
                    break;
            }
            if (res.scale >= 1) {
                this.msgNew = 0;
                this.autoBottom = true;
            }
        },

        onEventFocus(e) {
            this.inputFocus = true;
            this.$emit("on-focus", e)
        },

        onEventBlur(e) {
            this.inputFocus = false;
            this.$emit("on-blur", e)
        },

        onEventMore(e) {
            if (['image', 'file'].includes(e)) {
                this.$refs.chatUpload.handleClick()
            }
        },

        onActive() {
            this.$emit("on-active");
        },

        onToBottom() {
            this.autoBottom = true;
            this.$refs.scroller && this.$refs.scroller.autoToBottom();
        },

        openProject() {
            if (!this.dialogData.group_info) {
                return;
            }
            this.goForward({name: 'manage-project', params: {projectId:this.dialogData.group_info.id}});
        },

        openTask() {
            if (!this.dialogData.group_info) {
                return;
            }
            this.$store.dispatch("openTask", this.dialogData.group_info.id);
        },

        loadNextPage() {
            let topId = this.dialogMsgList[0].id;
            this.$store.dispatch('getDialogMoreMsgs', this.dialogId).then(() => {
                this.$nextTick(() => {
                    this.topId = topId;
                    $A.scrollToView(document.getElementById("view_" + topId), {
                        behavior: 'instant',
                        inline: 'start',
                    })
                });
            }).catch(() => {})
        },

        addDialogMsg() {
            if (this.isAutoBottom) {
                this.$nextTick(this.onToBottom);
            } else {
                this.$nextTick(() => {
                    if (this.$refs.scroller && this.$refs.scroller.scrollInfo().scrollE > 10) {
                        this.msgNew++;
                    }
                })
            }
        },

        openCreateGroup() {
            this.createGroupData = {
                userids: this.dialogData.dialog_user ? [this.userId, this.dialogData.dialog_user.userid] : [this.userId],
                uncancelable: [this.userId]
            };
            this.createGroupShow = true;
        },

        onCreateGroup() {
            this.createGroupLoad++;
            this.$store.dispatch("call", {
                url: 'dialog/group/add',
                data: this.createGroupData
            }).then(({data, msg}) => {
                $A.messageSuccess(msg);
                this.createGroupShow = false;
                this.createGroupData = {};
                this.$store.dispatch("saveDialog", data);
                this.goForward({name: 'manage-messenger', params: {dialogId: data.id}});
            }).catch(({msg}) => {
                $A.modalError(msg);
            }).finally(_ => {
                this.createGroupLoad--;
            });
        },
    }
}
</script>
