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

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import kr.co.hanwha.hifive.HFApplication;
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.TaskDetailData;
import kr.co.hanwha.hifive.data.model.TaskDetailRoot;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.enums.TaskEvaluationType;
import kr.co.hanwha.hifive.enums.TaskFlagType;
import kr.co.hanwha.hifive.enums.TaskStatusType;
import kr.co.hanwha.hifive.util.HFCommonUtil;
import kr.co.hanwha.hifive.util.HFDateUtil;
import kr.co.hanwha.hifive.util.HFSpanUtil;
import kr.co.hanwha.hifive.util.HFStringUtil;
import kr.co.hanwha.hifive.util.HFTaskUtil;
import kr.co.hanwha.hifive.view.HFEvaluationStarListView;

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

public class TaskDetailHeaderViewHolder extends BaseViewHolder {
    private TextView mTextViewAuthorName;
    private ImageView mImageViewArrow;
    private TextView mTextViewReceiverName;
    private TextView mTextViewMyTask;

    private LinearLayout mLayoutCampaign;
    private TextView mTextViewCampaign;
    private TextView mTextViewProject;
    private TextView mTextViewTitle;
    private ImageView mImageViewDueDate;
    private TextView mTextViewDday;
    private TextView mTextViewDueDate;
    private View mViewClosedLine;
    private TextView mTextViewClosedDate;
    private RelativeLayout mLayoutAction;
    private TextView mTextViewAction;

    private TextView mTextViewStatus;
    private HFEvaluationStarListView mEvaluationStarListView;

    private LinearLayout mLayoutLog;
    private TextView mTextViewLog;

    private int mFlag;

    private TaskDetailListContract.OnTaskDetailListener onTaskDetailListener;

    public TaskDetailHeaderViewHolder(@NonNull ViewGroup viewGroup, @NonNull TaskDetailListContract.OnTaskDetailListener onTaskDetailListener) {

        super(viewGroup, R.layout.item_task_detail_header);

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

        mLayoutCampaign = (LinearLayout) itemView.findViewById(R.id.campaign_linearLayout);
        mTextViewCampaign = (TextView) itemView.findViewById(R.id.campaign_textView);
        mTextViewProject = (TextView) itemView.findViewById(R.id.project_textView);
        mTextViewTitle = (TextView) itemView.findViewById(R.id.title_textView);
        mImageViewDueDate = (ImageView) itemView.findViewById(R.id.dueDate_imageView);
        mTextViewDday = (TextView) itemView.findViewById(R.id.dDay_textView);
        mTextViewDueDate = (TextView) itemView.findViewById(R.id.dueDate_textView);
        mViewClosedLine = (View) itemView.findViewById(R.id.closedLine_view);
        mTextViewClosedDate = (TextView) itemView.findViewById(R.id.closedDate_textView);
        mLayoutAction = (RelativeLayout) itemView.findViewById(R.id.action_relativeLayout);
        mTextViewAction = (TextView) itemView.findViewById(R.id.action_textView);
        mLayoutLog = (LinearLayout) itemView.findViewById(R.id.log_linearLayout);
        mTextViewLog = (TextView) itemView.findViewById(R.id.log_textView);

        mTextViewStatus = (TextView) itemView.findViewById(R.id.status_textView);
        mEvaluationStarListView = (HFEvaluationStarListView) itemView.findViewById(R.id.evaluationStarListView);

        this.onTaskDetailListener = onTaskDetailListener;
    }

    public void setFlag(int flag) {
        this.mFlag = flag;
    }

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

