package com.datarecovery.master.module.order;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.datarecovery.master.R;
import com.datarecovery.master.data.api.bean.OrderBean;
import com.datarecovery.master.databinding.ItemOrderBinding;
import com.datarecovery.master.utils.ClipboardUtil;
import com.datarecovery.master.utils.ToastUtil;

import java.util.List;
import java.util.Objects;

public class OrderItemAdapter extends RecyclerView.Adapter<OrderItemAdapter.ViewHolder> {


    private final AsyncListDiffer<OrderBean> listDiffer;
    private final LifecycleOwner lifecycleOwner;

    public OrderItemAdapter(LifecycleOwner lifecycleOwner) {
        this.lifecycleOwner = lifecycleOwner;
        listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<OrderBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull OrderBean oldItem, @NonNull OrderBean newItem) {
                return oldItem.getId() == newItem.getId();
            }

            @Override
            public boolean areContentsTheSame(@NonNull OrderBean oldItem, @NonNull OrderBean newItem) {
                if (!Objects.equals(oldItem.getOutTradeNo(), newItem.getOutTradeNo())) {
                    return false;
                }
                if (!Objects.equals(oldItem.getItemName(), newItem.getItemName())) {
                    return false;
                }
                if (!Objects.equals(oldItem.getAmount(), newItem.getAmount())) {
                    return false;
                }
                if (!Objects.equals(oldItem.getAuthCode(), newItem.getAuthCode())) {
                    return false;
                }
                if (!Objects.equals(oldItem.isPermanent(), newItem.isPermanent())) {
                    return false;
                }
                if (!Objects.equals(oldItem.getTutorialLink(), newItem.getTutorialLink())) {
                    return false;
                }
                return true;
            }
        });

    }

    @NonNull
    @Override
    public OrderItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemOrderBinding binding = ItemOrderBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderItemAdapter.ViewHolder holder, int position) {
        holder.bind(listDiffer.getCurrentList().get(position));
    }

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

    public void submit(List<OrderBean> list) {
        listDiffer.submitList(list);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemOrderBinding binding;

        public ViewHolder(@NonNull ItemOrderBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.setLifecycleOwner(lifecycleOwner);
            binding.tvCopyOrder.setOnClickListener(v -> {
                OrderBean orderBean = binding.getOrderBean();
                ClipboardUtil.copy(orderBean.getOutTradeNo());
                ToastUtil.show(R.string.copy_success, ToastUtil.LENGTH_SHORT);
            });
            binding.tvCopyLink.setOnClickListener(v -> {
                OrderBean orderBean = binding.getOrderBean();
                ClipboardUtil.copy(orderBean.getTutorialLink());
                ToastUtil.show(R.string.copy_success, ToastUtil.LENGTH_SHORT);
            });
            binding.tvAuthCodeCopy.setOnClickListener(v -> {
                OrderBean orderBean = binding.getOrderBean();
                ClipboardUtil.copy(orderBean.getAuthCode());
                ToastUtil.show(R.string.copy_success, ToastUtil.LENGTH_SHORT);
            });
        }

        public void bind(OrderBean orderBean) {
            binding.setOrderBean(orderBean);
        }
    }


}
