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 GroupChatAdapter extends BaseChatAdapter {

    GroupChatAdapter(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_group_chat_left, parent, false);

            return new MessageViewHolder(view);
        } else if(viewType == RIGHT_CHAT_TYPE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group_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:
                final Message leftMessage = mMessages.get(position - mPositionOffset.get(position));
                MessageViewHolder leftMessageViewHolder = (MessageViewHolder) holder;
                setGroupMessage(leftMessageViewHolder, leftMessage, position, true);

                break;
            case RIGHT_CHAT_TYPE:
                final Message rightMessage = mMessages.get(position - mPositionOffset.get(position));
                MessageViewHolder rightMessageViewHolder = (MessageViewHolder) holder;
                setGroupMessage(rightMessageViewHolder, rightMessage, position, false);

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

                break;
        }
    }
}
