package com.pixelnx.localhelp.ui.order;


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

import java.util.ArrayList;

public class ActivityCancelled extends Activity implements View.OnClickListener {
CustomTextSemiBold heading;
ImageView backIV;
Context mContext;
    SharedPref sharedPref;
    ModelUserData modelUserData;
    RecyclerView rvList;
    ArrayList<ModelNewOrderData.Data> dataOrder;
    CustomTextMedium tvByYou;
    CustomTextMedium tvByCustomer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cancelled);
        mContext=ActivityCancelled.this;
        initial();
    }
    void initial(){
        sharedPref = SharedPref.getInstance(mContext);
        modelUserData = sharedPref.getUser(Consts.Status.SP);
        heading=findViewById(R.id.Heading);
        tvByCustomer=findViewById(R.id.byCustomer);
        tvByCustomer.setOnClickListener(this);

        tvByYou=findViewById(R.id.byYou);
        tvByYou.setOnClickListener(this);
        rvList=findViewById(R.id.rvList);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        backIV=findViewById(R.id.backIv);
        backIV.setOnClickListener(this);
        heading.setText(R.string.cancel_orders);

        if(getIntent().hasExtra("customer")){
            tvByCustomer.setBackgroundResource(R.drawable.back_adapter_orangenwhite);
            apiDelivery("3");
        }else{
            tvByYou.setBackgroundResource(R.drawable.back_adapter_orangenwhite);
            apiDelivery("2");
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.backIv:
                onBackPressed();
                break;
            case R.id.byYou:
                tvByYou.setBackgroundResource(R.drawable.back_adapter_orangenwhite);
                tvByCustomer.setBackgroundResource(R.drawable.back_normal);
                apiDelivery("2");
                break;
            case R.id.byCustomer:
                tvByYou.setBackgroundResource(R.drawable.back_normal);
                tvByCustomer.setBackgroundResource(R.drawable.back_adapter_orangenwhite);
                apiDelivery("3");
                break;
        }

    }
    void apiDelivery(String sts){


        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",""+sts)
                .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(),"C");
                            rvList.setAdapter(adapterOrderList);
                        }else{
                            dataOrder=new ArrayList<>();
                            ProjectUtils.pauseProgressDialog();

                            AdapterOrderList adapterOrderList=new AdapterOrderList(mContext,dataOrder,"C");
                            rvList.setAdapter(adapterOrderList);
                        }
                        ProjectUtils.pauseProgressDialog();
                    }

                    @Override
                    public void onError(ANError anError) {

                        ProjectUtils.pauseProgressDialog();

                    }
                });


    }
}
