package com.cscodetech.moverslorry.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.cscodetech.moverslorry.adepter.MyLoadAdapter;
import com.cscodetech.moverslorry.databinding.FragmentCompletPostBinding;
import com.cscodetech.moverslorry.model.LoadHistoryDataItem;
import com.cscodetech.moverslorry.model.MyLoad;
import com.cscodetech.moverslorry.retrofit.APIClient;
import com.cscodetech.moverslorry.ui.MyLoadDetailsActivity;
import com.cscodetech.moverslorry.utils.CustPrograssbar;
import com.cscodetech.moverslorry.utils.SessionManager;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CompletLoadFragment extends Fragment implements MyLoadAdapter.RecyclerTouchListener {

    private FragmentCompletPostBinding binding;
    SessionManager sessionManager;
    CustPrograssbar custPrograssbar;

    public static CompletLoadFragment newInstance() {
        return new CompletLoadFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentCompletPostBinding.inflate(inflater, container, false);
        View view = binding.getRoot();

        custPrograssbar = new CustPrograssbar();
        sessionManager = new SessionManager(getActivity());
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        binding.recyclerView.setItemAnimator(new DefaultItemAnimator());
        getLoad();
        return view;
    }

    private void getLoad() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("owner_id", sessionManager.getUserDetails().getId());
            jsonObject.put("status", "Complete");

            RequestBody bodyRequest = RequestBody.create(jsonObject.toString(),MediaType.parse("application/json"));
            Call<JsonObject> call = APIClient.getInterface().loadHistory(bodyRequest);
            call.enqueue(new Callback<JsonObject>() {
                @Override
                public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                    custPrograssbar.closePrograssBar();

                    Gson gson = new Gson();
                    MyLoad myLoad = gson.fromJson(response.body().toString(), MyLoad.class);
                    if (myLoad.getLoadHistoryData().size() != 0) {
                        binding.recyclerView.setVisibility(View.VISIBLE);
                        binding.lvlNodata.setVisibility(View.GONE);
                        MyLoadAdapter adapter = new MyLoadAdapter(getActivity(), myLoad.getLoadHistoryData(), CompletLoadFragment.this);
                        binding.recyclerView.setAdapter(adapter);
                    } else {
                        binding.recyclerView.setVisibility(View.GONE);
                        binding.lvlNodata.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onFailure(Call<JsonObject> call, Throwable t) {
                    custPrograssbar.closePrograssBar();
                    call.cancel();
                    t.printStackTrace();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClickLoadPost(LoadHistoryDataItem item, int position) {

        startActivity(new Intent(getActivity(), MyLoadDetailsActivity.class).putExtra("lid", item.getId()));

    }
}
