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.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.contract.AddTaskSelectMemberListContract;
import kr.co.hanwha.hifive.base.BaseViewHolder;
import kr.co.hanwha.hifive.data.model.MemberInfoData;
import kr.co.hanwha.hifive.util.GlideUtil;

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

public class SelectMemberListViewHolder extends BaseViewHolder {
    private View mViewTopLine;
    private LinearLayout mLayoutBody;
    private ImageButton mImageButtonCheck;
    private ImageView mImageViewProfile;
    private TextView mTextViewName;
    private View mViewDivider;
    private TextView mTextViewPosition;
    private TextView mTextViewProject;

    private AddTaskSelectMemberListContract.OnSelectMemberListListener mOnSelectMemberListListener;


    public SelectMemberListViewHolder(@NonNull ViewGroup viewGroup,
                                      AddTaskSelectMemberListContract.OnSelectMemberListListener onSelectMemberListListener) {
        super(viewGroup, R.layout.item_select_member_list);

        mOnSelectMemberListListener = onSelectMemberListListener;

        mViewTopLine = (View) itemView.findViewById(R.id.topLine_view);
        mLayoutBody = (LinearLayout) itemView.findViewById(R.id.body_linearLayout);
        mImageButtonCheck = (ImageButton) itemView.findViewById(R.id.check_imageButton);
        mImageViewProfile = (ImageView) itemView.findViewById(R.id.profile_imageView);
        mTextViewName = (TextView) itemView.findViewById(R.id.name_textView);
        mViewDivider = (View) itemView.findViewById(R.id.divider_view);
        mTextViewPosition = (TextView) itemView.findViewById(R.id.position_textView);
        mTextViewProject = (TextView) itemView.findViewById(R.id.project_textView);

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

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

        final MemberInfoData data = (MemberInfoData) object;
        if (data != null) {
            mViewTopLine.setVisibility(data.isTopLine() ? View.VISIBLE : View.GONE);

            mImageButtonCheck.setSelected(data.isSelected());

            mViewDivider.setVisibility(View.VISIBLE);
            mTextViewPosition.setVisibility(View.VISIBLE);

            if (TextUtils.isEmpty(data.getProfile_url()))
                GlideUtil.getInstance().loadImageCircle(getContext(), R.mipmap.ic_avatar, mImageViewProfile);
            else
                GlideUtil.getInstance().loadImageCircle(getContext(), data.getProfile_url(), mImageViewProfile);

            mTextViewName.setText(data.getMember_name());

            mTextViewPosition.setText(data.getRank());

            mTextViewProject.setText(data.getProject_name());

            mImageButtonCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnSelectMemberListListener != null)
                        mOnSelectMemberListListener.onItemClicked(data, index);
                }
            });
            mLayoutBody.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnSelectMemberListListener != null)
                        mOnSelectMemberListListener.onItemClicked(data, index);
                }
            });
        }
    }
}
