package com.cscodetech.moverslorry.adepter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cscodetech.moverslorry.R;
import com.cscodetech.moverslorry.databinding.FindloadLayoutBinding;
import com.cscodetech.moverslorry.model.LoaddataItem;
import com.cscodetech.moverslorry.retrofit.APIClient;
import com.cscodetech.moverslorry.ui.BidderInfoActivity;
import com.cscodetech.moverslorry.utils.SessionManager;
import com.cscodetech.moverslorry.utils.Utility;

import java.util.List;

public class LoadFindAdapter extends RecyclerView.Adapter<LoadFindAdapter.MyViewHolder> {

    private Context mContext;
    private List<LoaddataItem> loadHistoryDataItems;
    private RecyclerTouchListener listener;
    SessionManager sessionManager;

    public interface RecyclerTouchListener {
        void onClickFindLoad(LoaddataItem item, int position);
        void onClickBidDeletLoad(LoaddataItem item, int position);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private FindloadLayoutBinding binding;

        public MyViewHolder(View view) {
            super(view);

                binding = FindloadLayoutBinding.bind(view);

        }
    }

    public LoadFindAdapter(Context mContext, List<LoaddataItem> typeList, final RecyclerTouchListener listener) {
        this.mContext = mContext;
        this.loadHistoryDataItems = typeList;
        this.listener = listener;
        sessionManager = new SessionManager(mContext);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.findload_layout, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        LoaddataItem item = loadHistoryDataItems.get(position);
        Glide.with(mContext)
                .load(APIClient.BASE_URL + "/" + item.getVehicleImg())
                .thumbnail(Glide.with(mContext).load(R.drawable.tprofile))
                .into(holder.binding.imgt);
        Glide.with(mContext)
                .load(APIClient.BASE_URL + "/" + item.getOwnerImg())
                .thumbnail(Glide.with(mContext).load(R.drawable.tprofile))
                .into(holder.binding.imgs);
        holder.binding.txtTitle.setText("" + item.getVehicleTitle());
        holder.binding.txtPrice.setText(sessionManager.getStringData(SessionManager.currency) + item.getAmount());
        holder.binding.txtTonne.setText("" + item.getWeight());
        holder.binding.txtTonnefix.setText(" /" + item.getAmtType());
        holder.binding.txtPick.setText("" + item.getPickupState());
        holder.binding.txtDrop.setText("" + item.getDropState());
        holder.binding.txtDate.setText("" + Utility.getInstance().parseDateToddMMyy(item.getPostDate()));
        holder.binding.txtName.setText("" + item.getOwnerName());
        holder.binding.txtRates.setText("" + item.getOwnerRating());
        holder.binding.txtMaterial.setText("" + item.getMaterialName());
        holder.binding.txtDist.setText("" + item.getLoadDistance());

        if (item.getIsBid() == 0) {
            holder.binding.txtBooknow.setVisibility(View.VISIBLE);
            holder.binding.imgDelete.setVisibility(View.GONE);
        } else if (item.getIsBid() == 1) {
            holder.binding.txtBooknow.setVisibility(View.GONE);
            holder.binding.imgDelete.setVisibility(View.VISIBLE);
            holder.binding.txtTonnefixb.setText(" /" + item.getBidAmountType());
            holder.binding.txtPriceb.setText(sessionManager.getStringData(SessionManager.currency) + item.getBidAmount());
            holder.binding.txtTonnefixb.setVisibility(View.VISIBLE);
            holder.binding.txtPriceb.setVisibility(View.VISIBLE);
        } else if (item.getIsBid() == 2) {
            holder.binding.txtBooknow.setVisibility(View.GONE);
            holder.binding.imgDelete.setVisibility(View.GONE);
            holder.binding.txtTonnefixb.setText(" /" + item.getBidAmountType());
            holder.binding.txtPriceb.setText(sessionManager.getStringData(SessionManager.currency) + item.getBidAmount());
        } else if (item.getIsBid() == 3) {
            holder.binding.txtBooknow.setVisibility(View.GONE);
            holder.binding.imgDelete.setVisibility(View.VISIBLE);
            holder.binding.txtTonnefixb.setVisibility(View.VISIBLE);
            holder.binding.txtTonnefixb.setText("Rejected");
        }

        holder.binding.txtBooknow.setOnClickListener(view -> {
            if (item.getIsBid() == 0) listener.onClickFindLoad(item, position);
        });

        holder.binding.imgDelete.setOnClickListener(view -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setMessage("Do you want to remove bid ?");
            builder.setTitle("Remove !");
            builder.setCancelable(false);
            builder.setPositiveButton("Yes", (dialog, which) -> {
                dialog.cancel();
                listener.onClickBidDeletLoad(item, position);
            });
            builder.setNegativeButton("No", (dialog, which) -> dialog.cancel());
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        });

        holder.binding.txtPick.setOnClickListener(view -> displayAddressWindow(holder.binding.txtPick, item.getPickupPoint()));
        holder.binding.txtDrop.setOnClickListener(view -> displayAddressWindow(holder.binding.txtDrop, item.getDropPoint()));
        holder.binding.imgs.setOnClickListener(view -> mContext.startActivity(new Intent(mContext, BidderInfoActivity.class)
                .putExtra("uid", item.getUid())));
    }

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

    private void displayAddressWindow(View anchorView, String address) {
        Activity activity = (Activity) mContext;
        PopupWindow popup = new PopupWindow(mContext);
        View layout = activity.getLayoutInflater().inflate(R.layout.popup_content, null);
        TextView textView = layout.findViewById(R.id.txt_address);
        textView.setText("" + address);
        popup.setContentView(layout);
        popup.setHeight(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        popup.setWidth(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        popup.setOutsideTouchable(true);
        popup.setFocusable(true);
        popup.setBackgroundDrawable(new ColorDrawable());
        popup.showAsDropDown(anchorView);
    }

    public void bidSenditem(int pos) {
        LoaddataItem item = loadHistoryDataItems.get(pos);
        item.setIsBid(1);
        loadHistoryDataItems.set(pos, item);
        notifyDataSetChanged();
    }

    public void bidRemoveitem(int pos) {
        Log.e("jfjfj", "ljalkfjasklfj" + pos);
        LoaddataItem item = loadHistoryDataItems.get(pos);
        item.setIsBid(0);
        loadHistoryDataItems.set(pos, item);
        notifyDataSetChanged();
    }
}