package com.pixelnx.localhelp.ui.storeitems;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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


import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.ParsedRequestListener;


import com.pixelnx.localhelp.R;
import com.pixelnx.localhelp.model.ModelSearchItem;
import com.pixelnx.localhelp.model.ModelUserData;

import com.pixelnx.localhelp.utils.Consts;
import com.pixelnx.localhelp.utils.ProjectUtils;
import com.pixelnx.localhelp.utils.sharedpref.SharedPref;
import com.pixelnx.localhelp.utils.widgets.CustomEditText;


public class ActivityItemList extends AppCompatActivity implements View.OnClickListener {
Context mContext;
ImageView backIv;

    private RecyclerView rvItem;
   SharedPref sharedPref;
    ModelUserData modelUserData;
   static String userId="";
    CustomEditText etSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_list);
        mContext=ActivityItemList.this;
        initial();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(!userId.isEmpty()){
            searchItem("");

        }
    }

    void initial(){
        backIv=findViewById(R.id.backIv);
        backIv.setOnClickListener(this);
        rvItem=findViewById(R.id.rvItem);
        etSearch=findViewById(R.id.etSearch);
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                searchItem(""+charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        sharedPref = SharedPref.getInstance(mContext);
        modelUserData = sharedPref.getUser(Consts.Status.SP);
        userId=modelUserData.getData().getUserId();
        ProjectUtils.showProgressDialog(mContext, false, "Loading...");
        searchItem("");
    }
    private void searchItem(String toString) {




        AndroidNetworking.post(Consts.Status.BASE_URL + Consts.Status.SEARCH_API)
                .addBodyParameter("itemName", toString)
                .addBodyParameter("vendorId", ""+userId)
                .setPriority(Priority.IMMEDIATE)
                .setTag(Consts.Status.SEARCH_API)
                .build()
                .getAsObject(ModelSearchItem.class, new ParsedRequestListener<ModelSearchItem>() {
                    @Override
                    public void onResponse(ModelSearchItem response) {
                             ProjectUtils.pauseProgressDialog();

                        if ("1".equalsIgnoreCase(response.getStatus())) {

                            rvItem.setLayoutManager(new LinearLayoutManager(mContext));
                           AdapterItemList searchAdapter = new AdapterItemList(mContext, response.getSubCategoryData());
                            rvItem.setAdapter(searchAdapter);
                        } else {

                             Toast.makeText(mContext, "Item not Found.", Toast.LENGTH_SHORT).show();

                        }
                        ProjectUtils.pauseProgressDialog();
                    }

                    @Override
                    public void onError(ANError anError) {
                        ProjectUtils.pauseProgressDialog();
                        Toast.makeText(mContext, anError.getErrorDetail(), Toast.LENGTH_SHORT).show();
                    }
                });

    }
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.backIv) {
            onBackPressed();
        }
    }
}
