import { namespace } from "vuex-class";

import * as dto from "../model";
import { Chat as ChatType } from "../xim/models/chat";
import * as chatDto from "../xim/models/chat";
export enum ChatStatus {
    opening = 0,
    terminated = 1,
}
export enum ChatMemberType {
    member = "25",
    admin = "85",
}
export namespace ChatStore {
    export const ns = "chatStore";
    /* state */
    export const STATE_CHAT_DIALOG_VISIBLE = "会话模块弹窗显示状态";
    export type STATE_CHAT_DIALOG_VISIBLE = boolean;
    export const STATE_CHAT_DIALOG_IS_SINGLE = "会话模块是否是单个弹窗";
    export type STATE_CHAT_DIALOG_IS_SINGLE = boolean;
    export const STATE_MY_CHAT_ROOM_LIST = "我的会话列表";
    export type STATE_MY_CHAT_ROOM_LIST = {
        list: ChatType[];
        total: number;
    } | null;
    export const STATE_SINGLE_CHAT = "单独的会话";
    export type STATE_SINGLE_CHAT = ChatType | null;
    export const STATE_CHAT_MSG_HISTORY = "某个会话聊天记录";
    export type STATE_CHAT_MSG_HISTORY = dto.MessageRequestResult | null;

    export const STATE_CHAT_SENDING_MESSAGES = "sendingMessages";
    export type STATE_CHAT_SENDING_MESSAGES = dto.MessageRequestResult | null;
    export type STATE_CHAT_SENDING_MESSAGE = dto.Message;

    export const STATE_CHAT_CURRENT_CHAT_ID = "当前chat-id";
    export type STATE_CHAT_CURRENT_CHAT_ID = number | null;
    export const STATE_CHAT_CURRENT_CHAT_VERSION = "当前chat的Uniplat version";
    export type STATE_CHAT_CURRENT_CHAT_VERSION = number | null;
    export const STATE_CHAT_CURRENT_IS_CHAT_MEMBER = "是否是当前chat的成员";
    export type STATE_CHAT_CURRENT_IS_CHAT_MEMBER = boolean;
    export const STATE_CHAT_CURRENT_USER_UID = "用户的UiplatId";
    export type STATE_CHAT_CURRENT_USER_UID = number | null;
    export const STATE_CHAT_CURRENT_USER_TYPE =
        "当前用户类型状态,25-普通用户,92-客服";
    export type STATE_CHAT_CURRENT_USER_TYPE = string | null;

    export const STATE_CHAT_CURRENT_IS_CHAT_ERROR = "当前会话是否错误";
    export type STATE_CHAT_CURRENT_IS_CHAT_ERROR = number | null;

    export const STATE_CHAT_CURRENT_CHAT_UNIPLAT_ID = "当前chat的Uniplat id";
    export type STATE_CHAT_CURRENT_CHAT_UNIPLAT_ID = string | null;

    export const STATE_CHAT_USERNAME = "会话用户id-name";
    export type STATE_CHAT_USERNAME = { [key: string]: string };

    export const STATE_CHAT_MY_ID = "聊天窗口显示在右边那个人的id";
    export type STATE_CHAT_MY_ID = string | null;
    export const STATE_CHAT_MY_UID = "聊天窗口显示在右边那个人的uid";
    export type STATE_CHAT_MY_UID = string | null;

    export const STATE_CHAT_SOURCE = "stateChatSource";
    export type STATE_CHAT_SOURCE = dto.ServiceType;

    export const STATE_CURRENT_CHAT_INPUTING = "当前会话正在输入的人";
    export type STATE_CURRENT_CHAT_INPUTING = string[];

    export const STATE_CURRENT_CHAT_INITING = "当前会是否正在初始化";
    export type STATE_CURRENT_CHAT_INITING = boolean;

    export const STATE_CHAT_SEND_FAIL_MESSAGE = "最新一条发送失败消息";
    export type STATE_CHAT_SEND_FAIL_MESSAGE = string | null;

    export const STATE_CURRENT_CHAT_MEMBERS = "当前会话参与者";
    export type STATE_CURRENT_CHAT_MEMBERS =
        | readonly (dto.ChatMember & dto.ChatMemberExtraInfo)[]
        | null;

    export const STATE_CURRENT_CHAT_TITLE = "会话标题";
    export type STATE_CURRENT_CHAT_TITLE = string;

    export const STATE_FUNC_SCROLL_TO_BOTTOM = "收到消息后滚动到底部的方法";
    export type STATE_FUNC_SCROLL_TO_BOTTOM = () => void;

