package com.tencent.qcloud.tuikit.tuichat.ui.view.message.viewholder;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tencent.qcloud.tuikit.tuichat.R;
import com.tencent.qcloud.tuikit.tuichat.TUIChatService;
import com.tencent.qcloud.tuikit.tuichat.bean.message.TUIMessageBean;
import com.tencent.qcloud.tuikit.tuichat.bean.message.TextMessageBean;
import com.tencent.qcloud.tuikit.tuichat.component.face.FaceManager;

public class TextMessageHolder extends MessageContentHolder {

    private TextView msgBodyText;
    private ImageView msgBodyTextLeftYuYinIV;
    private ImageView msgBodyTextRightYuYinIV;

    public TextMessageHolder(View itemView) {
        super(itemView);
        msgBodyText = itemView.findViewById(R.id.msg_body_tv);
        msgBodyTextLeftYuYinIV = itemView.findViewById(R.id.yuyinquxiao_left_iv);
        msgBodyTextRightYuYinIV = itemView.findViewById(R.id.yuyinquxiao_right_iv);
    }

    @Override
    public int getVariableLayout() {
        return R.layout.message_adapter_content_text;
    }

    @Override
    public void layoutVariableViews(TUIMessageBean msg, int position) {
        if (!(msg instanceof TextMessageBean)) {
            return;
        }
        TextMessageBean textMessageBean = (TextMessageBean) msg;

        msgBodyText.setVisibility(View.VISIBLE);
        if (textMessageBean.getText() != null) {
            FaceManager.handlerEmojiText(msgBodyText, textMessageBean.getText(), false);
        } else if (!TextUtils.isEmpty(textMessageBean.getExtra())) {
            FaceManager.handlerEmojiText(msgBodyText, textMessageBean.getExtra(), false);
        } else {
            FaceManager.handlerEmojiText(msgBodyText, TUIChatService.getAppContext().getString(R.string.no_support_msg), false);
        }
        if (properties.getChatContextFontSize() != 0) {
            msgBodyText.setTextSize(properties.getChatContextFontSize());
        }
        msgBodyTextLeftYuYinIV.setVisibility(View.GONE);
        msgBodyTextRightYuYinIV.setVisibility(View.GONE);
        if (textMessageBean.isSelf()) {
            if (properties.getRightChatContentFontColor() != 0) {
                msgBodyText.setTextColor(properties.getRightChatContentFontColor());
            }
            if (textMessageBean.getMsgType() == 2 && !(msg.getV2TIMMessage() != null && msg.getV2TIMMessage().getCustomElem() != null && msg.getV2TIMMessage().getCustomElem().getDescription().equalsIgnoreCase(itemView.getContext().getString(R.string.chat_gift_message)))) {
                msgBodyTextRightYuYinIV.setVisibility(View.VISIBLE);
            }
        } else {
            if (properties.getLeftChatContentFontColor() != 0) {
                msgBodyText.setTextColor(properties.getLeftChatContentFontColor());
            }
            if (textMessageBean.getMsgType() == 2 && !(msg.getV2TIMMessage() != null && msg.getV2TIMMessage().getCustomElem() != null && msg.getV2TIMMessage().getCustomElem().getDescription().equalsIgnoreCase(itemView.getContext().getString(R.string.chat_gift_message)))) {
                msgBodyTextLeftYuYinIV.setVisibility(View.VISIBLE);
            }
        }
    }

}
