package com.pixelnx.localhelp.ui.home;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;

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

import android.widget.ArrayAdapter;
import android.widget.ImageView;

import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatRadioButton;
import androidx.recyclerview.widget.RecyclerView;

import com.pixelnx.localhelp.R;
import com.pixelnx.localhelp.model.ModelSearchItem;
import com.pixelnx.localhelp.utils.Consts;
import com.pixelnx.localhelp.utils.widgets.CustomEditText;
import com.pixelnx.localhelp.utils.widgets.CustomTextBold;
import com.pixelnx.localhelp.utils.widgets.CustomTextMedium;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.HolderSearchAdapter> {
   List<ModelSearchItem.Data> subcategoryData;
    Context mContext;
    private HomeInterface homeInterface;


    public SearchAdapter(Context mContext, List<ModelSearchItem.Data> subcategoryData) {
        this.subcategoryData = subcategoryData;
        this.mContext = mContext;
        homeInterface = ((ActivityHome) mContext);
    }

    @NonNull
    @Override
    public HolderSearchAdapter onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_item, viewGroup, false);
        return new HolderSearchAdapter(view);

    }

    @Override
    public void onBindViewHolder(@NonNull final HolderSearchAdapter holder, int i) {

        final ModelSearchItem.Data data = subcategoryData.get(i);
        holder.tvItemName.setText(data.getSubCatName());
        if(data.getStockQnt().equalsIgnoreCase("0")){
            holder.etQty.setText("0");
            holder.etQty.setFocusable(false);
        }else{
            holder.etQty.setFocusable(true);
        holder.etQty.setText("1");}

     holder.etQty.addTextChangedListener(new TextWatcher() {
         @Override
         public void beforeTextChanged(CharSequence charSequence, int p, int i1, int i2) {

         }

         @Override
         public void onTextChanged(CharSequence charSequence, int p, int i1, int i2) {
             if(!holder.etQty.getText().toString().isEmpty()) {

                 if(charSequence.toString().equals("0")){
                     holder.etQty.setError("Enter a valid quantity.");
                     holder.etQty.requestFocus();
                 }else{
                 if(!data.getStockQnt().isEmpty()) {
                     int stcQty = Integer.parseInt(data.getStockQnt());
                     int qty = Integer.parseInt("" + charSequence.toString());
                     if (stcQty < qty) {
                         holder.etQty.setText("1");
                         holder.etQty.setSelection(holder.etQty.getText().length());
                         Toast.makeText(mContext, "Out of stock!!", Toast.LENGTH_SHORT).show();
                     }
                 }}
             }

         }

         @Override
         public void afterTextChanged(Editable editable) {


         }
     });

        if(data.getItemMrp().equalsIgnoreCase("0")){
            holder.tvItemMRP.setVisibility(View.GONE);
        }else{
            holder.tvItemMRP.setVisibility(View.VISIBLE);
        holder.tvItemMRP.setText("MRP : ₹"+data.getItemMrp());}
        if(data.getItemMrp().equalsIgnoreCase("")){
            holder.tvItemMRP.setVisibility(View.GONE);
        }
        holder.tvItemPrise.setText("₹"+data.getRate() + " / " +data.getItemStockWeight()+ data.getQntUnit());

        holder.rbFirst.setText("1" + data.getQntUnit());

        holder.rbTwo.setText("5" + data.getQntUnit());


        if(!data.getStockQnt().isEmpty()){
        int stock= Integer.parseInt(data.getStockQnt());

        if(stock < 1){
        holder.outOfStock.setVisibility(View.VISIBLE);
            ArrayList<String>  strings=new ArrayList<>();
            strings.add("0");

            ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, strings);
            stringArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            holder.spin.setAdapter(stringArrayAdapter);
            holder.spin.setEnabled(false);
        }else{
            holder.spin.setEnabled(true);
            holder.outOfStock.setVisibility(View.GONE);
            if(stock < 5){
                ArrayList<String>  strings=new ArrayList<>();
                for(int s=0;s<stock;s++){
                    int cal=s;
                    cal++;
                    strings.add(""+cal);
                }
                ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, strings);
                stringArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                holder.spin.setAdapter(stringArrayAdapter);
            }else{
                ArrayList<String>  string=new ArrayList<>();
                string.add("1");
                string.add("2");
                string.add("3");
                string.add("4");
                string.add("5");
                ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, string);
                stringArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                holder.spin.setAdapter(stringArrayAdapter);
            }
        }
        }

        Picasso
              .get()
                .load(Consts.Status.BASE_IMAGE_URL + data.getImage())
                .placeholder(R.drawable.noimagecart)
                .into(holder.ivImage);

        holder.tvAddToCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(!holder.etQty.getText().toString().isEmpty()) {
                    if(!holder.etQty.getText().toString().equals("0")) {

                        if (data.getStockQnt().equalsIgnoreCase("0")) {

                            Toast.makeText(mContext,
                                    "Out Of Stock !!!", Toast.LENGTH_SHORT).show();
                        } else {


                            int typ = Integer.parseInt("" + holder.etQty.getText().toString());
                            homeInterface.addToCart(data, typ);

                        }
                    }
                }else{
                    holder.etQty.setError("Enter Quantity");
                }

            }
        });

    }

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

    public static class HolderSearchAdapter extends RecyclerView.ViewHolder {

        CustomTextBold tvAddToCart;
        AppCompatRadioButton rbFirst;
        AppCompatRadioButton rbTwo;
        CustomTextMedium tvItemName;
        CustomTextMedium  tvItemPrise;
        CustomTextMedium  unitTv;
        CustomTextMedium  tvItemMRP;
        RadioGroup rGB;
        ImageView ivImage;
        ImageView outOfStock;
        Spinner spin;
        CustomEditText etQty;

        public HolderSearchAdapter(@NonNull View itemView) {
            super(itemView);
            tvAddToCart = itemView.findViewById(R.id.tvAddToCart);
            rGB = itemView.findViewById(R.id.RGB);
            rbFirst = itemView.findViewById(R.id.rbF);
            ivImage = itemView.findViewById(R.id.ivImage);
            outOfStock = itemView.findViewById(R.id.outpfstock);
            rbTwo = itemView.findViewById(R.id.rbT);
            tvItemName = itemView.findViewById(R.id.tvItemName);
            tvItemPrise = itemView.findViewById(R.id.tvItemPrise);
            unitTv = itemView.findViewById(R.id.unitTv);
            spin = itemView.findViewById(R.id.spin);
            tvItemMRP = itemView.findViewById(R.id.tvItemMRP);
            etQty = itemView.findViewById(R.id.etQty);



        }
    }

}
