package com.daya.live_teaching.im.provider;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.widget.TextView;

import com.daya.live_teaching.R;
import com.daya.live_teaching.im.message.MemberChangedMessage;
import com.daya.live_teaching.model.ClassMemberChangedAction;

import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imkit.conversation.messgelist.provider.MessageItemProviderConfig;
import io.rong.imkit.model.UiMessage;
import io.rong.imkit.widget.adapter.IViewProviderListener;
import io.rong.imkit.widget.adapter.ViewHolder;
import io.rong.imlib.model.MessageContent;

/**
 * 用户进出课堂消息提示模版
 */
public class ClassMemberChangedNotificationProvider extends BaseNotificationProvider<MemberChangedMessage> {

    protected MessageItemProviderConfig mConfig = new MessageItemProviderConfig();

    public ClassMemberChangedNotificationProvider() {
        mConfig.showPortrait = false;
        mConfig.showProgress = false;
        mConfig.showSummaryWithName = false;
        mConfig.centerInHorizontal = true;
    }
    @Override
    protected void bindMessageContentViewHolder(ViewHolder holder, ViewHolder parentHolder, MemberChangedMessage memberChangedMessage, UiMessage uiMessage, int position, List<UiMessage> list, IViewProviderListener<UiMessage> listener) {
        TextView tvMessage = holder.getView(R.id.tv_message);
        tvMessage.setText(getNotifyContent(holder.getContext(), memberChangedMessage));
    }



    @Override
    protected boolean isMessageViewType(MessageContent messageContent) {
        return messageContent instanceof MemberChangedMessage && !messageContent.isDestruct();
    }
    public String getNotifyContent(Context context, MemberChangedMessage memberChangedMessage) {
        String content = "";

        String userName = memberChangedMessage.getUserName();
        String currentUserId = RongIM.getInstance().getCurrentUserId();
        if (userName != null && userName.equals(currentUserId)) {
            userName = context.getString(R.string.you);
        }
        ClassMemberChangedAction action = memberChangedMessage.getAction();
        switch (action) {
            case JOIN:
                content = context.getString(R.string.class_conversation_notify_member_join_format, userName);
                break;
            case LEAVE:
                content = context.getString(R.string.class_conversation_notify_member_leave_format, userName);
                break;
            case KICK:
                content = context.getString(R.string.class_conversation_notify_member_kick_format, userName);
                break;
        }

        return content;
    }

    @Override
    public Spannable getSummarySpannable(Context context, MemberChangedMessage memberChangedMessage) {
        return new SpannableString(getNotifyContent(context, memberChangedMessage));
    }

}
