package com.utopiaindustries.qualitychecker.ui.activities.fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

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

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.utopiaindustries.qualitychecker.R;
import com.utopiaindustries.qualitychecker.apiservice.ApiService;
import com.utopiaindustries.qualitychecker.apiservice.ApiServiceFactory;
import com.utopiaindustries.qualitychecker.models.InspectionReport;
import com.utopiaindustries.qualitychecker.receiver.NetworkReceiver;
import com.utopiaindustries.qualitychecker.service.InspectionReportService;
import com.utopiaindustries.qualitychecker.store.Store;
import com.utopiaindustries.qualitychecker.ui.adapter.ReportAdapter;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ReportsFragment extends Fragment {

    RecyclerView recyclerView;
    ApiService apiService;

    Store store;
    InspectionReportService inspectionReportService;
    TextView emptyReportTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_reports , container, false );

        initializeViews( view );
        // Inflate the layout for this fragment
        return view;
    }

    private void initializeViews( View view ) {

        store = Store.getInstance();
        apiService = ApiServiceFactory.getApiService();
        inspectionReportService = InspectionReportService.getInstance();
        recyclerView = view.findViewById( R.id.reports_recyclerview );
        emptyReportTextView = view.findViewById(R.id.empty_report_text);

        recyclerView.setLayoutManager( new LinearLayoutManager( getContext() ) );

        //Toast.makeText(getContext(), "Reports Fragment", Toast.LENGTH_LONG).show();


    }

    private void refreshReportData(){
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("login_prefs", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", null );
        apiService.fetchAllReports( username ).enqueue(
                new Callback<List<InspectionReport>>() {
                    @Override
                    public void onResponse(Call<List<InspectionReport>> call, Response<List<InspectionReport>> response) {
                        if( response.isSuccessful() && response.body() != null ){
                            System.out.println( response.body().size() );
                            ReportAdapter adapter = new ReportAdapter( response.body() );
                            recyclerView.setAdapter( adapter );
                            if( response.body().isEmpty() )
                                emptyReportTextView.setVisibility( View.VISIBLE ) ;
                        } else {
                            System.out.println("Error");
                            emptyReportTextView.setVisibility( View.VISIBLE ) ;
                        }
                    }
                    @Override
                    public void onFailure(Call<List<InspectionReport>> call, Throwable t) {
                        System.out.println( t.getMessage() );
                        emptyReportTextView.setVisibility( View.VISIBLE ) ;
                    }
                }
        );
    }

    @Override
    public void onResume() {
        super.onResume();

        refreshReportData();
    }

    public void updateData() {
        // Your logic to update fragment data
        System.out.println("Reports-Update");
        refreshReportData();
    }
}