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.HFApplication;
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.MessageType;
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.HFDateUtil;
import kr.co.hanwha.hifive.util.HFTaskUtil;
import kr.co.hanwha.hifive.view.HFEvaluationStarListView;

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

public class MainHomeRequestListViewHolder extends BaseViewHolder {
    private LinearLayout mLayoutContainer;

    private TextView mTextViewDueDate;
    private HFEvaluationStarListView mEvaluationStarListView;
    private TextView mTextViewTitle;
    private TextView mTextViewReceiverName;

    private LinearLayout mLayoutActionEvaluation;
    private TextView mTextViewEvalReject;
    private TextView mTextViewEvalAccept;
    private LinearLayout mLayoutActionRejected;
    private TextView mTextViewDeleteRequest;
    private TextView mTextViewReRequest;

    private TextView mTextViewLog;

    private MainHomeContract.OnMainHomeListListener mOnMainHomeListListener;

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

        mOnMainHomeListListener = onMainHomeListListener;

        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);
        mTextViewReceiverName = (TextView) itemView.findViewById(R.id.receiverName_textView);

        mLayoutActionEvaluation = (LinearLayout) itemView.findViewById(R.id.actionEvaluation_linearLayout);
        mTextViewEvalReject = (TextView) itemView.findViewById(R.id.evalReject_textView);
        mTextViewEvalAccept = (TextView) itemView.findViewById(R.id.evalAccept_textView);
        mLayoutActionRejected = (LinearLayout) itemView.findViewById(R.id.actionRejected_linearLayout);
        mTextViewDeleteRequest = (TextView) itemView.findViewById(R.id.deleteRequest_textView);
        mTextViewReRequest = (TextView) itemView.findViewById(R.id.reRequest_textView);

        mTextViewLog = (TextView) itemView.findViewById(R.id.log_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());

            mTextViewReceiverName.setText(data.getMember_name());
//            mTextViewReceiverName.setTextColor(HFTaskUtil.getNameFontColor(getContext(), data.getStatus()));

            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);
            }


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

                mTextViewEvalAccept.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnMainHomeListListener.onEvalAcceptClicked(data);
                    }
                });

                mTextViewEvalReject.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnMainHomeListListener.onEvalRejectClicked(data);
                    }
                });

                mTextViewDeleteRequest.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnMainHomeListListener.onDeleteRequestClicked(data);
                    }
                });

                mTextViewReRequest.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnMainHomeListListener.onReRequestClicked(data);
                    }
                });
            }

            if (HFApplication.DEV_LOG) {
                String log = "TASK : " + data.getTask_id() +
                        " PROJECT : " + data.getProject_id() +
                        " TRACK : " + data.getTrack_id() +
                        "\nAuthorMemberId : " + data.getAuthor_member_id() +
                        " MemberId : " + data.getMember_id();

                mTextViewLog.setText(log);
                mTextViewLog.setVisibility(View.VISIBLE);
            } else {
                mTextViewLog.setVisibility(View.GONE);
            }
        }
    }
}
