package cc.winboll.studio.contacts.adapters;

/**
 * @Author ZhanGSKen<zhangsken@qq.com>
 * @Date 2025/02/26 13:09:32
 * @Describe CallLogAdapter
 */
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.Button;
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.CallLogModel;
import cc.winboll.studio.contacts.utils.ContactUtils;
import cc.winboll.studio.libaes.views.AOHPCTCSeekBar;
import com.hjq.toast.ToastUtils;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class CallLogAdapter extends RecyclerView.Adapter<CallLogAdapter.CallLogViewHolder> {
    public static final String TAG = "CallLogAdapter";

    private List<CallLogModel> callLogList;
    ContactUtils mContactUtils;
    Context mContext;

    public CallLogAdapter(Context context, List<CallLogModel> callLogList) {
        mContext = context;
        this.mContactUtils = ContactUtils.getInstance(mContext);
        this.callLogList = callLogList;
    }
	
	public void relaodContacts() {
		this.mContactUtils.relaodContacts();
	}

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

    @Override
    public void onBindViewHolder(@NonNull CallLogViewHolder holder, int position) {
        final CallLogModel callLog = callLogList.get(position);
        holder.phoneNumber.setText(callLog.getPhoneNumber() + "☎" + mContactUtils.getContactsName(callLog.getPhoneNumber()));
		holder.phoneNumber.setOnLongClickListener(new View.OnLongClickListener() {
				@Override
				public boolean onLongClick(View p1) {
					// 弹出复制菜单
					PopupMenu menu = new PopupMenu(mContext, holder.phoneNumber);
					//加载菜单资源
					menu.getMenuInflater().inflate(R.menu.toolbar_calllog_phonenumber, menu.getMenu());
					//设置点击事件的响应
					menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
							@Override
							public boolean onMenuItemClick(MenuItem menuItem) {
								int nItemId = menuItem.getItemId();
								if (nItemId == R.id.item_calllog_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", callLog.getPhoneNumber());
									// Set the clipboard's primary clip.
									clipboard.setPrimaryClip(clip);
									Toast.makeText(mContext, "Copy to clipboard.", Toast.LENGTH_SHORT).show();
								}

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

					return true;
				}
			});
		
        holder.callStatus.setText(callLog.getCallStatus());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        holder.callDate.setText(dateFormat.format(callLog.getCallDate()));

        // 初始化拉动后拨号控件 
        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 = callLog.getPhoneNumber().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 callLogList.size();
    }

    public class CallLogViewHolder extends RecyclerView.ViewHolder {
        TextView phoneNumber, callStatus, callDate;
        Button dialButton;
        AOHPCTCSeekBar dialAOHPCTCSeekBar;

        public CallLogViewHolder(@NonNull View itemView) {
            super(itemView);
            phoneNumber = itemView.findViewById(R.id.phone_number);
            callStatus = itemView.findViewById(R.id.call_status);
            callDate = itemView.findViewById(R.id.call_date);
            dialAOHPCTCSeekBar = itemView.findViewById(R.id.aohpctcseekbar_dial);
        }
    }

}

