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

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.admin.admin.adapter.AdminMemberListAdapter;
import kr.co.hanwha.hifive.admin.network.AdminMemberUtil;
import kr.co.hanwha.hifive.admin.network.MemberStatusCode;
import kr.co.hanwha.hifive.admin.network.model.AdminMemberHrInfo;
import kr.co.hanwha.hifive.base.BaseViewHolder;
import kr.co.hanwha.hifive.util.GlideUtil;
import kr.co.hanwha.hifive.util.HFSpanUtil;

/**
 * Created by sanghoonkwon on 2018. 3. 9..
 */

public class AdminMemberListViewHolder extends BaseViewHolder {
    private ImageView mImageViewDot;
    private ImageView mImageViewSender;
    private TextView mTextViewTitle;
    private TextView mTextViewMessage;
    private TextView mTextViewDate;
    private TextView mTextViewStatus;
    private View mViewUnderline;
    private String mSearchWord = "";

    private AdminMemberListAdapter.OnAdminMemberListListener mOnAdminMemberListListener;

    public AdminMemberListViewHolder(@NonNull ViewGroup viewGroup,
                                     AdminMemberListAdapter.OnAdminMemberListListener onAdminMemberListListener) {
        super(viewGroup, R.layout.item_admin_member_list);

        mOnAdminMemberListListener = onAdminMemberListListener;

        mImageViewDot = (ImageView) itemView.findViewById(R.id.dot_imageView);
        mImageViewSender = (ImageView) itemView.findViewById(R.id.sender_imageView);
        mTextViewTitle = (TextView) itemView.findViewById(R.id.title_textView);
        mTextViewMessage = (TextView) itemView.findViewById(R.id.message_textView);
        mTextViewDate = (TextView) itemView.findViewById(R.id.date_textView);
        mTextViewStatus = (TextView) itemView.findViewById(R.id.status_textView);

        mViewUnderline = (View) itemView.findViewById(R.id.underline_view);

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

    public void setSearchWord(String searchWord) {
        this.mSearchWord = searchWord;
    }

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

        if (object != null && object instanceof AdminMemberHrInfo) {
            final AdminMemberHrInfo data = (AdminMemberHrInfo) object;
//            if (TextUtils.isEmpty(data.getProfile_url()))
            GlideUtil.getInstance().loadImageCircle(getContext(), R.mipmap.ic_avatar, mImageViewSender);
//            else
//                GlideUtil.getInstance().loadImageCircle(getContext(), data.getProfile_url(), mImageViewSender);

            mImageViewDot.setVisibility(data.isDeleted() ? View.INVISIBLE : View.VISIBLE);

            String name = TextUtils.isEmpty(data.getName()) ? "" : data.getName();

            HFSpanUtil.setSearchResult(getContext(), mTextViewTitle, name, mSearchWord);

            String message = (data.getCompany() + (TextUtils.isEmpty(data.getCompany()) ? "" : " | ") + data.getDepartment());
            HFSpanUtil.setSearchResult(getContext(), mTextViewMessage, message, mSearchWord);

            mTextViewDate.setText("MemberId : " + data.getId() + "  Status : " + data.getStatus());

            mTextViewStatus.setText(AdminMemberUtil.getMessage(data.getStatus()));

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnAdminMemberListListener != null)
                        mOnAdminMemberListListener.onItemClicked(data);
                }
            });
        }
    }
}