import { AgoraRTMessage } from '$utils/socket/agoraRTM/agoraRTMIM';
import {RoomChatType} from '../components/const';
import verification,{VerificationType} from '$utils/verification/index';
export default <LibMixins>{

    inject:['getAgoraRTMIM','installSocketItem'],

    data(){
      return {
          // 房间公告通知
          noticeData:[],
          // 消息存储对象
          messageData:[],
          // 最大存储
          messageLength:100,
          // 消息
          message:'',
          // 表情模块
          emojiModal:false,
          // 最大输入
          maxLength:300,
      }
    },

    provide(){
        return {
            addMessage:(message:string)=> this.addMessage(message)
        }
    },

    methods:{

        addMessage(useMessage:string){
            let message = this.message + useMessage;

            if (this.message !== message) {
                this.message = message;
            }
        },

        // 打开emoji
        toggleEmoji(){
            this.emojiModal = !this.emojiModal;
        },

        // 隐藏模块
        hiddenModal(){
            this.emojiModal = false;
        },

        // 发送消息
        sendMessage(){

            if (verification.trigger[VerificationType.empty](this.message)) {

                let message = this.message;

                if (message.length >= this.maxLength) {
                    message = message.substr(0,this.maxLength);
                }

                let resultData = this.agoraRTMIM.addUserInfo({});
                // 隐藏模块
                this.hiddenModal();
                // 消息内容
                resultData.message = message;
                // 清空消息
                this.message = '';
                // 执行视图消息
                this.triggerPushMessage(AgoraRTMessage.chat,resultData);
                // 发送消息
                this.agoraRTMIM.send(resultData,AgoraRTMessage.chat);
            }

        },

        // 添加消息
        pushMessage(type,item,messageType){

            if (messageType === AgoraRTMessage.system) {
                this.noticeData.push({
                    type,
                    item,
                    messageType
                });
            } else {
                if (this.messageData.length > this.messageLength) {
                    this.messageData.unshift();
                }

                this.messageData.push({
                    type,
                    item,
                    messageType
                });
            }


            // 设置滚动条位置
            this.sendMessageScroll();

        },

        // 执行发送消息
        triggerPushMessage(type,item){
            if (RoomChatType[type]) {
                return this.pushMessage(RoomChatType[type],item,type);
            } else if (type === AgoraRTMessage.clearMessage) {
                return this.clearMessage();
            }
        },

        // 清空消息
        clearMessage(){
            if (this.messageData.length > 0) {
                this.messageData = [];
            }
        }


    },

    mounted() {
        // 聊天对象
        this.agoraRTMIM = this.getAgoraRTMIM();

        // 消息监听器
        this.messageCallback = function ({type,data}) {
            this.triggerPushMessage(type,data);
        }.bind(this);

        // 监听message
        this.agoraRTMIM.on(AgoraRTMessage.message,this.messageCallback);

        // 安装消息对象
        this.installSocketItem(this);
    },

    beforeUnmount() {
        this.agoraRTMIM.off(AgoraRTMessage.message,this.messageCallback);
        delete this.messageCallback;
    }

}
