package com.phlx.wool.ui.search.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.phlx.wool.R;
import com.phlx.wool.entity.Unit;
import com.phlx.wool.ui.search.interf.IOnUnitItemClickListener;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UnitSearchAdapter extends RecyclerView.Adapter<UnitSearchAdapter.MyViewHolder> implements View.OnClickListener {

    private Context context;

    private ArrayList<Unit> units = new ArrayList<>();

    public void setHighlightStr(String highlightStr) {
        this.highlightStr = highlightStr;
    }

    private String highlightStr = null;

    public UnitSearchAdapter(Context context, ArrayList<Unit> units) {
        this.context = context;
        this.units = units;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).
                inflate(R.layout.item_unit_search, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.name.setText(units.get(position).getManagerName());
        holder.phone.setText(units.get(position).getManagerPhone());
        holder.id.setText(units.get(position).getManagerCode());
        holder.idCard.setText(units.get(position).getIdCard());

        if (highlightStr != null) {
            setSpecifiedText(holder.name, highlightStr);
            setSpecifiedText(holder.phone, highlightStr);
            setSpecifiedText(holder.id, highlightStr);
            setSpecifiedText(holder.idCard, highlightStr);
        }
        holder.itemView.setTag(position);

    }

    @Override
    public int getItemCount() {
        return units.size();
    }

    @Override
    public void onClick(View v) {
        if (iOnUnitItemClickListener != null && units.size() > 0) {
            iOnUnitItemClickListener.onItemClick(units.get((Integer) v.getTag()));
        }

    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView name;
        TextView phone;
        TextView id;
        TextView idCard;

        public MyViewHolder(View view) {
            super(view);
            name = (TextView) view.findViewById(R.id.name_tv);
            phone = (TextView) view.findViewById(R.id.phone_tv);
            id = (TextView) view.findViewById(R.id.id_tv);
            idCard = (TextView) view.findViewById(R.id.idCard_tv);
        }
    }

    private IOnUnitItemClickListener iOnUnitItemClickListener;

    public void setOnItemClickListener(IOnUnitItemClickListener iOnUnitItemClickListener) {
        this.iOnUnitItemClickListener = iOnUnitItemClickListener;
    }


    private void setSpecifiedText(TextView textView, String specifiedText) {
        String[] keyword = new String[]{specifiedText};
        SpannableStringBuilder spannable =
                new SpannableStringBuilder(textView.getText().toString());
        CharacterStyle span;
        String wordReg;
        for (int i = 0; i < keyword.length; i++) {
            String key = "";
            //  处理通配符问题
            if (keyword[i].contains("*") || keyword[i].contains("(") || keyword[i].contains(")")) {
                char[] chars = keyword[i].toCharArray();
                for (int k = 0; k < chars.length; k++) {
                    if (chars[k] == '*' || chars[k] == '(' || chars[k] == ')') {
                        key = key + "\\" + String.valueOf(chars[k]);
                    } else {
                        key = key + String.valueOf(chars[k]);
                    }
                }
                keyword[i] = key;
            }

            wordReg = "(?i)" + keyword[i];   //忽略字母大小写
            Pattern pattern = Pattern.compile(wordReg);
            Matcher matcher = pattern.matcher(textView.getText().toString());
            while (matcher.find()) {
                span = new ForegroundColorSpan(Color.parseColor("#e81c1c"));
                spannable.setSpan(span, matcher.start(), matcher.end(), Spannable.SPAN_MARK_MARK);
            }
        }
        textView.setText(spannable);
    }

}
