package com.navinfo.outdoor.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.navinfo.outdoor.R;
import com.navinfo.outdoor.bean.PhoneBean;

import java.util.ArrayList;
import java.util.List;

/**
 * poi 适配器
 */
public class PoiRecycleAdapter extends RecyclerView.Adapter<PoiRecycleAdapter.MyViewHolder> {
    private Context context;
    private List<PhoneBean> list=new ArrayList<>();

    public PoiRecycleAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<PhoneBean> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }
    public boolean getPhoneBean(){
        for (int i = 0; i < list.size(); i++) {
            PhoneBean poiBean = list.get(i);
            if (poiBean.getPhone()==null){
                return false;
            }
        }
        return true;
    }


    public String getList() {
        String a="";
        for (int i = 0; i < list.size(); i++) {
            PhoneBean poiBean = list.get(i);
            if (list.size()==1){
                if(poiBean.getPhone().indexOf('1')==0){
                    a=poiBean.getPhone();
                }else {
                    a=poiBean.getArea()+"-"+poiBean.getPhone();
                }
            }else {
                if (i==list.size()-1){
                    if(poiBean.getPhone().indexOf('1')==0){
                        a+=poiBean.getPhone();
                    }else {
                        a+=poiBean.getArea()+"-"+poiBean.getPhone();
                    }
                }else{
                    if(poiBean.getPhone().indexOf('1')==0){
                        a+=poiBean.getPhone()+"|";
                    }else {
                        a+=poiBean.getArea()+"-"+poiBean.getPhone()+"|";
                    }
                }

            }

        }
        return a;
    }

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

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        PhoneBean poiBean = list.get(position);
        if(poiBean.getPhone().startsWith("1")){
            holder.llPoi.setVisibility(View.GONE);
        }else {
            holder.llPoi.setVisibility(View.VISIBLE);
        }
        holder.editAreaCode.setText(poiBean.getArea());
        holder.tvPhone.setText(poiBean.getName());
        holder.image.setImageResource(poiBean.getImage());
        holder.editPhoneNumber.setText(poiBean.getPhone());
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == 0) {
                    addData(new PhoneBean("", "","", R.drawable.icon_del_bg));
                } else {
                    removeData(position);
                }
            }
        });
        holder.editAreaCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String area = s.toString().trim();
                poiBean.setArea(area);
            }
        });

        holder.editPhoneNumber.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //正在输入
                String str = holder.editPhoneNumber.getText().toString().trim();
                if(str.indexOf('1')==0){
                   holder.llPoi.setVisibility(View.GONE);
                }else {
                    holder.llPoi.setVisibility(View.VISIBLE);
                }

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //输入之前

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString().trim();
                poiBean.setPhone(str);
            }
        });

    }

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

    // 添加数据
    public void addData(PhoneBean poiBean) {
        //在list中添加数据，并通知条目加入一条
        list.add(poiBean);
        //添加动画
        notifyDataSetChanged();
    }

    // 删除数据
    public void removeData(int position) {
        list.remove(position);
        //删除动画
        notifyItemRemoved(position);

    }

    /**
     * ViewHolder的类，用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvPhone;
        EditText editAreaCode, editPhoneNumber;
        ImageView image;
        LinearLayout llPoi;

        //因为删除有可能会删除中间条目，然后会造成角标越界，所以必须整体刷新一下！
        public MyViewHolder(View view) {
            super(view);
            tvPhone = view.findViewById(R.id.tv_phone);
            editAreaCode = view.findViewById(R.id.edit_area_code);
            llPoi = view.findViewById(R.id.ll_poi);
            editPhoneNumber = view.findViewById(R.id.edit_phone_number);
            image = view.findViewById(R.id.image);
        }
    }
}
