package com.p3.bewoplaner.page.chat;

import com.p3.bewoplaner.R;
import com.p3.bewoplaner.model.Message;
import com.p3.bewoplaner.utils.DateUtils;
import com.p3.bewoplaner.utils.ui.ViewUtils;

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;

/**
 * Created by Rares Teodorescu on 21/02/2017.
 */
class SingleChatAdapter extends BaseChatAdapter {

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

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(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.setProgressBarColorToOrange(progressBar);
        return new ProgressViewHolder(progressBar);
    }

    @SuppressWarnings("ConstantConditions")
    @Override
    public void onBindViewHolder(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.setProgressBarColorToOrange(rightChatHolder.statusProgressBar);
                    rightChatHolder.statusProgressBar.setVisibility(View.VISIBLE);
                    rightChatHolder.sendingFailedTextView.setVisibility(View.GONE);
                    rightChatHolder.statusImageView.setVisibility(View.GONE);
                }
                break;
        }
    }

}
