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.LinearLayout;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.base.BaseViewHolder;
import kr.co.hanwha.hifive.data.model.TaskListData;
import kr.co.hanwha.hifive.enums.TaskStatusType;
import kr.co.hanwha.hifive.screen.main.home.MainHomeContract;
import kr.co.hanwha.hifive.util.GlideUtil;
import kr.co.hanwha.hifive.util.HFCommonUtil;
import kr.co.hanwha.hifive.util.HFTaskUtil;

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

public class MainHomeRequestListViewHolder extends BaseViewHolder {
    private TextView mTextViewTitle;
    private ImageView mImageViewReceiverProfile;
    private TextView mTextViewReceiverName;
    private ImageView mImageViewIcon;
    private TextView mTextViewDueDate;
    private LinearLayout mLayoutActionEvaluation;
    private LinearLayout mLayoutActionRejected;


    private MainHomeContract.OnMainHomeListListener mOnMainHomeListListener;

    public MainHomeRequestListViewHolder(@NonNull ViewGroup viewGroup, MainHomeContract.OnMainHomeListListener onMainHomeListListener) {
        super(viewGroup, R.layout.item_main_home_request_list);

        mOnMainHomeListListener = onMainHomeListListener;

        mTextViewTitle = (TextView) itemView.findViewById(R.id.title_textView);
        mImageViewReceiverProfile = (ImageView) itemView.findViewById(R.id.receiverProfile_imageView);
        mTextViewReceiverName = (TextView) itemView.findViewById(R.id.receiverName_textView);
        mImageViewIcon = (ImageView) itemView.findViewById(R.id.icon_imageView);
        mTextViewDueDate = (TextView) itemView.findViewById(R.id.dueDate_textView);
        mLayoutActionEvaluation = (LinearLayout) itemView.findViewById(R.id.actionEvaluation_linearLayout);
        mLayoutActionRejected = (LinearLayout) itemView.findViewById(R.id.actionRejected_linearLayout);

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

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

        final TaskListData data = (TaskListData) object;

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

            mTextViewReceiverName.setText(String.format(getContext().getString(R.string.main_home_receiver), data.getMember_name()));

            mImageViewReceiverProfile.setVisibility(View.VISIBLE);
            mTextViewReceiverName.setVisibility(View.VISIBLE);
            mTextViewTitle.setText(data.getTask_name());

            mTextViewTitle.setBackground(null);

            mImageViewIcon.setImageDrawable(HFTaskUtil.getDueDateIconDrawable(getContext(), data.getDue_to(), data.getServer_time()));

            mTextViewDueDate.setTextColor(HFTaskUtil.getDueDateFontColor(getContext(), data.getDue_to(), data.getServer_time()));

            mTextViewDueDate.setText(HFTaskUtil.getDueDateText(getContext(), data.getDue_to(), data.getServer_time()));

            if (data.getStatus() == TaskStatusType.EVALUATED.getCode()) {
                mLayoutActionEvaluation.setVisibility(View.VISIBLE);
                mLayoutActionRejected.setVisibility(View.GONE);
            } else if (data.getStatus() == TaskStatusType.REJECTED_REQUEST.getCode()) {
                mLayoutActionEvaluation.setVisibility(View.GONE);
                mLayoutActionRejected.setVisibility(View.VISIBLE);
            } else {
                mLayoutActionEvaluation.setVisibility(View.GONE);
                mLayoutActionRejected.setVisibility(View.GONE);
            }

            String status = "";
            if (data.getStatus() == TaskStatusType.ASSIGNED.getCode() ||
                    data.getStatus() == TaskStatusType.DOING_TODAY.getCode()) status = "수락";
            else if (data.getStatus() == TaskStatusType.REQUESTED.getCode()) status = "대기 중";
            else if (data.getStatus() == TaskStatusType.REJECTED_REQUEST.getCode()) status = "반려";
            else if (data.getStatus() == TaskStatusType.EVALUATED.getCode()) status = "평가";
            else if (data.getStatus() == TaskStatusType.REJECTED_EVALUATION.getCode())
                status = "평가 반려";
            else if (data.getStatus() == TaskStatusType.CLOSED.getCode()) status = "완료";

            mTextViewDueDate.setText(status);
        }

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnMainHomeListListener != null)
                    mOnMainHomeListListener.onItemClicked(data);
            }
        });
    }
}
