package com.bc_chat.im.messages;

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

import com.bc_chat.im.R;

import io.rong.imkit.RongContext;
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.UserInfo;
import io.rong.message.RecallNotificationMessage;

/**
 * author：Created by zhaohaoting on 2019/3/4
 * email：526309416@qq.com
 * desc：撤回消息
 */
@ProviderTag(
        messageContent = RecallNotificationMessage.class,
        showPortrait = false,
        showProgress = false,
        showWarning = false,
        centerInHorizontal = true,
        showSummaryWithName = false
)
public class RecallMessageItemProvider extends IContainerItemProvider.MessageProvider<RecallNotificationMessage> {
    public RecallMessageItemProvider() {
    }

    public void onItemClick(View view, int position, RecallNotificationMessage content, UIMessage message) {
    }

    public void bindView(View v, int position, RecallNotificationMessage content, UIMessage message) {
        ViewHolder viewHolder = (ViewHolder) v.getTag();
        if (content != null && message != null) {
            String information;
            String operatorId = content.getOperatorId(); //发送撤回消息通知的用户
            if (operatorId.equals(RongIM.getInstance().getCurrentUserId())) {
                information = RongContext.getInstance().getString(io.rong.imkit.R.string.rc_you_recalled_a_message);
            } else {
                UserInfo userInfo = content.getUserInfo(); //发送原消息的用户
                if (userInfo == null) {
                    userInfo = RongUserInfoManager.getInstance().getUserInfo(message.getSenderUserId());
                }
                if (!userInfo.getUserId().equals(content.getOperatorId())) {
                    if (userInfo.getUserId().equals(RongIM.getInstance().getCurrentUserId())) {
                        information = userInfo.getName() + RongContext.getInstance().getString(io.rong.imkit.R.string.rc_admin_recalled_you_a_message);
                    } else {
                        information = userInfo.getName() + RongContext.getInstance().getString(io.rong.imkit.R.string.rc_admin_recalled_a_message);
                    }
                } else {
                    UserInfo noticeUserInfo = RongUserInfoManager.getInstance().getUserInfo(content.getOperatorId());
                    if (noticeUserInfo != null && noticeUserInfo.getName() != null) {
                        information = noticeUserInfo.getName() + RongContext.getInstance().getString(io.rong.imkit.R.string.rc_recalled_a_message);
                    } else {
                        information = content.getOperatorId() + RongContext.getInstance().getString(io.rong.imkit.R.string.rc_recalled_a_message);
                    }
                }
            }

            viewHolder.contentTextView.setText(information);
        }

    }

    public void onItemLongClick(View view, int position, RecallNotificationMessage content, UIMessage message) {
    }

    public Spannable getContentSummary(RecallNotificationMessage data) {
        return null;
    }

    public Spannable getContentSummary(Context context, RecallNotificationMessage data) {
        if (data != null && !TextUtils.isEmpty(data.getOperatorId())) {
            String information;
            if (data.getOperatorId().equals(RongIM.getInstance().getCurrentUserId())) {
                information = context.getString(io.rong.imkit.R.string.rc_you_recalled_a_message);
            } else {
                UserInfo userInfo = RongUserInfoManager.getInstance().getUserInfo(data.getOperatorId());
                if (userInfo != null && userInfo.getName() != null) {
                    information = userInfo.getName() + context.getString(io.rong.imkit.R.string.rc_recalled_a_message);
                } else {
                    information = data.getOperatorId() + context.getString(io.rong.imkit.R.string.rc_recalled_a_message);
                }
            }

            return new SpannableString(information);
        } else {
            return null;
        }
    }

    public View newView(Context context, ViewGroup group) {
        View view = LayoutInflater.from(context).inflate(io.rong.imkit.R.layout.rc_item_information_notification_message, (ViewGroup) null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.contentTextView = (TextView) view.findViewById(R.id.rc_msg);
        viewHolder.contentTextView.setMovementMethod(LinkMovementMethod.getInstance());
        view.setTag(viewHolder);
        return view;
    }

    private static class ViewHolder {
        TextView contentTextView;

        private ViewHolder() {
        }
    }
}