    export const STATE_FUNC_ON_NEW_MSG = "收到消息回调方法";
    export type STATE_FUNC_ON_NEW_MSG = (e: chatDto.Message) => void;

    export const STATE_CURRENT_UNREAD_MESSAGE_COUNT = "当前未读消息数";
    export type STATE_CURRENT_UNREAD_MESSAGE_COUNT = number;

    /* getter */
    export const GETTER_CURRENT_CHAT_PRESENT_MEMBERS = "当前会话未退出的参与者";
    export type GETTER_CURRENT_CHAT_PRESENT_MEMBERS = dto.ChatMembers | null;
    export const GETTER_CURRENT_CURRENT_CHAT = "当前打开的会话";
    export type GETTER_CURRENT_CURRENT_CHAT = ChatType | null;

    /* mutation */
    export const MUTATION_SHOW_CHAT = "打开会话弹窗";
    export type MUTATION_SHOW_CHAT = () => void;

    export const MUTATION_HIDE_CHAT = "关闭会话弹窗";
    export type MUTATION_HIDE_CHAT = () => void;

    export const MUTATION_SAVE_CHAT_LIST = "保存我的会话列表";
    export type MUTATION_SAVE_CHAT_LIST = (
        list: STATE_MY_CHAT_ROOM_LIST
    ) => void;

    export const MUTATION_PUSH_CHAT_MSG_HISTORY = "新增下一页聊天记录";
    export type MUTATION_PUSH_CHAT_MSG_HISTORY = (
        list: ChatStore.STATE_CHAT_MSG_HISTORY
    ) => void;

    export const MUTATION_INITING_CHAT = "当前会话正在初始化";
    export type MUTATION_INITING_CHAT = () => void;
    export const MUTATION_INITING_CHAT_DONE = "当前会话初始完成";
    export type MUTATION_INITING_CHAT_DONE = () => void;

    export const MUTATION_UNSHIFT_CHAT_MSG_HISTORY = "新增上一页聊天记录";
    export type MUTATION_UNSHIFT_CHAT_MSG_HISTORY = (
        list: ChatStore.STATE_CHAT_MSG_HISTORY
    ) => void;

    export const MUTATION_CLEAR_CHAT_MSG_HISTORY = "清空聊天记录";
    export type MUTATION_CLEAR_CHAT_MSG_HISTORY = () => void;

    export const MUTATION_SAVE_SINGLE_CHAT = "设置单独的会话";
    export type MUTATION_SAVE_SINGLE_CHAT = (v: ChatType) => void;

    export const MUTATION_CLEAR_SINGLE_CHAT = "清空单独的会话";
    export type MUTATION_CLEAR_SINGLE_CHAT = () => void;

    export const MUTATION_SAVE_SEND_FAIL_MESSAGE = "更新最新一条发送失败消息";
    export type MUTATION_SAVE_SEND_FAIL_MESSAGE = (param: {
        msg: string;
        ts: number;
    }) => void;

    export const MUTATION_SAVE_USERNAME = "更新用户id-name";
    export type MUTATION_SAVE_USERNAME = (param: {
        id: string;
        name: string;
    }) => void;

    export const MUTATION_SAVE_CURRENT_CHAT_ID = "保存当前chat-id";
    export type MUTATION_SAVE_CURRENT_CHAT_ID = (
        chatId: ChatStore.STATE_CHAT_CURRENT_CHAT_ID
    ) => void;

    export const MUTATION_CLEAR_CURRENT_CHAT_ID = "清空chat-id";
    export type MUTATION_CLEAR_CURRENT_CHAT_ID = () => void;

    export const MUTATION_SET_CURRENT_USER_UID = "设置当前用户UniplatId";
    export type MUTATION_SET_CURRENT_USER_UID = (v: number) => void;

    export const MUTATION_SAVE_CURRENT_CHAT_VERSION =
        "保存当前chat uniplat version";
    export type MUTATION_SAVE_CURRENT_CHAT_VERSION = (
        v: ChatStore.STATE_CHAT_CURRENT_CHAT_VERSION
    ) => void;

    export const MUTATION_CLEAR_CURRENT_CHAT_VERSION =
        "清空chat uniplat version";
    export type MUTATION_CLEAR_CURRENT_CHAT_VERSION = () => void;
    export const MUTATION_SAVE_CURRENT_CHAT_UNIPLAT_ID =
        "保存当前chat uniplat id";
    export type MUTATION_SAVE_CURRENT_CHAT_UNIPLAT_ID = (
        v: ChatStore.STATE_CHAT_CURRENT_CHAT_UNIPLAT_ID
    ) => void;

