package com.navinfo.outdoor.ui.view;

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.view.ViewParent;
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.api.Constant;
import com.navinfo.outdoor.bean.PhoneBean;

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

/**
 *手机号数据存储
 */
public class ContactView {
    private Context context;
    private ViewGroup rootView;
    private List<PhoneBean> poiBeanList;

    public ContactView(Context context, ViewGroup roatView, List<PhoneBean> poiBeans) {
        this.context = context;
        this.rootView = roatView;
        this.poiBeanList = poiBeans;
    }

    public void resetView() {
        if (poiBeanList != null && !poiBeanList.isEmpty()) {
            if (rootView != null) {
                rootView.removeAllViews();
            }
            for (int i = 0; i < poiBeanList.size(); i++) {
                PhoneBean poiBean = poiBeanList.get(i);
                View itemView = LayoutInflater.from(context).inflate(R.layout.item_poi, null, false);
                MyViewHolder holder = new MyViewHolder(itemView);
                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());
                int finalI = i;
                holder.image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (finalI == 0) {
                            View view = addRootView();
                            rootView.addView(view);
                        } else {
                            ViewParent parent = v.getParent();
                            rootView.removeView((View) parent);
                        }
//                        rootView.removeViewAt(i);
                    }
                });
                if (poiBean.getPhone()==null){
                    holder.editPhoneNumber.setText("");
                }else {
                    holder.editPhoneNumber.setText(poiBean.getPhone()+"");
                }

                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) {
                    }
                });
                rootView.addView(itemView);
            }
        }
    }

    /**
     * 添加数据
     * @return
     */
    public View addRootView() {
        PhoneBean poiBean=null;
        if (Constant.CODE==null||Constant.CODE.equals("")){
             poiBean = new PhoneBean("", "", "", R.drawable.icon_del_bg);
        }else {
            poiBean= new PhoneBean("", "", Constant.CODE, R.drawable.icon_del_bg);
        }
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_poi, null, false);
        MyViewHolder holder = new MyViewHolder(itemView);
        holder.editAreaCode.setText(poiBean.getArea());
        holder.tvPhone.setText(poiBean.getName());
        holder.image.setImageResource(poiBean.getImage());
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewParent parent = v.getParent();
                rootView.removeView((View) parent);
//                  rootView.removeViewAt(i);
            }
        });
        holder.editPhoneNumber.setText(poiBean.getPhone());
        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) {
            }
        });
        return itemView;
    }

    /**
     * 获取数据
     *
     * @return
     */
    public List<PhoneBean> getPoiBeanListByRootView() {
        ArrayList<PhoneBean> poiBeans = new ArrayList<>();
        if (rootView != null) {
            for (int i = 0; i < rootView.getChildCount(); i++) {
                View itemView = rootView.getChildAt(i);
                EditText editAreaCode = itemView.findViewById(R.id.edit_area_code);
                EditText editPhoneNumber = itemView.findViewById(R.id.edit_phone_number);
                String areaCode = "";
                if (editAreaCode.isShown()) {
                    areaCode = editAreaCode.getText().toString().trim();
                }
                poiBeans.add(new PhoneBean(null, editPhoneNumber.getText().toString().trim(), areaCode, 0));
            }
        }
        return poiBeans;
    }

    /**
     * 检验数据
     *
     * @return
     */
    public boolean getPhoneBean() {
        List<PhoneBean> list = getPoiBeanListByRootView();
        for (int i = 0; i < list.size(); i++) {
            PhoneBean poiBean = list.get(i);
            if (poiBean.getPhone().startsWith("1")) {
                if (poiBean.getPhone().length()!=11){
                    return false;
                }
            } else {
              if (poiBean.getArea().length()+poiBean.getPhone().length()<10 || poiBean.getArea().length()+poiBean.getPhone().length()>12||poiBean.getArea().length()<2){
                  return false;
              }
            }

        }
        return true;
    }

    /**
     * 检验数据
     *
     * @return
     */
    public int getPhoneList() {
        List<PhoneBean> list = getPoiBeanListByRootView();
        for (int i = 0; i < list.size(); i++) {
            PhoneBean poiBean = list.get(i);
            if (poiBean.getPhone().startsWith("1")) {
                if (poiBean.getPhone().length()!=11){
                    return i+1;
                }
            } else {
                if (poiBean.getArea().length()+poiBean.getPhone().length()<10 || poiBean.getArea().length()+poiBean.getPhone().length()>12||poiBean.getArea().length()<2){
                    return i+1;
                }
            }

        }
        return -1;
    }
    /**
     * 整理数据
     *
     * @return
     */
    public String getList() {
        List<PhoneBean> list = getPoiBeanListByRootView();
        StringBuffer phoneSb = new StringBuffer("");
        for (int i = 0; i < list.size(); i++) {
            PhoneBean poiBean = list.get(i);
            if (poiBean.getPhone().startsWith("1")) {
                phoneSb.append(poiBean.getPhone());
            } else {
                phoneSb.append(poiBean.getArea()).append("-").append(poiBean.getPhone());
            }
            phoneSb.append("|");
        }
        String result = phoneSb.toString();
        if (result.length()>0&&result.endsWith("|")) {
            result = result.substring(0, result.length()-1);
        }
        return result;
    }

    /**
     * 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);
        }
    }
}
