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.RoleSingleChangedMessage;
import com.daya.live_teaching.model.Role;
import com.daya.live_teaching.model.RoleChangedUser;

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 RoleChangedMessageItemProvider extends BaseNotificationProvider<RoleSingleChangedMessage> {

    protected MessageItemProviderConfig mConfig = new MessageItemProviderConfig();

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


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

        RoleChangedUser user = roleChangedMessage.getUser();
        Role role;
        // 获取身份，当有多人时身份为统一变动
        role = user.getRole();
        String currentUserId = RongIM.getInstance().getCurrentUserId();

        // 姓名集合，当有多人改变身份时，拼接姓名
        StringBuilder userNameUnionBuilder = new StringBuilder();
        String nameSeparate = context.getString(R.string.mark_name_separate);

        String userName = user.getUserName();

        // 当自己时改变显示称呼
        if (currentUserId.equals(user.getUserId())) {
            userName = context.getString(R.string.you);
        }
        userNameUnionBuilder.append(userName).append(nameSeparate);

        String userNameUnion = userNameUnionBuilder.substring(0, userNameUnionBuilder.length() - 1);
        // 根据身份不同显示不同的信息
        switch (role) {
            case LECTURER:
                content = context.getString(R.string.class_conversation_notify_role_to_lecturer, userNameUnion);
                break;
            case STUDENT:
                content = context.getString(R.string.class_conversation_notify_role_to_student, userNameUnion);
                break;
            case LISTENER:
                content = context.getString(R.string.class_conversation_notify_role_to_listener, userNameUnion);
                break;
        }

        return content;
    }

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


}