    export const MUTATION_CLEAR_CURRENT_CHAT_UNIPLAT_ID = "清空chat uniplat id";
    export type MUTATION_CLEAR_CURRENT_CHAT_UNIPLAT_ID = () => void;

    export const MUTATION_WITHDRAW = "撤回";
    export type MUTATION_WITHDRAW = (id: number) => void;

    export const MUTATION_SAVE_MYSELF_ID =
        "保存我的id:聊天窗口显示在右边那个人的id";
    export type MUTATION_SAVE_MYSELF_ID = () => void;

    export const MUTATION_CLEAR_MYSELF_ID =
        "清空我的id:聊天窗口显示在右边那个人的id";
    export type MUTATION_CLEAR_MYSELF_ID = () => void;

    export const MUTATION_SET_CHAT_SOURCE = "setChatSource";
    export type MUTATION_SET_CHAT_SOURCE = (payload: dto.ServiceType) => void;

    export const MUTATION_SAVE_CURRENT_CHAT_MEMBERS = "保存当前会话参与者";
    export type MUTATION_SAVE_CURRENT_CHAT_MEMBERS = (
        params: ChatStore.STATE_CURRENT_CHAT_MEMBERS
    ) => void;

    export const MUTATION_CLEAR_CURRENT_CHAT_MEMBERS = "清空当前会话参与者";
    export type MUTATION_CLEAR_CURRENT_CHAT_MEMBERS = () => void;

    export const MUTATION_SAVE_CHAT_TITLE = "保存会话标题";
    export type MUTATION_SAVE_CHAT_TITLE = (
        title: ChatStore.STATE_CURRENT_CHAT_TITLE
    ) => void;

    export const MUTATION_CLEAR_CHAT_TITLE = "清空会话标题";
    export type MUTATION_CLEAR_CHAT_TITLE = () => void;

    export const MUTATION_SCROLL_TO_BOTTOM = "收到新消息后滚动到底部";
    export type MUTATION_SCROLL_TO_BOTTOM = () => void;

    export const MUTATION_SAVE_FUNC_SCROLL_TO_BOTTOM =
        "保存收到新消息后滚动到底部的方法";
    export type MUTATION_SAVE_FUNC_SCROLL_TO_BOTTOM = (
        func: () => void
    ) => void;
    export const MUTATION_CLEAR_FUNC_SCROLL_TO_BOTTOM =
        "删除收到新消息后滚动到底部的方法";
    export type MUTATION_CLEAR_FUNC_SCROLL_TO_BOTTOM = () => void;

    export const MUTATION_SAVE_FUNC_ON_NEW_MSG = "保存收到新消息后的方法";
    export type MUTATION_SAVE_FUNC_ON_NEW_MSG = (
        func: (e: dto.Message) => void
    ) => void;
    export const MUTATION_CLEAR_FUNC_ON_NEW_MSG = "删除收到新消息后的方法";
    export type MUTATION_CLEAR_FUNC_ON_NEW_MSG = () => void;

    export const MUTATION_APPEND_SENDING_MESSAGE = "appendSendingMessage";
    export type MUTATION_APPEND_SENDING_MESSAGE = (
        payload: dto.Message
    ) => void;

    export const MUTATION_REMOVE_SENDING_MESSAGE = "removeSendingMessage";
    export type MUTATION_REMOVE_SENDING_MESSAGE = (id: number) => void;

    export const MUTATION_FAILED_SENDING_MESSAGE = "failedSendingMessage";
    export type MUTATION_FAILED_SENDING_MESSAGE = (id: number) => void;

    export const MUTATION_SAVE_CURRENT_CHAT_INPUTING = "保存正在输入";
    export type MUTATION_SAVE_CURRENT_CHAT_INPUTING = (
        params: chatDto.NotifyMessage
    ) => void;
    export const MUTATION_CLEAR_CURRENT_CHAT_INPUTING = "清空正在输入";
    export type MUTATION_CLEAR_CURRENT_CHAT_INPUTING = () => void;

    export const MUTATION_CHAT_UPDATE_IS_MEMBER = "更新是否是当前成员";
    export type MUTATION_CHAT_UPDATE_IS_MEMBER = (v: boolean) => Promise<void>;

    export const MUTATION_CHAT_UPDATE_USER_TYPE = "更新当前用户的类型";
    export type MUTATION_CHAT_UPDATE_USER_TYPE = (v: string) => Promise<void>;

    /* action */
    export const ACTION_GET_MY_CHAT_LIST = "获取我的会话列表";
    export type ACTION_GET_MY_CHAT_LIST = (
        keyword?: string
    ) => Promise<ChatType[]>;

