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

import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
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.TaskStatusType;
import kr.co.hanwha.hifive.screen.main.home.MainHomeContract;
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. 24..
 */

public class MainHomeAssignedListViewHolder extends BaseViewHolder {
    private LinearLayout mLayoutContainer;
    private LinearLayout mLayoutAuthor;
    private TextView mTextViewMyTask;
    private TextView mTextViewAuthorName;
    private ImageButton mImageButtonCheckToday;
    private TextView mTextViewTitle;
    private View mViewTitleMargin;
    private TextView mTextViewDueDate;
    private LinearLayout mLayoutAction;
    private TextView mTextViewReject;
    private TextView mTextViewAccept;
    private HFEvaluationStarListView mEvaluationStarListView;
    private TextView mTextViewLog;

    private MainHomeContract.OnMainHomeListListener mOnMainHomeListListener;

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

        mOnMainHomeListListener = onMainHomeListListener;

        mLayoutContainer = (LinearLayout) itemView.findViewById(R.id.container_linearLayout);
        mLayoutAuthor = (LinearLayout) itemView.findViewById(R.id.authorName_linearLayout);
        mTextViewMyTask = (TextView) itemView.findViewById(R.id.myTask_textView);
        mTextViewAuthorName = (TextView) itemView.findViewById(R.id.authorName_textView);
        mImageButtonCheckToday = (ImageButton) itemView.findViewById(R.id.checkToday_imageButton);
        mTextViewTitle = (TextView) itemView.findViewById(R.id.title_textView);
        mViewTitleMargin = (View) itemView.findViewById(R.id.titleMargin_view);
        mTextViewDueDate = (TextView) itemView.findViewById(R.id.dueDate_textView);
        mLayoutAction = (LinearLayout) itemView.findViewById(R.id.action_linearLayout);
        mTextViewReject = (TextView) itemView.findViewById(R.id.reject_textView);
        mTextViewAccept = (TextView) itemView.findViewById(R.id.accept_textView);
        mEvaluationStarListView = (HFEvaluationStarListView) itemView.findViewById(R.id.evaluationStarListView);
        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) {
            mTextViewAuthorName.setText(data.getAuthor_name());
            if (data.getAuthor_member_id() == data.getMember_id()) {
                mLayoutAuthor.setVisibility(View.GONE);
                mTextViewMyTask.setVisibility(View.VISIBLE);
            } else {
                mLayoutAuthor.setVisibility(View.VISIBLE);
                mTextViewMyTask.setVisibility(View.GONE);
            }

            mTextViewTitle.setText(data.getTask_name());

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

            if (data.getStatus() == TaskStatusType.REQUESTED.getCode()) {
                mImageButtonCheckToday.setVisibility(View.GONE);
                mViewTitleMargin.setVisibility(View.VISIBLE);
                mLayoutAction.setVisibility(View.VISIBLE);
                mEvaluationStarListView.setVisibility(View.VISIBLE);
            } else if (data.getStatus() == TaskStatusType.EVALUATED.getCode()) {
                mImageButtonCheckToday.setVisibility(View.GONE);
                mViewTitleMargin.setVisibility(View.VISIBLE);
                mLayoutAction.setVisibility(View.GONE);
                mEvaluationStarListView.setVisibility(View.VISIBLE);
            } else if (data.getStatus() == TaskStatusType.CLOSED.getCode()) {
                mImageButtonCheckToday.setVisibility(View.GONE);
                mViewTitleMargin.setVisibility(View.VISIBLE);
                mLayoutAction.setVisibility(View.GONE);
                mEvaluationStarListView.setVisibility(View.VISIBLE);
            } else {
                mImageButtonCheckToday.setSelected(data.isIs_today());
                mImageButtonCheckToday.setVisibility(View.VISIBLE);
                mViewTitleMargin.setVisibility(View.GONE);
                mLayoutAction.setVisibility(View.GONE);
                mEvaluationStarListView.setVisibility(View.GONE);
            }

            if (mOnMainHomeListListener != null) {
                mLayoutContainer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mOnMainHomeListListener.onItemClicked(data);
                    }
                });
                mImageButtonCheckToday.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnMainHomeListListener.updateTodoTaskToTodayTask(data, !data.isIs_today());
                    }
                });
                mTextViewReject.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnMainHomeListListener.onTaskRejectClicked(data);
                    }
                });
                mTextViewAccept.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnMainHomeListListener.onTaskAcceptClicked(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);
            }
        }
    }
}