package kr.co.hanwha.hifive.adapter.viewholder;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.contract.TaskDetailListContract;
import kr.co.hanwha.hifive.base.BaseViewHolder;
import kr.co.hanwha.hifive.data.model.MessageListData;
import kr.co.hanwha.hifive.enums.MessageType;
import kr.co.hanwha.hifive.util.GlideUtil;
import kr.co.hanwha.hifive.util.HFCommonUtil;
import kr.co.hanwha.hifive.util.HFDateUtil;
import kr.co.hanwha.hifive.util.HFSpanUtil;

/**
 * Created by sanghoonkwon on 2018. 1. 22..
 */

public class CommentListViewHolder extends BaseViewHolder {
    private ImageView mImageViewDot;
    private ImageView mImageViewAuthor;
    private TextView mTextViewAuthor;
    private TextView mTextViewMessage;
    private TextView mTextViewDate;
    private TextView mTextViewStatus;
    private View mViewUnderline;
    private View mViewBottomMargin;

    private TaskDetailListContract.OnTaskDetailListener onTaskDetailListener;

    public CommentListViewHolder(@NonNull ViewGroup viewGroup,
                                 @NonNull TaskDetailListContract.OnTaskDetailListener onTaskDetailListener) {
        super(viewGroup, R.layout.item_comment_list);

        mImageViewDot = (ImageView) itemView.findViewById(R.id.dot_imageView);
        mImageViewAuthor = (ImageView) itemView.findViewById(R.id.author_imageView);
        mTextViewAuthor = (TextView) itemView.findViewById(R.id.author_textView);
        mTextViewMessage = (TextView) itemView.findViewById(R.id.message_textView);
        mTextViewDate = (TextView) itemView.findViewById(R.id.date_textView);
        mTextViewStatus = (TextView) itemView.findViewById(R.id.status_textView);

        mViewUnderline = (View) itemView.findViewById(R.id.underline_view);
        mViewBottomMargin = (View) itemView.findViewById(R.id.bottomMargin_view);

        GlideUtil.getInstance().loadImageCircle(getContext(), R.mipmap.ic_avatar, mImageViewAuthor);

        this.onTaskDetailListener = onTaskDetailListener;
    }

    public void setLastItem(boolean isLast) {
        if (isLast) {
            mViewBottomMargin.setVisibility(View.VISIBLE);
            mViewUnderline.setVisibility(View.GONE);
        } else {
            mViewBottomMargin.setVisibility(View.GONE);
            mViewUnderline.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onBind(Object object, final int index) {
        super.onBind(object, index);

        final MessageListData data = (MessageListData) object;

        if (data != null) {
            if (TextUtils.isEmpty(data.getProfile_url()))
                GlideUtil.getInstance().loadImageCircle(getContext(), R.mipmap.ic_avatar, mImageViewAuthor);
            else
                GlideUtil.getInstance().loadImageCircle(getContext(), data.getProfile_url(), mImageViewAuthor);

            mImageViewDot.setVisibility(HFDateUtil.checkIsToday(data.getCreated_at(), HFDateUtil.FORMAT_COMMON_SERVER) ? View.VISIBLE : View.GONE);

            mTextViewAuthor.setText(data.getMember_name());

            mTextViewDate.setText(HFDateUtil.getDateFormatCommon(getContext(), data.getCreated_at()));

            HFSpanUtil.setCommentText(getContext(), mTextViewMessage, data.getContents(), onTaskDetailListener);

            if (data.getType() == MessageType.COMMENT.getCode()) {
                mTextViewStatus.setText("");
                mTextViewStatus.setVisibility(View.GONE);
            } else if (data.getType() == MessageType.REJECT_REQUEST.getCode()) {
                int textColor = HFCommonUtil.getColor(getContext(), R.color.gray_31);
                mTextViewStatus.setText(getContext().getString(R.string.task_detail_status_reject));
                mTextViewStatus.setTextColor(textColor);
                mTextViewStatus.setBackground(getContext().getDrawable(R.drawable.rounded_rectangle_status_red2));
                mTextViewStatus.setVisibility(View.VISIBLE);
            } else if (data.getType() == MessageType.TRANSFER.getCode()) {
                int textColor = HFCommonUtil.getColor(getContext(), R.color.gray_31);
                mTextViewStatus.setText(getContext().getString(R.string.task_detail_status_transfer));
                mTextViewStatus.setTextColor(textColor);
                mTextViewStatus.setBackground(getContext().getDrawable(R.drawable.rounded_rectangle_status_green));
                mTextViewStatus.setVisibility(View.VISIBLE);
            } else if (data.getType() == MessageType.REJECT_EVALUATION.getCode()) {
                int textColor = HFCommonUtil.getColor(getContext(), R.color.gray_31);
                mTextViewStatus.setText(getContext().getString(R.string.task_detail_status_eval_reject));
                mTextViewStatus.setTextColor(textColor);
                mTextViewStatus.setBackground(getContext().getDrawable(R.drawable.rounded_rectangle_status_red2));
                mTextViewStatus.setVisibility(View.VISIBLE);
            } else {
                mTextViewStatus.setText("");
                mTextViewStatus.setVisibility(View.GONE);
            }
        }

    }
}