package org.tio.sitexxx.im.server.handler.wx;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.jfinal.kit.Ret;
import org.tio.sitexxx.service.Command;
import org.tio.sitexxx.im.common.CommandHandler;
import org.tio.sitexxx.im.common.ImPacket;
import org.tio.sitexxx.im.common.bs.wx.WxImageChatReq;
import org.tio.sitexxx.im.common.bs.wx.WxImageChatResp;
import org.tio.sitexxx.im.server.Ims;
import org.tio.sitexxx.im.server.handler.AbsImServerHandler;
import org.tio.sitexxx.service.model.main.Img;
import org.tio.sitexxx.service.model.main.User;
import org.tio.sitexxx.service.model.main.WxChatItems;
import org.tio.sitexxx.service.model.main.WxChatUserItem;
import org.tio.sitexxx.service.service.chat.ChatIndexService;
import org.tio.sitexxx.service.service.chat.ChatService;
import org.tio.sitexxx.service.service.chat.GroupService;
import org.tio.sitexxx.service.utils.RetUtils;
import org.tio.sitexxx.service.vo.Const;
import org.tio.sitexxx.service.vo.RetCode;
import org.tio.sitexxx.service.vo.SimpleUser;
import org.tio.sitexxx.service.ThreadUtil;
import org.tio.utils.json.Json;
import org.tio.utils.resp.Resp;

import java.util.Objects;

/**
 * <pre>
 *    @author : vagrancy
 *    e-mail : 18050829067@163.com
 *    time   : 2025-06-20 11:43
 *    desc   : 图片会话
 * </pre>
 */
@CommandHandler(Command.WxImageChatReq)
public class WxImageChatReqHandler extends AbsImServerHandler {
    private static Logger log = LoggerFactory.getLogger(WxImageChatReqHandler.class);

    @Override
    public void handler(ImPacket packet, ChannelContext channelContext, boolean isWebsocket, User curr, SimpleUser currSimpleUser) throws Exception {
        WxImageChatReq request = Json.toBean(packet.getBodyStr(), WxImageChatReq.class);
        Long chatLinkId = request.getChatlinkid();
        if (chatLinkId <= 0) {
            Ret check = GroupService.checkGroupMsg(-chatLinkId, curr.getId());
            if (check.isFail()) {
                sendResult(channelContext, RetUtils.getRetMsg(check));
                return;
            }
            chatLinkId = RetUtils.getOkTData(check);
        }
        WxChatItems chatItems = ChatService.me.getBaseChatItems(chatLinkId);
        if (chatItems == null) {
            sendResult(channelContext, RetCode.CommonCode.PARAM_ERROR.value);
            return;
        }
        if (Objects.equals(chatItems.getChatmode(), Const.ChatMode.P2P)) {
            WxChatUserItem toUserItem = ChatIndexService.fdUserIndex(chatItems.getBizid().intValue(), chatItems.getUid());
            if (!ChatService.existTwoFriend(toUserItem)) {
                sendResult(channelContext, "对方不是你的好友");
                return;
            }
            if (toUserItem.getUid().equals(curr.getId())) {
                sendResult(channelContext, "不能给自己发送消息");
                return;
            }
        }
        Resp resp = ChatService.me.processObsImg(curr.getId(),"", request.getPath(), request.getFilename(), request.getWidth(), request.getHeight());
        if (!resp.isOk()) {
            sendResult(channelContext, resp.getMsg());
            return;
        }
        Img img = (Img) resp.getData();
        String text = Json.toJson(img);
        ThreadUtil.getBsExecutor().execute(()->{
            try {
                Ret ret;
                if (Objects.equals(chatItems.getChatmode(), Const.ChatMode.P2P)) {
                    ret = WxChatApi.sendFdMsgEach(text, Const.ContentType.IMG, chatItems.getUid(), chatItems.getBizid().intValue(), Const.YesOrNo.NO);
                } else {
                    ret = WxChatApi.sendGroupMsgEach(text, Const.ContentType.IMG, chatItems.getUid(), chatItems.getBizid(), null, null, null);
                }
                if (ret.isFail()) {
                    log.error(RetUtils.getRetMsg(ret));
                }
            } catch (Exception e){
                log.error(e.getMessage());
            }
        });
        sendResult(channelContext, "发送成功");
    }

    private void sendResult(ChannelContext context, String error){
        WxImageChatResp resp = new WxImageChatResp();
        resp.setMsg(error);
        ImPacket imPacket = new ImPacket(Command.WxImageChatNtf, resp);
        Ims.send(context, imPacket);
    }
}
