/*
 * F7cloud Talk - Android Client
 *
 * SPDX-FileCopyrightText: 2022-2023 Marcel Hibbe <dev@mhibbe.de>
 * SPDX-FileCopyrightText: 2021-2022 Tim Krüger <t@timkrueger.me>
 * SPDX-FileCopyrightText: 2021 Andy Scherzinger <info@andy-scherzinger.de>
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package ru.f7cloud.talk.adapters.messages;

import android.text.Spanned;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.google.android.material.card.MaterialCardView;
import ru.f7cloud.talk.R;
import ru.f7cloud.talk.chat.data.model.ChatMessage;
import ru.f7cloud.talk.databinding.ItemCustomIncomingPreviewMessageBinding;
import ru.f7cloud.talk.databinding.ItemThreadTitleBinding;
import ru.f7cloud.talk.databinding.ReactionsInsideMessageBinding;
import ru.f7cloud.talk.utils.TextMatchers;

import java.util.HashMap;
import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.emoji2.widget.EmojiTextView;

public class IncomingPreviewMessageViewHolder extends PreviewMessageViewHolder {
    private final ItemCustomIncomingPreviewMessageBinding binding;

    public IncomingPreviewMessageViewHolder(View itemView, Object payload) {
        super(itemView, payload);
        binding = ItemCustomIncomingPreviewMessageBinding.bind(itemView);
    }

    @Override
    public void onBind(@NonNull ChatMessage message) {
        super.onBind(message);
        if(!message.isVoiceMessage()
            && !Objects.equals(message.getMessage(), "{file}")
        ) {
            Spanned processedMessageText = null;
            binding.bubble.setBackgroundResource(R.drawable.shape_grouped_incoming_message);
            if (viewThemeUtils != null ) {
                processedMessageText = messageUtils.enrichChatMessageText(
                    binding.messageCaption.getContext(),
                    message,
                    true,
                    viewThemeUtils);
                viewThemeUtils.talk.themeIncomingMessageBubble(binding.bubble, true, false,
                                                               false);
            }

            if (processedMessageText != null) {
                processedMessageText = messageUtils.processMessageParameters(
                    binding.messageCaption.getContext(),
                    viewThemeUtils,
                    processedMessageText,
                    message,
                    binding.bubble);
            }
            binding.bubble.setOnClickListener(null);

            float textSize = 0;
            if (context != null) {
                textSize = context.getResources().getDimension(R.dimen.chat_text_size);
            }
            HashMap<String, HashMap<String, String>> messageParameters = message.getMessageParameters();
            if (
                (messageParameters == null || messageParameters.size() <= 0) &&
                    TextMatchers.isMessageWithSingleEmoticonOnly(message.getText())
            ) {
                textSize = (float) (textSize * IncomingTextMessageViewHolder.TEXT_SIZE_MULTIPLIER);
                itemView.setSelected(true);
            }
            binding.messageCaption.setVisibility(View.VISIBLE);
            binding.messageCaption.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            binding.messageCaption.setText(processedMessageText);
        } else {
            binding.bubble.setBackground(null);
            binding.messageCaption.setVisibility(View.GONE);
        }
        binding.messageAuthor.setText(message.getActorDisplayName());
        binding.messageText.setTextColor(ContextCompat.getColor(binding.messageText.getContext(),
                                                                R.color.no_emphasis_text));
        binding.messageTime.setTextColor(ContextCompat.getColor(binding.messageText.getContext(),
                                                                R.color.no_emphasis_text));

        binding.messageEditIndicator.setTextColor(ContextCompat.getColor(binding.messageText.getContext(),
                                                                         R.color.no_emphasis_text));

        if(!message.isThread()) {
            binding.threadTitleWrapperContainer.setVisibility(View.GONE);
        } else {
            binding.threadTitleWrapperContainer.setVisibility(View.VISIBLE);
        }
    }

    @NonNull
    @Override
    public android.widget.TextView getMessageEditIndicator() {
        return binding.messageEditIndicator;
    }


    @NonNull
    @Override
    public EmojiTextView getMessageText() {
        return binding.messageText;
    }

    @NonNull
    @Override
    public EmojiTextView getMessageCaption() {
        return binding.messageCaption;
    }

    @Override
    public ProgressBar getProgressBar() {
        return binding.progressBar;
    }

    @NonNull
    @Override
    public View getPreviewContainer() {
        return binding.previewContainer;
    }

    @NonNull
    @Override
    public MaterialCardView getPreviewContactContainer() {
        return binding.contactContainer;
    }

    @NonNull
    @Override
    public ImageView getPreviewContactPhoto() {
        return binding.contactPhoto;
    }

    @NonNull
    @Override
    public EmojiTextView getPreviewContactName() {
        return binding.contactName;
    }

    @Override
    public ProgressBar getPreviewContactProgressBar() {
        return binding.contactProgressBar;
    }

    @Override
    public ReactionsInsideMessageBinding getReactionsBinding(){ return binding.reactions; }

    @Override
    public ItemThreadTitleBinding getThreadsBinding(){ return binding.threadTitleWrapper; }
}
