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.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

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.GlideUtil;
import kr.co.hanwha.hifive.util.HFCommonUtil;
import kr.co.hanwha.hifive.util.HFTaskUtil;

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

public class MainHomeAssignedListViewHolder extends BaseViewHolder {
    private LinearLayout mLayoutContainer;
    private ImageButton mImageButtonCheckToday;
    private TextView mTextViewTitle;
    private ImageView mImageViewSenderProfile;
    private TextView mTextViewSenderName;
    private ImageView mImageViewIcon;
    private TextView mTextViewDueDate;
    private LinearLayout mLayoutAction;
    private LinearLayout mLayoutReject;
    private LinearLayout mLayoutAccept;
    private boolean mNeedAnimation = false;

    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);
        mImageButtonCheckToday = (ImageButton) itemView.findViewById(R.id.checkToday_imageButton);
        mTextViewTitle = (TextView) itemView.findViewById(R.id.title_textView);
        mImageViewSenderProfile = (ImageView) itemView.findViewById(R.id.senderProfile_imageView);
        mTextViewSenderName = (TextView) itemView.findViewById(R.id.senderName_textView);
        mImageViewIcon = (ImageView) itemView.findViewById(R.id.icon_imageView);
        mTextViewDueDate = (TextView) itemView.findViewById(R.id.dueDate_textView);
        mLayoutAction = (LinearLayout) itemView.findViewById(R.id.action_linearLayout);
        mLayoutReject = (LinearLayout) itemView.findViewById(R.id.reject_linearLayout);
        mLayoutAccept = (LinearLayout) itemView.findViewById(R.id.accept_linearLayout);

        GlideUtil.getInstance().loadImageCircle(getContext(), R.mipmap.ic_avatar, mImageViewSenderProfile);
    }

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

        final TaskListData data = (TaskListData) object;

        if (data != null) {
            if (data.getAuthor_member_id() == data.getMember_id()) {
                mImageViewSenderProfile.setVisibility(View.INVISIBLE);
                mTextViewSenderName.setVisibility(View.INVISIBLE);
            } else {
                if (TextUtils.isEmpty(data.getAuthor_profile_url()))
                    GlideUtil.getInstance().loadImageCircle(getContext(), R.mipmap.ic_avatar, mImageViewSenderProfile);
                else
                    GlideUtil.getInstance().loadImageCircle(getContext(), data.getAuthor_profile_url(), mImageViewSenderProfile);

                mTextViewSenderName.setText(String.format(getContext().getString(R.string.main_home_sender), data.getAuthor_name()));

                mImageViewSenderProfile.setVisibility(View.VISIBLE);
                mTextViewSenderName.setVisibility(View.VISIBLE);
            }
            mTextViewTitle.setText(data.getTask_name());

            mTextViewTitle.setBackground(null);

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

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

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

            if (data.getStatus() == TaskStatusType.REQUESTED.getCode()) {
                mNeedAnimation = true;
                mImageButtonCheckToday.setVisibility(View.GONE);
                mLayoutAction.setVisibility(View.VISIBLE);
            } else if (data.getStatus() == TaskStatusType.CLOSED.getCode()) {
                mImageButtonCheckToday.setVisibility(View.GONE);
                mLayoutAction.setVisibility(View.GONE);
                mTextViewDueDate.setText("완료");// TODO - CHECK
            } else {
                mImageButtonCheckToday.setVisibility(View.VISIBLE);
                mImageButtonCheckToday.setSelected(data.isIs_today());
                mLayoutAction.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());
                    }
                });
                mLayoutReject.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnMainHomeListListener.onTaskRejectClicked(data);
                    }
                });
                mLayoutAccept.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnMainHomeListListener.onTaskAcceptClicked(data);
                    }
                });
            }
        }
    }

    public void checkAnimation() {
        if (mNeedAnimation) {
            mNeedAnimation = false;
            mLayoutAction.postDelayed(new Runnable() {
                @Override
                public void run() {
                    float fromYDelta = -HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp10);
                    Animation anim = new TranslateAnimation(0, 0, fromYDelta, 0);
                    anim.setDuration(300);
                    anim.setInterpolator(AnimationUtils.loadInterpolator(getContext(), android.R.anim.overshoot_interpolator));
                    anim.setFillAfter(true);
                    mLayoutAction.startAnimation(anim);
                }
            }, 100);
        }
    }
}