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

import android.support.annotation.NonNull;
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.adapter.contract.SearchTaskListContract;
import kr.co.hanwha.hifive.base.BaseViewHolder;
import kr.co.hanwha.hifive.data.model.TaskListData;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.enums.TaskStatusType;
import kr.co.hanwha.hifive.util.HFDateUtil;
import kr.co.hanwha.hifive.util.HFTaskUtil;
import kr.co.hanwha.hifive.view.HFEvaluationStarListView;

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

public class SearchTaskListViewHolder extends BaseViewHolder {
    private LinearLayout mLayoutContainer;

    private TextView mTextViewDueDate;
    private HFEvaluationStarListView mEvaluationStarListView;

    private TextView mTextViewMyTask;
    private TextView mTextViewAuthorName;
    private ImageView mImageViewArrow;
    private TextView mTextViewReceiverName;
    private TextView mTextViewTitle;

    private SearchTaskListContract.OnSearchListListener mOnSearchListListener;

    public SearchTaskListViewHolder(@NonNull ViewGroup viewGroup,
                                    SearchTaskListContract.OnSearchListListener onSearchListListener) {

        super(viewGroup, R.layout.item_search_task_list);

        mOnSearchListListener = onSearchListListener;

        mLayoutContainer = (LinearLayout) itemView.findViewById(R.id.container_linearLayout);

        mTextViewDueDate = (TextView) itemView.findViewById(R.id.dueDate_textView);
        mEvaluationStarListView = (HFEvaluationStarListView) itemView.findViewById(R.id.evaluationStarListView);
        mTextViewTitle = (TextView) itemView.findViewById(R.id.title_textView);

        mTextViewMyTask = (TextView) itemView.findViewById(R.id.myTask_textView);
        mTextViewAuthorName = (TextView) itemView.findViewById(R.id.authorName_textView);
        mImageViewArrow = (ImageView) itemView.findViewById(R.id.arrow_imageView);
        mTextViewReceiverName = (TextView) itemView.findViewById(R.id.receiverName_textView);
    }


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

        final TaskListData data = (TaskListData) object;

        if (data != null) {
            if (TaskStatusType.CLOSED.getCode() == data.getStatus()) {//완료
                mTextViewDueDate.setTextColor(HFTaskUtil.getDueDateFontColor(getContext(), data.getStatus()));

                mTextViewDueDate.setText(HFDateUtil.getChangeDateFormat(data.getDue_to(), HFDateUtil.FORMAT_COMMON_SERVER, HFDateUtil.FORMAT_YYYY_MM_DD));
            } else {

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

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

            mEvaluationStarListView.setEvaluationData(data.getStatus(), data.getEvaluation());

            mTextViewTitle.setText(data.getTask_name());

//            mTextViewTitle.setTextColor(HFTaskUtil.getTaskFontColor(getContext(), data.getStatus()));

            if (data.getAuthor_member_id() == data.getMember_id() &&
                    data.getMember_id() == MemberDataRepository.getInstance().getMemberId()) {
                mTextViewAuthorName.setVisibility(View.GONE);
                mImageViewArrow.setVisibility(View.GONE);
                mTextViewReceiverName.setVisibility(View.GONE);

                mTextViewMyTask.setVisibility(View.VISIBLE);
            } else {
                mTextViewAuthorName.setText(data.getAuthor_name());
                mTextViewReceiverName.setText(data.getMember_name());
                mTextViewAuthorName.setVisibility(View.VISIBLE);
                mTextViewReceiverName.setVisibility(View.VISIBLE);
                mImageViewArrow.setVisibility(View.VISIBLE);

                mTextViewMyTask.setVisibility(View.GONE);
            }

            if (mOnSearchListListener != null) {
                mLayoutContainer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnSearchListListener.onTaskClicked(data);
                    }
                });
            }
        }
    }
}