        final TaskDetailRoot root = (TaskDetailRoot) object;
        if (root != null) {
            final TaskDetailData data = root.getTaskResult();

            if (data != null) {
                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 (root.getListCampaigns() != null && root.getListCampaigns().size() > 0) {
                    mLayoutCampaign.setVisibility(View.VISIBLE);
                    mTextViewCampaign.setText(root.getListCampaigns().get(0).getCampaign_desc()
                            + (TextUtils.isEmpty(data.getProject_name()) ? "" : "  |  " + data.getProject_name()));
//                    mTextViewProject.setText(TextUtils.isEmpty(data.getProject_name()) ? "" : data.getProject_name());
                } else {
                    mLayoutCampaign.setVisibility(View.GONE);
                }

                HFSpanUtil.setDescText(getContext(), mTextViewTitle, data.getDescription(), onTaskDetailListener);

                if (TaskStatusType.CLOSED.getCode() == data.getStatus()) {//완료
                    mTextViewDday.setVisibility(View.GONE);

                    mImageViewDueDate.setImageDrawable(getContext().getResources().getDrawable(R.mipmap.ic_date_gray));

//                    mTextViewDueDate.setTextColor(HFTaskUtil.getDueDateFontColor(getContext(), data.getStatus()));
                    mTextViewDueDate.setText(getContext().getString(R.string.task_detail_duedate) + " "
                            + HFDateUtil.getChangeDateFormat(data.getDue_to(), HFDateUtil.FORMAT_COMMON_SERVER, HFDateUtil.FORMAT_YYYY_MM_DD));

                    if (data.getEvaluation() != TaskEvaluationType.CANCELED.getCode()) {

                        mViewClosedLine.setVisibility(View.VISIBLE);
                        mTextViewClosedDate.setText(HFStringUtil.getDisplayClosedDateTaskDetail(getContext(), data.getUpdated_at()));
                        mTextViewClosedDate.setVisibility(View.VISIBLE);
                    } else {
                        mViewClosedLine.setVisibility(View.GONE);
                        mTextViewClosedDate.setVisibility(View.GONE);
                    }
                } else {
                    mImageViewDueDate.setImageDrawable(HFTaskUtil.getDueDateIconDrawable(getContext(), data.getDue_to(), root.getServer_time()));

                    mTextViewDday.setVisibility(View.VISIBLE);
                    mTextViewDday.setTextColor(HFTaskUtil.getDueDateFontColor(getContext(), data.getDue_to(), root.getServer_time()));
                    mTextViewDday.setText(HFTaskUtil.getDueDateText(getContext(), data.getDue_to(), root.getServer_time()));
                    
//                    mTextViewDueDate.setTextColor(HFTaskUtil.getDueDateFontColor(getContext(), data.getDue_to(), root.getServer_time()));
                    mTextViewDueDate.setText(getContext().getString(R.string.task_detail_duedate) + " "
                            + HFDateUtil.getChangeDateFormat(data.getDue_to(), HFDateUtil.FORMAT_COMMON_SERVER, HFDateUtil.FORMAT_YYYY_MM_DD));

                    mViewClosedLine.setVisibility(View.GONE);
                    mTextViewClosedDate.setVisibility(View.GONE);
                }

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

                // 평가
                if (TaskFlagType.ASSIGNED_MY_TASK.getCode() == mFlag &&
                        (TaskStatusType.ASSIGNED.getCode() == data.getStatus() ||
                                TaskStatusType.DOING_TODAY.getCode() == data.getStatus() ||
                                TaskStatusType.REJECTED_EVALUATION.getCode() == data.getStatus())) {
                    mLayoutAction.setVisibility(View.VISIBLE);
                    mTextViewAction.setAlpha(1.0f);
                    Animation animFadeIn = AnimationUtils.loadAnimation(getContext(), R.anim.fade_in);
                    animFadeIn.setFillAfter(true);
                    mTextViewAction.startAnimation(animFadeIn);

                    mTextViewAction.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (onTaskDetailListener != null)
                                onTaskDetailListener.onEvaluationClicked();
                        }
                    });
                } else {
                    mLayoutAction.setVisibility(View.GONE);
                }

                //Log
                if (HFApplication.DEV_LOG) {
                    mLayoutLog.setVisibility(View.VISIBLE);
                    try {
//                        mTextViewLog.setText(data.toString());
                        mTextViewLog.setText(root.toString());
                        mTextViewLog.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                HFCommonUtil.copyClipboard(getContext(), data.toString(), "클립보드에 복사되었습니다.");
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    mLayoutLog.setVisibility(View.GONE);
                }
            }
        }
    }

}
