package com.sjkj.appthreefloor_tsgz.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.bean.ImageBean;
import com.sjkj.appthreefloor_tsgz.databinding.ItemImageListBinding;
import com.sjkj.appthreefloor_tsgz.utils.AppSystemUtils;
import com.sjkj.appthreefloor_tsgz.utils.ItemSlideCallBack;
import com.sjkj.base_lib.adapter.BaseRecAdapter;

import java.util.Collections;
import java.util.List;

public class ImageAdapter extends BaseRecAdapter<ImageBean, ItemImageListBinding> implements ItemSlideCallBack {
    private OnDeleteClick onDeleteClick;

    public ImageAdapter(Context context) {
        super(context);
    }

    public ImageAdapter(Context context, List<ImageBean> list) {
        super(context, list);
    }

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_image_list;
    }

    /**
     * 设置列表的点击事件回调接口
     */
    public void setOnDeleteClick(OnDeleteClick onDeleteClick) {
        this.onDeleteClick = onDeleteClick;
    }

    @Override
    protected void onBindItem(ItemImageListBinding binding, ImageBean bean, int position) {
        binding.setModel(bean);
        binding.executePendingBindings();
        if (AppSystemUtils.isGhb()) {
            binding.rlImage.setLayoutParams(new RelativeLayout.LayoutParams(300, 300));
            ViewGroup.LayoutParams params = binding.ivIcon.getLayoutParams();
            params.height = 280;
            params.width = 280;
            binding.ivIcon.setLayoutParams(params);
            ViewGroup.LayoutParams params_del = binding.deleteIv.getLayoutParams();
            params_del.height = 75;
            params_del.width = 75;
            binding.deleteIv.setLayoutParams(params_del);
        }
//        binding.titleTv TODO
        binding.deleteIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list != null && list.size() > 0) {
                    list.remove(position);
                    notifyDataSetChanged();
                }

                if (onDeleteClick != null) {
                    onDeleteClick.delete(bean, position);
                }
            }
        });
    }


    @Override
    public void onMove(int fromPosition, int toPosition) {
        Collections.swap(list, fromPosition, toPosition);//交换数据
        notifyItemMoved(fromPosition, toPosition);
        notifyItemChanged(fromPosition);
        notifyItemChanged(toPosition);

    }
}
