package com.ttstd.dialer.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.shehuan.niv.NiceImageView;
import com.ttstd.dialer.R;
import com.ttstd.dialer.db.contact.ContactInfo;

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

public class ContactInfoAdapter extends RecyclerView.Adapter<ContactInfoAdapter.ContactInfoHolder> {
    private static final String TAG = "ContactInfoAdapter";

    private FragmentActivity mContext;

    private List<ContactInfo> mContactInfos;

    public void setContactInfos(List<ContactInfo> contactInfos) {
        mContactInfos = contactInfos;
        notifyDataSetChanged();
    }

    public void setItemMoveCallback(ItemMoveCallback itemMoveCallback) {
        mItemMoveCallback = itemMoveCallback;
    }

    private ItemMoveCallback mItemMoveCallback;

    public interface ItemMoveCallback {
        void onItemMove(int fromPosition, int toPosition);

        void onItemRemoved(int position);
    }

    public interface OnClickListener {
        void onClick(ContactInfo contactInfo);
    }

    private OnClickListener mOnClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    @NonNull
    @Override
    public ContactInfoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = (FragmentActivity) parent.getContext();
        return new ContactInfoHolder(LayoutInflater.from(mContext).inflate(R.layout.item_contact, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ContactInfoHolder holder, int position) {
        ContactInfo contactInfo = mContactInfos.get(position);
        String name = contactInfo.getName();
        holder.tv_name.setText(name);
        String phone = contactInfo.getPhoneNumber();
        holder.tv_phone.setText(phone);
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClick: " + contactInfo);
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(contactInfo);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mContactInfos == null ? 0 : mContactInfos.size();
    }


    // 处理拖动交换位置
    public void onItemMove(int fromPosition, int toPosition) {
        Log.e(TAG, "onItemMove: fromPosition = " + fromPosition);
        Log.e(TAG, "onItemMove: toPosition = " + toPosition);
        if (fromPosition == toPosition) {
            return; // 直接返回，不执行后续操作
        }

        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(mContactInfos, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(mContactInfos, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
        if (mItemMoveCallback != null) {
            mItemMoveCallback.onItemMove(fromPosition, toPosition);
        }
    }

    // 处理滑动删除
    public void onItemDismiss(int position) {
//        mContacts.remove(position);
        if (mItemMoveCallback != null) {
            mItemMoveCallback.onItemRemoved(position);
        }
        notifyItemRemoved(position);
    }

    public class ContactInfoHolder extends RecyclerView.ViewHolder {
        ConstraintLayout root;
        NiceImageView nv_avatar;
        TextView tv_name, tv_phone;

        public ContactInfoHolder(@NonNull View itemView) {
            super(itemView);
            root = itemView.findViewById(R.id.root);
            nv_avatar = itemView.findViewById(R.id.nv_avatar);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_phone = itemView.findViewById(R.id.tv_phone);
        }
    }
}
