package com.ycgis.macall.personalcenter.v.custom;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.PopupWindow;

import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.m.adapterbean.SelectAddressBean;
import com.ycgis.macall.personalcenter.p.callback.SelectReturnCallback;
import com.ycgis.macall.personalcenter.v.adapter.SelectAddressAdapter;

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

/**
 * created by:   Macall
 * create time:  2022/11/14 16:10
 * copyright:    @ruansee.com
 * Describe:
 */
public class SelectAddressPopupWindow extends PopupWindow {
    private SelectAddressAdapter addressAdapter;
    private String[] names = {"省厅:3400", "合肥:3401", "芜湖:3402", "蚌埠:3403", "淮南:3404",
            "马鞍山:3405", "淮北:3406", "铜陵:3407", "安庆:3408", "黄山:3410", "滁州:3411",
            "阜阳:3412", "宿州:3413", "六安:3415", "亳州:3416", "池州:3417", "宣城:3418"};


    public SelectAddressPopupWindow(Activity activity, SelectReturnCallback<SelectAddressBean> selectReturnCallback, int matchParent, int dip2px) {
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_select_all_ds, null, false);
        GridView view1 = view.findViewById(R.id.gv_grid);
        addressAdapter = new SelectAddressAdapter(activity, getData());
        view1.setAdapter(addressAdapter);
        view1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SelectAddressBean item = addressAdapter.getItem(position);
                for (int i = 0; i <addressAdapter.getCount(); i++) {
                    addressAdapter.getItem(i).setSelect(false);
                }
                item.setSelect(true);
                addressAdapter.notifyDataSetChanged();
                selectReturnCallback.returnMsg(item);
                dismiss();
            }
        });

        setContentView(view);

        setContentView(view);
//设置宽度
        setWidth(matchParent);
//设置高度
        setHeight(dip2px);
//设置显示隐藏动画
        setAnimationStyle(R.style.DialogScaleAnim);
//设置背景透明
        setBackgroundDrawable(new ColorDrawable(0));

    }

    private List<SelectAddressBean> getData() {
        List<SelectAddressBean> list = new ArrayList<>();
        for (int i = 0; i < names.length; i++) {
            String[] split = names[i].split(":");
            SelectAddressBean bean = new SelectAddressBean();
            bean.setName(split[0]);
            bean.setCode(split[1]);
            bean.setSelect(false);
            list.add(bean);
        }
        list.get(0).setSelect(true);
        return list;
    }

}