    export const ACTION_REBUILD_UNREAD_MESSAGE_COUNT = "重新计算未读消息数";
    export type ACTION_REBUILD_UNREAD_MESSAGE_COUNT = () => void;

    export const ACTION_JOIN_CHAT = "加入某个会话";
    export type ACTION_JOIN_CHAT = (chatId: number) => void;

    export const ACTION_GET_CHAT_MESSAGES = "打开某个会话时获取他的聊天记录";
    export type ACTION_GET_CHAT_MESSAGES =
        () => Promise<dto.MessageRequestResult>;

    export const ACTION_GET_CHAT_MESSAGES_BEFORE_SPECIFIC_ID =
        "获取某个消息之前的10条消息";
    export type ACTION_GET_CHAT_MESSAGES_BEFORE_SPECIFIC_ID = (
        msgId: number
    ) => Promise<dto.MessageRequestResult>;

    export const ACTION_GET_CHAT_MESSAGES_AFTER_SPECIFIC_ID =
        "获取某个消息之后的10条消息";
    export type ACTION_GET_CHAT_MESSAGES_AFTER_SPECIFIC_ID = (
        msgId: number
    ) => Promise<dto.MessageRequestResult>;

    export const ACTION_GET_FRESH_MESSAGE = "获取最新的消息";
    export type ACTION_GET_FRESH_MESSAGE =
        () => Promise<dto.MessageRequestResult>;

    export const ACTION_CREATE_NEW_CHAT_BY_SERVICE_MAN = "客服向顾客发起新会话";
    export type ACTION_CREATE_NEW_CHAT_BY_SERVICE_MAN = (params: {
        modelName: string;
        selectedListId: string;
        uids: string[];
        showByPage?: boolean;
    }) => Promise<{ chat: number; catalog: string }>;

    export const ACTION_CREATE_NEW_CHAT_BY_CLIENT = "顾客向客服发起新会话";
    export type ACTION_CREATE_NEW_CHAT_BY_CLIENT = (params: {
        modelName: string;
        selectedListId: string;
        uids: string[];
    }) => Promise<number>;

    export const ACTION_CREATE_NEW_CHAT_BY_CLIENT_SIDE =
        "startNewConversationByCustomerSide";
    export type ACTION_CREATE_NEW_CHAT_BY_CLIENT_SIDE = (option: {
        customerServiceId?: number | string;
        customerServiceGroupId?: number | string;
    }) => Promise<number>;

    export const ACTION_SAVE_CURRENT_CHAT_ID_VERSION = "action:保存当前chat-id";
    export type ACTION_SAVE_CURRENT_CHAT_ID_VERSION = (
        chatId: ChatStore.STATE_CHAT_CURRENT_CHAT_ID
    ) => Promise<void>;

    export const ACTION_CLEAR_CURRENT_CHAT_DATA = "action:清空当前会话数据";
    export type ACTION_CLEAR_CURRENT_CHAT_DATA = () => Promise<void>;

    export const ACTION_REGISTER_EVENT = "给当前会话注册事件通知";
    export type ACTION_REGISTER_EVENT = () => Promise<void>;

    export const ACTION_GET_CHAT_MEMBERS = "获取会话成员列表";
    export type ACTION_GET_CHAT_MEMBERS = () => Promise<void>;

    export const ACTION_SEND_MESSAGE = "发送消息";
    export type ACTION_SEND_MESSAGE = (params: {
        msgType: dto.MessageType;
        msg: string;
        ts?: number;
    }) => Promise<void>;
    export const ACTION_TERINATE_CHAT = "结束会话";
    export type ACTION_TERINATE_CHAT = () => Promise<void>;
    export const ACTION_CHAT_ADD_MEMBERS = "添加成员";
    export type ACTION_CHAT_ADD_MEMBERS = (uids: (string | number)[]) => Promise<void>;
    export const ACTION_CHAT_REMOVE_MEMBER = "移除成员";
    export type ACTION_CHAT_REMOVE_MEMBER = (uids: (string | number)[]) => Promise<void>;
    export const ACTION_CHAT_ADD_CS = "添加客服";
    export type ACTION_CHAT_ADD_CS = (uids: string[]) => Promise<void>;

    export const ACTION_CHAT_START_RECEPTION = "开始接待";
    export type ACTION_CHAT_START_RECEPTION = () => Promise<void>;

    export const ACTION_CHAT_FINISH_RECEPTION = "结束接待";
    export type ACTION_CHAT_FINISH_RECEPTION = () => Promise<void>;

