package com.pixelnx.localhelp.ui.order;


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

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

import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;

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.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;
import com.pixelnx.localhelp.utils.widgets.CustomTextSemiBold;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class ActivityDelivered extends Activity implements View.OnClickListener {
ImageView backIv;
Context mContext;

    SharedPref sharedPref;
    ModelUserData modelUserData;
    RecyclerView rvList;
    ArrayList<ModelNewOrderData.Data> dataOrder;
    CustomTextSemiBold noData;
    LinearLayout openDatePick;
    DatePickerDialog.OnDateSetListener datePick;
    Calendar myCalendar;
    CustomEditText etSearch;
    String dateSelect="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delivered);
        mContext=ActivityDelivered.this;
        backIv=findViewById(R.id.backIv);
        noData=findViewById(R.id.nodata);
        etSearch=findViewById(R.id.etSearch);
        etSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, datePick, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH));

                datePickerDialog.show();
            }
        });
        backIv.setOnClickListener(this);
        sharedPref = SharedPref.getInstance(mContext);
        modelUserData = sharedPref.getUser(Consts.Status.SP);
        rvList=findViewById(R.id.rvList);
        openDatePick=findViewById(R.id.opendatepick);
        openDatePick.setOnClickListener(this);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        myCalendar = Calendar.getInstance();

        datePick = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {

                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                String myFormat = "dd/MM/yyyy";
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat);

                dateSelect=""+sdf.format(myCalendar.getTime());
                etSearch.setText("" + sdf.format(myCalendar.getTime()));
apiDelivery();
            }

        };
        apiDelivery();
    }

    void apiDelivery(){


        ProjectUtils.showProgressDialog(mContext, false, "Loading...");

        AndroidNetworking.post(Consts.Status.BASE_URL +Consts.Status.API_DELIVERY_ORDER)
                .addBodyParameter("vendor_id",""+modelUserData.getData().getUserId())
                .addBodyParameter("delivery_status","1")
                .addBodyParameter("delivery_date",""+dateSelect)
                .setPriority(Priority.IMMEDIATE)
                .setTag(Consts.Status.SEARCH_API)
                .build()
                .getAsObject(ModelNewOrderData.class, new ParsedRequestListener<ModelNewOrderData>() {
                    @Override
                    public void onResponse(ModelNewOrderData response) {

                        if(response.getStatus().equalsIgnoreCase("1")){
                        dataOrder=new ArrayList<>();
                        ProjectUtils.pauseProgressDialog();

                        AdapterOrderList adapterOrderList=new AdapterOrderList(mContext,response.getOrderData(),"D");
                        rvList.setAdapter(adapterOrderList);
                        }else{
                            dataOrder=new ArrayList<>();
                            noData.setVisibility(View.VISIBLE);
                            AdapterOrderList adapterOrderList=new AdapterOrderList(mContext,dataOrder,"D");
                            rvList.setAdapter(adapterOrderList);
                        }
                        ProjectUtils.pauseProgressDialog();
                    }

                    @Override
                    public void onError(ANError anError) {

                        ProjectUtils.pauseProgressDialog();

                    }
                });


    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.backIv) {
            onBackPressed();
        } else if (id == R.id.opendatepick) {
            DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, datePick, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH));

            datePickerDialog.show();
        }

    }
}
