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

import android.support.annotation.NonNull;
import android.support.v4.text.TextUtilsCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.contract.AddTaskSelectCampaignListContract;
import kr.co.hanwha.hifive.base.BaseViewHolder;
import kr.co.hanwha.hifive.data.model.CampaignData;
import kr.co.hanwha.hifive.data.model.ProjectCampaignData;
import kr.co.hanwha.hifive.util.HFSpanUtil;

/**
 * Created by sanghoonkwon on 2018. 2. 21..
 */

public class SelectCampaignListViewHolder extends BaseViewHolder {
    private LinearLayout mLayoutBody;
    private ImageButton mImageButtonCheck;
    private TextView mTextViewProject;
    private View mViewLine;
    private TextView mTextViewCampaign;

    private String mSearchWord = "";
    private String mSelectedCampaignKey = "";

    private AddTaskSelectCampaignListContract.OnSelectCampaignListListener mOnSelectCampaignListListener;


    public SelectCampaignListViewHolder(@NonNull ViewGroup viewGroup,
                                        AddTaskSelectCampaignListContract.OnSelectCampaignListListener onSelectCampaignListListener) {
//        super(viewGroup, R.layout.temp_item_select_campaign_list);
        super(viewGroup, R.layout.item_select_campaign_list);

        mOnSelectCampaignListListener = onSelectCampaignListListener;

        mLayoutBody = (LinearLayout) itemView.findViewById(R.id.body_linearLayout);
        mImageButtonCheck = (ImageButton) itemView.findViewById(R.id.check_imageButton);
        mTextViewProject = (TextView) itemView.findViewById(R.id.project_textView);
        mViewLine = (View) itemView.findViewById(R.id.line_view);
        mTextViewCampaign = (TextView) itemView.findViewById(R.id.campaign_textView);
    }

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

    public void setSelectedCampaignKey(String selectedCampaignKey) {
        this.mSelectedCampaignKey = selectedCampaignKey;
    }

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

        if (object != null && object instanceof CampaignData) {
            final CampaignData data = (CampaignData) object;

            mImageButtonCheck.setSelected((mSelectedCampaignKey != null && mSelectedCampaignKey.equals(data.getKey())));

            HFSpanUtil.setSearchResult(getContext(), mTextViewProject, data.getProject_name(), mSearchWord);

            HFSpanUtil.setSearchResult(getContext(), mTextViewCampaign, data.getCampaign_desc(), mSearchWord);

            if (data.isFirstItem()) {
                mTextViewProject.setVisibility(View.VISIBLE);
                mViewLine.setVisibility(View.VISIBLE);
            } else {
                mTextViewProject.setVisibility(View.GONE);
                mViewLine.setVisibility(View.GONE);
            }

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