    export const ACTION_CHAT_REMOVE_CS = "移除客服";
    export type ACTION_CHAT_REMOVE_CS = (uids: string[]) => Promise<void>;

    export const ACTION_CHAT_USER_EXIT = "普通用户退出";
    export type ACTION_CHAT_USER_EXIT = () => Promise<void>;

    export const ACTION_CHAT_CS_EXIT = "客服退出";
    export type ACTION_CHAT_CS_EXIT = () => Promise<void>;

    export const ACTION_SET_HANDLED = "设置敏感词已处理";
    export type ACTION_SET_HANDLED = (p: {
        id: number;
        value: dto.MessageHandled;
    }) => void;

    export const ACTION_SET_CHAT_ERROR = "标记会话关键性错误";
    export type ACTION_SET_CHAT_ERROR = (chat: number) => void;

    export interface ChatUpdateParameter {
        chat: number;
        type?: dto.MessageType;
        msg?: string;
        ts?: number;
        eid?: string;
        unread?: number;
    }

    export const ACTION_UPDATE_CHAT = "更新会话信息";
    export type ACTION_UPDATE_CHAT = (p: ChatUpdateParameter) => void;
}

export interface ChatStoreState {
    [ChatStore.STATE_CHAT_MSG_HISTORY]: ChatStore.STATE_CHAT_MSG_HISTORY;
    [ChatStore.STATE_CHAT_SENDING_MESSAGES]: ChatStore.STATE_CHAT_SENDING_MESSAGES;
    [ChatStore.STATE_CHAT_CURRENT_CHAT_ID]: ChatStore.STATE_CHAT_CURRENT_CHAT_ID;
    [ChatStore.STATE_CHAT_CURRENT_CHAT_UNIPLAT_ID]: ChatStore.STATE_CHAT_CURRENT_CHAT_UNIPLAT_ID;
    [ChatStore.STATE_CHAT_CURRENT_CHAT_VERSION]: ChatStore.STATE_CHAT_CURRENT_CHAT_VERSION;
    [ChatStore.STATE_CHAT_MY_UID]: ChatStore.STATE_CHAT_MY_UID;
    [ChatStore.STATE_CHAT_MY_ID]: ChatStore.STATE_CHAT_MY_ID;
    [ChatStore.STATE_CHAT_SOURCE]: ChatStore.STATE_CHAT_SOURCE;
    [ChatStore.STATE_CURRENT_CHAT_MEMBERS]: ChatStore.STATE_CURRENT_CHAT_MEMBERS;
    [ChatStore.STATE_CURRENT_CHAT_TITLE]: ChatStore.STATE_CURRENT_CHAT_TITLE;
    [ChatStore.STATE_FUNC_SCROLL_TO_BOTTOM]: ChatStore.STATE_FUNC_SCROLL_TO_BOTTOM;
    [ChatStore.STATE_FUNC_ON_NEW_MSG]: ChatStore.STATE_FUNC_ON_NEW_MSG;
    [ChatStore.STATE_CURRENT_CHAT_INPUTING]: ChatStore.STATE_CURRENT_CHAT_INPUTING;
    [ChatStore.STATE_CURRENT_CHAT_INITING]: ChatStore.STATE_CURRENT_CHAT_INITING;
    [ChatStore.STATE_MY_CHAT_ROOM_LIST]: ChatStore.STATE_MY_CHAT_ROOM_LIST;
    [ChatStore.STATE_CHAT_DIALOG_VISIBLE]: ChatStore.STATE_CHAT_DIALOG_VISIBLE;
    [ChatStore.STATE_CHAT_DIALOG_IS_SINGLE]: ChatStore.STATE_CHAT_DIALOG_IS_SINGLE;
    [ChatStore.STATE_SINGLE_CHAT]: ChatStore.STATE_SINGLE_CHAT;
    [ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER]: ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER;
    [ChatStore.STATE_CHAT_CURRENT_USER_UID]: ChatStore.STATE_CHAT_CURRENT_USER_UID;
    [ChatStore.STATE_CHAT_CURRENT_USER_TYPE]: ChatStore.STATE_CHAT_CURRENT_USER_TYPE;
    [ChatStore.STATE_CHAT_SEND_FAIL_MESSAGE]: ChatStore.STATE_CHAT_SEND_FAIL_MESSAGE;
    [ChatStore.STATE_CHAT_USERNAME]: ChatStore.STATE_CHAT_USERNAME;
    [ChatStore.STATE_CURRENT_UNREAD_MESSAGE_COUNT]: ChatStore.STATE_CURRENT_UNREAD_MESSAGE_COUNT;
}

export const chatStore = namespace(ChatStore.ns);