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.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.enums.MessageType;
import kr.co.hanwha.hifive.util.GlideUtil;
import kr.co.hanwha.hifive.util.HFDateUtil;
import kr.co.hanwha.hifive.util.HFSpanUtil;
import kr.co.hanwha.hifive.util.HFTaskUtil;

/**
 * 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 TextView mTextViewDelete;
    private View mViewDefaultMargin;
    private View mViewUnderline;
    private View mViewLastline;
    private View mViewBottomMargin;

    private TaskDetailListContract.OnTaskDetailListener mOnTaskDetailListener;

    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);
        mTextViewDelete = (TextView) itemView.findViewById(R.id.delete_textView);
        mViewDefaultMargin = (View) itemView.findViewById(R.id.defaultMargin_view);
        mViewUnderline = (View) itemView.findViewById(R.id.underline_view);
        mViewLastline = (View) itemView.findViewById(R.id.lastline_view);
        mViewBottomMargin = (View) itemView.findViewById(R.id.bottomMargin_view);


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

        this.mOnTaskDetailListener = onTaskDetailListener;
    }

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

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

        if (object != null && object instanceof MessageListData) {
            final MessageListData data = (MessageListData) object;
            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(), mOnTaskDetailListener);

            mTextViewStatus.setTextColor(HFTaskUtil.getStatusMessageFontColor(getContext(), data.getType()));
            mTextViewStatus.setBackground(HFTaskUtil.getStatusMessageBackground(getContext(), data.getType()));
            mTextViewStatus.setText(HFTaskUtil.getStatusMessageText(getContext(), data.getType()));
            mTextViewStatus.setVisibility(HFTaskUtil.getStatusMessageVisibility(getContext(), data.getType()) ? View.VISIBLE : View.GONE);

            if (data.getMember_id() == MemberDataRepository.getInstance().getMemberId() &&
                    data.getType() == MessageType.COMMENT.getCode()) {
                mTextViewDelete.setVisibility(View.VISIBLE);
                mViewDefaultMargin.setVisibility(View.GONE);
                mTextViewDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnTaskDetailListener != null)
                            mOnTaskDetailListener.onDeleteCommentClicked(data);
                    }
                });
            } else {
                mTextViewDelete.setVisibility(View.GONE);
                mViewDefaultMargin.setVisibility(View.VISIBLE);
            }
        }
    }
}