package com.cooleshow.base.adapter;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cooleshow.base.R;
import com.cooleshow.base.bean.AddressBean;
import com.cooleshow.base.router.RouterPath;

import java.util.List;

/**
 * 创建日期：2022/5/17 15:32
 *
 * @author Ryan
 * 类说明：
 */
public class AddressListAdapter extends BaseQuickAdapter<AddressBean, BaseViewHolder> {

    private OnDelClickListener mDelClickListener;

    public AddressListAdapter() {
        super(R.layout.layout_address_item);
    }


    @Override
    protected void convert(BaseViewHolder helper, AddressBean item) {
        TextView tv_name = helper.getView(R.id.tv_name);
        TextView tv_phone = helper.getView(R.id.tv_phone);
        TextView tv_address = helper.getView(R.id.tv_address);
        View view_line = helper.getView(R.id.view_line);
        tv_name.setText(item.name);
        String cPhone = item.phoneNumber;
        if (item.phoneNumber.length() == 11) {
            cPhone = item.phoneNumber.substring(0, 3) + "****" + item.phoneNumber.substring(7);
        }
        tv_phone.setText(cPhone);
        String addressDetail = item.province + item.city + item.region + item.detailAddress;
        tv_address.setText(addressDetail);
        if (getData().size() - 1 == getItemPosition(item)) {
            view_line.setVisibility(View.GONE);
        } else {
            view_line.setVisibility(View.VISIBLE);
        }
        ImageView im_modify = helper.getView(R.id.im_modify);
        im_modify.setOnClickListener(view -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("AddressBean", item);

            ARouter.getInstance().build(RouterPath.BaseCenter.MINE_EDIT_ADDRESS)
                    .withBoolean("isAdd", false)
                    .withBundle("bundle", bundle)
                    .navigation();
        });

        helper.getView(R.id.im_del).setOnClickListener(v -> {
            if(mDelClickListener!=null){
                mDelClickListener.delAddress(helper.getAbsoluteAdapterPosition());
            }
        });
    }

    public void setDelClickListener(OnDelClickListener delClickListener) {
        mDelClickListener = delClickListener;
    }

    public interface OnDelClickListener{
        void delAddress(int position);
    }
}
