package com.pixelnx.localhelp.ui.home;


import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;


import android.content.Context;

import android.content.Intent;

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.ModelShopListData;
import com.pixelnx.localhelp.model.ModelUserData;
import com.pixelnx.localhelp.spash.Database;
import com.pixelnx.localhelp.spash.Modellogo;
import com.pixelnx.localhelp.ui.base.ActivityBase;
import com.pixelnx.localhelp.ui.user.ActivityProfileSetting;
import com.pixelnx.localhelp.utils.Consts;
import com.pixelnx.localhelp.utils.sharedpref.SharedPref;

import com.pixelnx.localhelp.utils.widgets.CustomEditText;
import com.pixelnx.localhelp.utils.widgets.CustomTextExtraBold;
import com.pixelnx.localhelp.utils.widgets.CustomTextMedium;




import java.util.ArrayList;

public class ActivityHome extends ActivityBase implements  View.OnClickListener {
    private Context mContext;
    private RecyclerView rvItem;
    private CustomEditText etSearch;
    CardView rvOrderRes;
    SharedPref sharedPref;
    ModelUserData modelUserData;
    CustomTextMedium noSearch;

    ImageView profileIv;
    ImageView sideDrawer;
    ImageView shopIv;
    CustomTextExtraBold heading;
    String catId="";
    ModelShopListData.Data data;
    ArrayList<ModelSearchItem.Data> subcategoryData;
    Database dataBase;
    Modellogo modellogo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentBaseView(R.layout.activity_home);
        mContext = ActivityHome.this;

        sharedPref = SharedPref.getInstance(mContext);
        modelUserData = sharedPref.getUser(Consts.Status.SP);
        profileIv=findViewById(R.id.profileIv);
        rvOrderRes = findViewById(R.id.rvorderRes);
        rvOrderRes.setVisibility(View.VISIBLE);


        profileIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, ActivityProfileSetting.class);
                startActivity(intent);

            }
        });



        initial();
    }




    private void initial() {

        if(getIntent().hasExtra("data")){
            data= (ModelShopListData.Data) getIntent().getSerializableExtra("data");
            vendorOrStoreId=data.getUserId();

        }
        if(getIntent().hasExtra("catId")){
            catId=getIntent().getStringExtra("catId");

        }
        heading = findViewById(R.id.Heading);

        dataBase = Room.databaseBuilder
                (getApplicationContext(),
                        Database.class, "database-name").fallbackToDestructiveMigration()
                .allowMainThreadQueries().build();
        modellogo=new Modellogo();
        modellogo= dataBase.daoAccess().getAllSetData();

        if(!modellogo.getLogoTitle().isEmpty()){
            heading.setText(""+modellogo.getLogoTitle());}

        rvItem = findViewById(R.id.rvItem);
        noSearch = findViewById(R.id.noSearch);
        shopIv = findViewById(R.id.shopIv);
        shopIv.findViewById(R.id.shopIv);
        shopIv.setOnClickListener(this);

        sideDrawer = findViewById(R.id.sideDrawer);
        sideDrawer.setOnClickListener(this);
        searchItem("");

        if(getIntent().hasExtra("tag")){
            heading.setText(""+getIntent().getStringExtra("tag"));

        }



        etSearch = findViewById(R.id.etSearch);
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().length() > 1) {

                    searchItem(s.toString());

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });





    }


    private void searchItem(String toString) {



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

                    subcategoryData=new ArrayList<>();
                        if ("1".equalsIgnoreCase(response.getStatus())) {
                            noSearch.setVisibility(View.GONE);
                            rvItem.setLayoutManager(new LinearLayoutManager(mContext));
                            SearchAdapter searchAdapter = new SearchAdapter(mContext, response.getSubCategoryData());
                            rvItem.setAdapter(searchAdapter);
                        } else {
                            noSearch.setVisibility(View.VISIBLE);

                            SearchAdapter searchAdapter = new SearchAdapter(mContext, subcategoryData);
                            rvItem.setAdapter(searchAdapter);
                        }

                    }

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

    }









    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.sideDrawer) {
            onBackPressed();
        } else if (id == R.id.shopIv) {
            congratsDialog();
        }
    }
}
