package com.renitha.Fragments;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.renitha.Adapter.RecyclerUserComplaintCompletedAdapter;
import com.renitha.Adapter.RecyclerUserComplaintRejectedAdapter;
import com.renitha.Interface.APIService;
import com.renitha.Model.APIUrl;
import com.renitha.Model.SetterComplaintDetails;
import com.renitha.Pref.Pref;
import com.renitha.R;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ViewUserComplaintsRejectedList extends Fragment {
    RecyclerView recyclerView;
    RecyclerView.LayoutManager recyclerViewlayoutManager;
    ArrayList<SetterComplaintDetails> listing;
    RecyclerUserComplaintRejectedAdapter recyclerAdapter;
    ImageView lt;
    LinearLayout linear_layout1,linear_layout2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View rootView       =   inflater.inflate(R.layout.activity_complaint_user_list, container, false);

        recyclerView        =   (RecyclerView)rootView.findViewById(R.id.recyclerView);
        linear_layout1      =   (LinearLayout)rootView.findViewById(R.id.linear_layout1);
        linear_layout2      =   (LinearLayout)rootView.findViewById(R.id.linear_layout2);
        lt                  =   (ImageView)rootView.findViewById(R.id.lt);
        recyclerView.setHasFixedSize(true);
        recyclerViewlayoutManager   =   new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(recyclerViewlayoutManager);
        listing = new ArrayList<>();

        JSON_GET_DATA();
        return rootView;
    }
    private void JSON_GET_DATA()
    {
        final ProgressDialog progressDialog = new ProgressDialog(getContext());
        progressDialog.setMessage("Wait...");
        progressDialog.show();
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // set your desired log level
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        // add your other interceptors …
        // add logging as last interceptor
        httpClient.addInterceptor(logging);  // <-- this is the important line

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(APIUrl.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build();
        //Toast.makeText(MyAppointmentActivity.this, selected_date + " selected!", Toast.LENGTH_SHORT).show();

        APIService service = retrofit.create(APIService.class);
        Call<List<SetterComplaintDetails>> call = service.getUserComplaintList(Pref.getInstance(getContext()).getLoginId(),Pref.getInstance(getContext()).getLoginType(),"rejected");
        call.enqueue(new Callback<List<SetterComplaintDetails>>()
        {
            @Override
            public void onResponse(Call<List<SetterComplaintDetails>> call, Response<List<SetterComplaintDetails>> response)
            {
                progressDialog.dismiss();
                Log.d("category", response.body() + "");
                List<SetterComplaintDetails> list = response.body();
                SetterComplaintDetails product = null;
                ///list.clear();
                listing.clear();
                if (list.size() != 0)
                {
                    linear_layout2.setVisibility(View.VISIBLE);
                    linear_layout1.setVisibility(View.GONE);
                    for (int i = 0; i < list.size(); i++)
                    {
                        Log.d("in For Loop", i + "");
                        product = new SetterComplaintDetails();
                        String tbl_ticket_entry_id		=   list.get(i).getTbl_ticket_entry_id();
                        String ticket_no            	=   list.get(i).getTicket_no();
                        String ticket_date              =   list.get(i).getTicket_date();
                        String complaint_type           =   list.get(i).getComplaint_type();
                        String serialno                 =   list.get(i).getSerialno();
                        String category_name            =   list.get(i).getCategory_name();
                        String product_id               =   list.get(i).getProduct_id();
                        String make                     =   list.get(i).getMake();
                        String model                    =   list.get(i).getModel();
                        String ticket_priority          =   list.get(i).getTicket_priority();
                        String equp_cond                =   list.get(i).getEqup_cond();
                        String remark                   =   list.get(i).getRemark();
                        String assign_date              =   list.get(i).getAssign_date();
                        String completed_date           =   list.get(i).getCompleted_date();
                        String flag                     =   list.get(i).getFlag();
                        String assign_flag              =   list.get(i).getAssign_flag();
                        String user_name                =   list.get(i).getUser_name();
                        String user_contact             =   list.get(i).getUser_contact();
                        String user_email               =   list.get(i).getUser_email();
                        String cust_name                =   list.get(i).getCust_name();
                        String cust_contact             =   list.get(i).getCust_contact();
                        String cust_email               =   list.get(i).getCust_email();
                        String hospital_name            =   list.get(i).getHospital_name();
                        String hospital_address         =   list.get(i).getHospital_address();

                        product.setTbl_ticket_entry_id(tbl_ticket_entry_id);
                        product.setTicket_no(ticket_no);
                        product.setTicket_date(ticket_date);
                        product.setComplaint_type(complaint_type);
                        product.setSerialno(serialno);
                        product.setCategory_name(category_name);
                        product.setProduct_id(product_id);
                        product.setMake(make);
                        product.setModel(model);

                        product.setTicket_priority(ticket_priority);
                        product.setEqup_cond(equp_cond);
                        product.setRemark(remark);
                        product.setAssign_date(assign_date);
                        product.setCompleted_date(completed_date);
                        product.setFlag(flag);
                        product.setAssign_flag(assign_flag);
                        product.setUser_name(user_name);
                        product.setUser_contact(user_contact);
                        product.setUser_email(user_email);
                        product.setCust_name(cust_name);
                        product.setCust_contact(cust_contact);
                        product.setCust_email(cust_email);
                        product.setHospital_name(hospital_name);
                        product.setHospital_address(hospital_address);

                        listing.add(product);
                    }

                    recyclerAdapter                         =   new RecyclerUserComplaintRejectedAdapter(listing, getContext());
                    RecyclerView.LayoutManager recyce       =   new LinearLayoutManager(getContext());

                    recyclerView.setLayoutManager(recyce);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                    recyclerView.setAdapter(recyclerAdapter);
                    recyclerAdapter.notifyDataSetChanged();
                }
                else
                {
                    linear_layout2.setVisibility(View.GONE);
                    linear_layout1.setVisibility(View.VISIBLE);
                    // showAlert("No record found!!!");
                }
            }
            @Override
            public void onFailure(Call<List<SetterComplaintDetails>> call, Throwable t) {
                progressDialog.dismiss();
            }
        });
    }

}
