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

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

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.enums.TaskFlagType;
import kr.co.hanwha.hifive.enums.TaskStatusType;
import kr.co.hanwha.hifive.util.GlideUtil;
import kr.co.hanwha.hifive.util.HFCommonUtil;
import kr.co.hanwha.hifive.util.HFSpanUtil;
import kr.co.hanwha.hifive.util.HFStringUtil;
import kr.co.hanwha.hifive.util.HFTaskUtil;

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

public class TaskDetailHeaderViewHolder extends BaseViewHolder {
    private LinearLayout mLayoutSender;
    private ImageView mImageViewSender;
    private TextView mTextViewSender;
    private ImageView mImageViewReceiver;
    private TextView mTextViewReceiver;
    private TextView mTextViewTitle;
    private ImageView mImageViewDueDate;
    private TextView mTextViewDueDateStr;
    private TextView mTextViewDueDate;
    private RelativeLayout mLayoutAction;
    private TextView mTextViewAction;

    private LinearLayout mLayoutLog;
    private TextView mTextViewLog;

    private Activity mActivity;
    private int mFlag;

    private TaskDetailListContract.OnTaskDetailListener onTaskDetailListener;

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

        super(viewGroup, R.layout.item_task_detail_header);

        mLayoutSender = (LinearLayout) itemView.findViewById(R.id.sender_linearLayout);
        mImageViewSender = (ImageView) itemView.findViewById(R.id.sender_imageView);
        mTextViewSender = (TextView) itemView.findViewById(R.id.sender_textView);
        mImageViewReceiver = (ImageView) itemView.findViewById(R.id.receiver_imageView);
        mTextViewReceiver = (TextView) itemView.findViewById(R.id.receiver_textView);
        mTextViewTitle = (TextView) itemView.findViewById(R.id.title_textView);
        mImageViewDueDate = (ImageView) itemView.findViewById(R.id.dueDate_imageView);
        mTextViewDueDateStr = (TextView) itemView.findViewById(R.id.dueDateStr_textView);
        mTextViewDueDate = (TextView) itemView.findViewById(R.id.dueDate_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);

        GlideUtil.getInstance().loadImageCircle(getContext(), R.mipmap.ic_avatar, mImageViewSender);
        GlideUtil.getInstance().loadImageCircle(getContext(), R.mipmap.ic_avatar, mImageViewReceiver);

        this.onTaskDetailListener = onTaskDetailListener;
    }

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

    public void setActivity(Activity activity) {
        mActivity = activity;
    }

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

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

            if (data != null) {
                // 보낸 사람
                if (TextUtils.isEmpty(data.getAuthor_name()) == false) {
                    GlideUtil.getInstance().loadImageCircle(getContext(), data.getAuthor_profile_url(), mImageViewSender);
                    mTextViewSender.setText(data.getAuthor_name());
                    mLayoutSender.setVisibility(View.VISIBLE);
                }

                // 받는 사람
                if (TextUtils.isEmpty(data.getMember_name()) == false) {
                    GlideUtil.getInstance().loadImageCircle(getContext(), data.getMember_profile_url(), mImageViewReceiver);
                    mTextViewReceiver.setText(data.getMember_name());
                }

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

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

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

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

                mTextViewDueDate.setText(HFStringUtil.getDisplayDueDateTaskDetail(data.getCreated_at(), data.getDue_to()));

                // 평가
                if (TaskFlagType.ASSIGNED_MY_TASK.getCode() == mFlag &&
                        (TaskStatusType.ASSIGNED.getCode() == data.getStatus() ||
                                TaskStatusType.DOING_TODAY.getCode() == data.getStatus())) {
                    mLayoutAction.setVisibility(View.VISIBLE);
                    mTextViewAction.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
//                            int x = (int) event.getRawX();
                            int x = HFCommonUtil.getScreenWidth(getContext()) / 2;
                            int y = (int) event.getRawY();
//                            int x = (int) v.getX() + v.getWidth() / 2;
//                            int y = (int) v.getY() + v.getHeight() / 2;
                            if (onTaskDetailListener != null)
                                onTaskDetailListener.onEvaluationClicked(x, y);
                            return false;
                        }
                    });
                }else {
                    mLayoutAction.setVisibility(View.GONE);
                }
            }
        }
    }

}
