package com.pixelnx.localhelp.ui.user;


import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

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.ModelOrderListData;
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.CustomTextSemiBold;

import java.util.ArrayList;

public class ActivityPreviousOrderList extends Activity {
    private RecyclerView rvItem;
    private Context mContext;
    ImageView backIv;
    ArrayList<ModelOrderListData.Data> dataArrayList;
    SharedPref sharedPref;
    ModelUserData modelUserData;
    CustomTextSemiBold noData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_previous_order_list);
        backIv = findViewById(R.id.backIv);
        noData= findViewById(R.id.nodata);

        mContext = ActivityPreviousOrderList.this;
        dataArrayList = new ArrayList<>();
        sharedPref = SharedPref.getInstance(mContext);
        modelUserData = sharedPref.getUser(Consts.Status.SP);

        rvItem = findViewById(R.id.rvItem);
        rvItem.setLayoutManager(new LinearLayoutManager(mContext));

        backIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        ProjectUtils.showProgressDialog(mContext, false, "Loading...");
        if (ProjectUtils.checkConnection(mContext)) {
            initial();
        } else {
            Toast.makeText(mContext, "No internet connection...Try again! ", Toast.LENGTH_SHORT).show();


        }


    }

    void initial() {


        AndroidNetworking.post(Consts.Status.BASE_URL + Consts.Status.GET_ALL_ORDERS_API)
                .addBodyParameter("start", "" + 0)
                .addBodyParameter("limit", "" + 100)
                .addBodyParameter("user_id", "" + modelUserData.getData().getUserId())
                .setPriority(Priority.IMMEDIATE)
                .build()
                .getAsObject(ModelOrderListData.class, new ParsedRequestListener<ModelOrderListData>() {
                    @Override
                    public void onResponse(ModelOrderListData response) {

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


                        AdapterHistoryOrder adapterHistoryOrder=new AdapterHistoryOrder(mContext,response.getData());
                        rvItem.setAdapter(adapterHistoryOrder);
                     }else {

                        Toast.makeText(mContext,"No Order Found.",Toast.LENGTH_SHORT).show();

                            }

                        ProjectUtils.pauseProgressDialog(); }

                    @Override
                    public void onError(ANError anError) {
                        ProjectUtils.pauseProgressDialog();

                    }
                });


    }

    @Override
    protected void onResume() {
        initial();
        super.onResume();
    }
}
