package cc.winboll.studio.contacts.adapters;

/**
 * @Author ZhanGSKen<zhangsken@qq.com>
 * @Date 2025/02/26 13:35:44
 * @Describe ContactAdapter
 */
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import cc.winboll.studio.contacts.R;
import cc.winboll.studio.contacts.beans.ContactModel;
import cc.winboll.studio.contacts.utils.ContactUtils;
import cc.winboll.studio.libaes.views.AOHPCTCSeekBar;
import cc.winboll.studio.libappbase.utils.ToastUtils;
import java.util.List;

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {

    public static final String TAG = "ContactAdapter";

    private static final int REQUEST_CALL_PHONE = 1;

    private List<ContactModel> contactList;
	Context mContext;

    public ContactAdapter(Context context, List<ContactModel> contactList) {
		mContext = context;
        this.contactList = contactList;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
        final ContactModel contact = contactList.get(position);
		holder.llPhoneNumberMain.setOnLongClickListener(new View.OnLongClickListener() {
				@Override
				public boolean onLongClick(View p1) {
					// 弹出复制菜单
					PopupMenu menu = new PopupMenu(mContext, holder.llPhoneNumberMain);
					//加载菜单资源
					menu.getMenuInflater().inflate(R.menu.toolbar_contact_phonenumber, menu.getMenu());
					//设置点击事件的响应
					menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
							@Override
							public boolean onMenuItemClick(MenuItem menuItem) {
								int nItemId = menuItem.getItemId();
								if (nItemId == R.id.item_contact_phonenumber_copy) {
									// Gets a handle to the clipboard service.
									ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
									// Creates a new text clip to put on the clipboard
									ClipData clip = ClipData.newPlainText("simple text", contact.getNumber());
									// Set the clipboard's primary clip.
									clipboard.setPrimaryClip(clip);
									Toast.makeText(mContext, "Copy to clipboard.", Toast.LENGTH_SHORT).show();
								} else if (nItemId == R.id.item_calllog_phonenumber_edit_contact) {
									//ToastUtils.show("Test");
									Long nContactId = ContactUtils.getContactIdByPhone(mContext, contact.getNumber());
									//ToastUtils.show(String.format("%d", nContactId));
									ContactUtils.jumpToEditContact(mContext, contact.getNumber(), nContactId);
								}

								return true;
							}
						});
					//一定要调用show()来显示弹出式菜单
					menu.show();

					return true;
				}
			});
        holder.contactName.setText(contact.getName());
        holder.contactNumber.setText(contact.getNumber());

        // 初始化拉动后拨号控件 
        holder.dialAOHPCTCSeekBar.setThumb(holder.itemView.getContext().getDrawable(R.drawable.ic_call));
        holder.dialAOHPCTCSeekBar.setBlurRightDP(80);
        holder.dialAOHPCTCSeekBar.setThumbOffset(0);
        holder.dialAOHPCTCSeekBar.setOnOHPCListener(
            new AOHPCTCSeekBar.OnOHPCListener(){
                @Override
                public void onOHPCommit() {
                    String phoneNumber = contact.getNumber().replaceAll("\\s", "");
                    ToastUtils.show(phoneNumber);
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    intent.setData(android.net.Uri.parse("tel:" + phoneNumber));
                    // 添加 FLAG_ACTIVITY_NEW_TASK 标志
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    holder.itemView.getContext().startActivity(intent);
                }
            });
    }

    @Override
    public int getItemCount() {
        return contactList.size();
    }

    public class ContactViewHolder extends RecyclerView.ViewHolder {
		LinearLayout llPhoneNumberMain;
        TextView contactName;
        TextView contactNumber;
        AOHPCTCSeekBar dialAOHPCTCSeekBar;

        public ContactViewHolder(@NonNull View itemView) {
            super(itemView);
			llPhoneNumberMain = itemView.findViewById(R.id.itemcontactLinearLayout1);
            contactName = itemView.findViewById(R.id.contact_name);
            contactNumber = itemView.findViewById(R.id.contact_number);
            dialAOHPCTCSeekBar = itemView.findViewById(R.id.aohpctcseekbar_dial);
        }
    }
}

