package io.rong.imkit.conversation.messgelist.provider;

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.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.common.WebConstants;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.utils.GlideUtils;
import com.cooleshow.base.utils.helper.WebStartHelper;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.R;
import io.rong.imkit.adapter.ItemMarkAdapter;
import io.rong.imkit.model.UiMessage;
import io.rong.imkit.widget.RCChatShareMusicMessage;
import io.rong.imkit.widget.adapter.IViewProviderListener;
import io.rong.imkit.widget.adapter.ViewHolder;
import io.rong.imlib.model.MessageContent;

/**
 * 创建日期：2022/6/14 11:13
 *
 * @author Ryan
 * 类说明：
 */
public class RCChatShareMusicProvider extends BaseMessageItemProvider<RCChatShareMusicMessage> {

    public RCChatShareMusicProvider() {
        mConfig.showContentBubble = false;
        mConfig.showProgress = false;
    }

    @Override
    protected ViewHolder onCreateMessageContentViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rc_chat_share_music, parent, false);
        return new ViewHolder(view.getContext(), view);
    }

    @Override
    protected void bindMessageContentViewHolder(final ViewHolder holder, ViewHolder parentHolder, RCChatShareMusicMessage shareMusicMessage, final UiMessage uiMessage, int position, List<UiMessage> list, IViewProviderListener<UiMessage> listener) {
        TextView tv_name = holder.getView(R.id.tv_name);
        TextView tv_author_name = holder.getView(R.id.tv_author_name);
        ImageView im_state = holder.getView(R.id.im_state);
        ImageView im_header = holder.getView(R.id.im_header);
        TextView tv_teacher_name = holder.getView(R.id.tv_teacher_name);
        RecyclerView rv_mark = holder.getView(R.id.rv_mark);

        switch (shareMusicMessage.getChargeType()) {
            case "FREE":
                im_state.setBackgroundResource(R.drawable.icon_score_free);
                break;
            case "VIP":
                im_state.setBackgroundResource(R.drawable.icon_score_vip);
                break;
            case "CHARGE":
                im_state.setBackgroundResource(R.drawable.icon_score_demand);
                break;
        }
        tv_name.setText(shareMusicMessage.getSongName());
        tv_author_name.setText(shareMusicMessage.getSongAuth());
        GlideUtils.INSTANCE.loadImage(holder.getContext(), shareMusicMessage.getTeacherAvatar(), im_header, R.drawable.icon_teacher_default_head);
        tv_teacher_name.setText(shareMusicMessage.getTeacherName());
        if (!TextUtils.isEmpty(shareMusicMessage.getMusicTagNames())) {
            rv_mark.setVisibility(View.VISIBLE);
            List<String> markList = new ArrayList<>();
            if (shareMusicMessage.getMusicTagNames().contains(",")) {
                String[] split = shareMusicMessage.getMusicTagNames().split(",");
                for (String s : split) {
                    markList.add(s);
                }
            } else {
                markList.add(shareMusicMessage.getMusicTagNames());
            }
            LinearLayoutManager manager = new LinearLayoutManager(holder.getContext());
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rv_mark.setLayoutManager(manager);
            ItemMarkAdapter itemMarkAdapter = new ItemMarkAdapter(markList);
            rv_mark.setAdapter(itemMarkAdapter);
        } else {
            rv_mark.setVisibility(View.GONE);
        }
    }


    @Override
    protected boolean isMessageViewType(MessageContent messageContent) {
        return messageContent instanceof RCChatShareMusicMessage;
    }


    @Override
    public Spannable getSummarySpannable(Context context, RCChatShareMusicMessage shareMusicMessage) {
        return new SpannableString("[曲谱分享]");
    }

    @Override
    protected boolean onItemClick(ViewHolder holder, RCChatShareMusicMessage shareMusicMessage, UiMessage uiMessage, int position, List<UiMessage> list, IViewProviderListener<UiMessage> listener) {
        if (shareMusicMessage != null && !TextUtils.isEmpty(shareMusicMessage.getSongId())) {
            WebStartHelper.startAccompany(shareMusicMessage.getSongId());
        }
        return true;
    }
}