package de.beyondsoft.ownchat.page.chat;

import de.beyondsoft.ownchat.R;
import de.beyondsoft.ownchat.model.Message;
import de.beyondsoft.ownchat.utils.DateUtils;
import de.beyondsoft.ownchat.utils.ui.ViewUtils;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

class SingleChatAdapter extends BaseChatAdapter {

    SingleChatAdapter(long userId) {
        super(userId);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType == LEFT_CHAT_TYPE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_single_chat_left, parent, false);

            return new MessageViewHolder(view);
        } else if(viewType == RIGHT_CHAT_TYPE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_single_chat_right, parent, false);

            return new MessageViewHolder(view);
        } else if(viewType == DATE_TYPE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_date, parent, false);

            return new DateViewHolder(view);
        }

        ProgressBar progressBar = new ProgressBar(parent.getContext());

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        params.setMargins(0, (int) parent.getContext().getResources().getDimension(R.dimen.messages_progress_top_margin), 0, 0);
        progressBar.setLayoutParams(params);

        ViewUtils.setProgressBarColor(progressBar, R.color.colorWhite);

        return new ProgressViewHolder(progressBar);
    }

    @SuppressWarnings("ConstantConditions")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        switch(viewType) {
            case LOADING_TYPE:
                break;
            case DATE_TYPE:
                ((DateViewHolder) holder).dateTextView.setText(DateUtils.toDayOfYearString(mDateList.get(position)));

                break;
            case LEFT_CHAT_TYPE:
                MessageViewHolder leftChatHolder = (MessageViewHolder) holder;
                final Message leftMessage = mMessages.get(position - mPositionOffset.get(position));
                setSingleMessage(leftChatHolder, leftMessage, position, true);

                break;
            case RIGHT_CHAT_TYPE:
                MessageViewHolder rightChatHolder = (MessageViewHolder) holder;

                final Message rightMessage = mMessages.get(position - mPositionOffset.get(position));

                setSingleMessage(rightChatHolder, rightMessage, position, false);

                if(rightMessage.status == Message.Status.DELIVERED) {
                    rightChatHolder.statusProgressBar.setVisibility(View.GONE);
                    rightChatHolder.statusImageView.setVisibility(View.VISIBLE);
                    rightChatHolder.statusImageView.setImageResource(R.drawable.delivered);
                    rightChatHolder.sendingFailedTextView.setVisibility(View.GONE);
                } else if(rightMessage.status == Message.Status.FAILED) {
                    rightChatHolder.statusProgressBar.setVisibility(View.GONE);
                    rightChatHolder.statusImageView.setVisibility(View.VISIBLE);
                    rightChatHolder.statusImageView.setImageResource(R.drawable.failed);
                    rightChatHolder.sendingFailedTextView.setVisibility(View.VISIBLE);
                } else {
                    ViewUtils.setProgressBarColor(rightChatHolder.statusProgressBar, R.color.colorWhite);
                    rightChatHolder.statusProgressBar.setVisibility(View.VISIBLE);
                    rightChatHolder.sendingFailedTextView.setVisibility(View.GONE);
                    rightChatHolder.statusImageView.setVisibility(View.GONE);
                }

                break;
        }
    }
}
