package com.bc_chat.im.messages.vibration;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bc_chat.im.R;
import com.bc_chat.im.messages.systen.RCSysMessage;

import io.rong.imkit.RongIM;
import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.userInfoCache.RongUserInfoManager;
import io.rong.imkit.widget.provider.IContainerItemProvider;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Group;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.UserInfo;

/**
 * author：Created by zhaohaoting on 2019/03/03
 * email：526309416@qq.com
 * desc：
 */
@ProviderTag(
        messageContent = RCVibrationMessage.class,
        centerInHorizontal = false,
        showPortrait = true
)
public class RCVibrationMessageProvider extends IContainerItemProvider.MessageProvider<RCVibrationMessage> {
    @Override
    public void bindView(View view, int i, RCVibrationMessage rcVibrationMessage, UIMessage uiMessage) {
        ViewHolder holder = (ViewHolder) view.getTag();
        if (uiMessage.getMessageDirection() == Message.MessageDirection.SEND) {
            holder.contentView.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_right);
        } else {
            holder.contentView.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_left);
        }
        String senderUserId = uiMessage.getSenderUserId();
        UserInfo sendUserInfo = uiMessage.getUserInfo();
        String currentUserId = RongIM.getInstance().getCurrentUserId();
        if (sendUserInfo == null) {
            sendUserInfo = RongUserInfoManager.getInstance().getUserInfo(senderUserId);
        }
        String sendUserName = sendUserInfo == null ? senderUserId : sendUserInfo.getName();
        String information = null;
        if (uiMessage.getConversationType() == Conversation.ConversationType.PRIVATE) {
            UserInfo userInfo = RongUserInfoManager.getInstance().getUserInfo(uiMessage.getTargetId());
            String userInfoName = userInfo == null ? uiMessage.getTargetId() : userInfo.getName();
            if (senderUserId.equals(currentUserId)) {
                information = "你正在震" + userInfoName + "上线";
            } else {
                information = sendUserName + "正在震你上线";
            }
        } else if (uiMessage.getConversationType() == Conversation.ConversationType.GROUP) {
            Group group = RongUserInfoManager.getInstance().getGroupInfo(uiMessage.getTargetId());
            String groupName = group == null ? uiMessage.getTargetId() : group.getName();
            if (senderUserId.equals(currentUserId)) {
                information = "我发起了一个群震";
            } else {
                information = sendUserName + "发起了一个群震";
            }
        }
        String msgContent = getMsgContent(rcVibrationMessage, uiMessage.getMessage());
        if (!TextUtils.isEmpty(msgContent)) {
            information = msgContent;
        }
        holder.contentView.setText(information);
    }

    @Override
    public Spannable getContentSummary(RCVibrationMessage rcVibrationMessage) {
        if (rcVibrationMessage == null)
            return null;
        return new SpannableString(TextUtils.isEmpty(rcVibrationMessage.getContent()) ? "[震]" : rcVibrationMessage.getContent());
    }

    public Spannable getContentSummary1(RCSysMessage data) {
        return new SpannableString(data.getContent());
    }

    @Override
    public void onItemClick(View view, int i, RCVibrationMessage rcVibrationMessage, UIMessage uiMessage) {
    }

    @Override
    public void onItemLongClick(View view, int position, RCVibrationMessage rcVibrationMessage, UIMessage message) {
        super.onItemLongClick(view, position, rcVibrationMessage, message);
    }

    @Override
    public View newView(Context context, ViewGroup viewGroup) {
        View view = LayoutInflater.from(context).inflate(R.layout.rc_item_vibration_message, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.contentView = view.findViewById(R.id.rc_vibration_content);
        view.setTag(viewHolder);
        return view;
    }

    private class ViewHolder {
        TextView contentView;
    }

    private String getMsgContent(RCVibrationMessage rcVibrationMessage, Message uiMessage) {
        return rcVibrationMessage.getContent();
    }
}
