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

import android.content.Context;
import android.content.Intent;
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.db.ReportRepository;
import com.utopiaindustries.qualitychecker.models.InspectionReport;
import com.utopiaindustries.qualitychecker.models.InspectionReportWrapper;
import com.utopiaindustries.qualitychecker.store.Store;
import com.utopiaindustries.qualitychecker.ui.activities.MainActivity;
import com.utopiaindustries.qualitychecker.ui.adapter.DraftReportAdapter;
import com.utopiaindustries.qualitychecker.ui.adapter.ReportAdapter;
import com.utopiaindustries.qualitychecker.utils.FileUtils;
import com.utopiaindustries.qualitychecker.utils.SelectListener;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.List;

public class DraftFragment extends Fragment implements SelectListener {

    RecyclerView recyclerView;
    TextView emptyReportTextView;
    private Store store;

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

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

        initializeViews( view );

        return view;
    }

    private void initializeViews( View view ) {
        recyclerView = view.findViewById( R.id.draft_recyclerview );
        emptyReportTextView = view.findViewById(R.id.empty_report_text);

        recyclerView.setLayoutManager( new LinearLayoutManager( getContext() ) );
        store = Store.getInstance();

        refreshReportData();
    }

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

        refreshReportData();
    }

    public void refreshReportData() {
        ReportRepository repository = new ReportRepository( getContext() );
        List<InspectionReportWrapper> reportWrappers = repository.findAllDraftReports();
        System.out.println(reportWrappers.size());

        DraftReportAdapter adapter = new DraftReportAdapter( reportWrappers, this );
        recyclerView.setAdapter( adapter );

        if(reportWrappers.isEmpty()) {
            emptyReportTextView.setVisibility( View.VISIBLE ) ;
        }
        else {
            emptyReportTextView.setVisibility( View.INVISIBLE ) ;
        }


    }

    @Override
    public void onItemClicked(InspectionReportWrapper reportWrapper) {
        //Toast.makeText(getContext(), String.valueOf(reportWrapper.getId()), Toast.LENGTH_SHORT).show();

        SharedPreferences sharedPreferences = getContext().getSharedPreferences("login_prefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putString("draftReportId", String.valueOf(reportWrapper.getId()));
        editor.apply();

        Intent intent = new Intent( getContext(), MainActivity.class );
        intent.putExtra("fromClass", "Draft");
        intent.putExtra("draftID", String.valueOf(reportWrapper.getId()));
        startActivity( intent );
    }


}