package com.creativeapps.schoolbusdriver.ui.activity.main.settings;

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.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.creativeapps.schoolbusdriver.R;
import com.creativeapps.schoolbusdriver.data.Util;
import com.creativeapps.schoolbusdriver.data.network.models.Driver;
import com.creativeapps.schoolbusdriver.data.network.models.Parent;
import com.creativeapps.schoolbusdriver.ui.activity.main.MainActivity;
import com.creativeapps.schoolbusdriver.ui.activity.main.contacts.ParentsAdapter;

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


public class ParentsOrderFragment extends Fragment implements ParentsAdapter.ParentsAdapterListener {
    //adapter used with recyclerView used to display name and telephone numbers of parents
    private ParentsAdapter mAdapter;
    private RecyclerView mRecyclerView;

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


        //inflate the layout
        View view = inflater.inflate(R.layout.fragment_parent_contacts, container, false);
        //get the last saved driver information from the SharedPreference
        Driver driver = Util.getSavedObjectFromPreference(this.getActivity().getApplicationContext(),
                "mPreference", "Driver", Driver.class);
        //define a parent list
        List<Parent> parentList = new ArrayList<>();
        if (driver.getParents() != null) {
            //set the parent list with the parents in the driver object
            parentList = driver.getParents();
        }
        Util.sortParents(parentList, this.getActivity().getApplicationContext());
        //get the adapter
        mAdapter = new ParentsAdapter(parentList, this, true);

        return view;
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // Setup any handles to view objects here
        final LinearLayoutManager manager = new LinearLayoutManager(getActivity());

        //define the recyclerView that used to display the names and phone numbers of parents
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter);

        //define the search view
        view.findViewById(R.id.search).setVisibility(View.GONE);

        ItemTouchHelper.Callback callback =
                new ItemMoveCallback(mAdapter);
        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
        touchHelper.attachToRecyclerView(mRecyclerView);

        ((MainActivity) getActivity()).showHideProgressBar(false);
    }

    @Override
    public void onParentSelected(final Parent parent) {

    }

    @Override
    public void onParentListChanged(List<Parent> mParentListOriginal) {
        String[] parentsTelNumbers = new String[mParentListOriginal.size()];
        //save parentsIds in the SharedPreference
        for (int i = 0; i < mParentListOriginal.size(); i++) {
            parentsTelNumbers[i] = mParentListOriginal.get(i).getTel_number();
        }
        //check if afternoon or morning
        if (Util.isAfternoon(getContext())) {
            Util.saveObjectToSharedPreference(this.getActivity().getApplicationContext(),
                    "mPreference", "afternoonOrderedParentsTelNumbers", parentsTelNumbers);
        } else {
            Util.saveObjectToSharedPreference(this.getActivity().getApplicationContext(),
                    "mPreference", "morningOrderedParentsTelNumbers", parentsTelNumbers);
        }
    }
}