package de.fau.amos.virtualledger.android.views.contacts;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.List;
import java.util.logging.Logger;

import de.fau.amos.virtualledger.R;
import de.fau.amos.virtualledger.android.dagger.App;
import de.fau.amos.virtualledger.dtos.Contact;
import de.fau.amos.virtualledger.android.views.shared.transactionList.DataListening;
import de.fau.amos.virtualledger.android.views.shared.transactionList.Supplier;

/**
 * Created by Simon on 01.07.2017.
 */

public class ContactsFragment extends Fragment implements DataListening {

    private ContactsAdapter adapter;
    private ListView contactListView;
    private Supplier<Contact> contactSupplier;


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        /*((App) getActivity().getApplication()).getNetComponent().inject(this);*/
        super.onActivityCreated(savedInstanceState);

        this.contactSupplier = new ContactsSupplier(getActivity());
        this.contactSupplier.addDataListeningObject(this);
        adapter = new ContactsAdapter(getActivity(), R.id.contacts_list, contactSupplier.getAll());
        contactListView.setAdapter(adapter);
        this.contactSupplier.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.contacts_list, container, false);
        this.contactListView = (ListView) view.findViewById(R.id.contacts_list);
        return view;
    }

    @Override
    public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
        inflater.inflate(R.menu.contacts_add_bar, menu);
    }

    @Override
    public void notifyDataChanged() {
        this.adapter.clear();
        List<Contact> allContacts= this.contactSupplier.getAll();
        if(allContacts != null && allContacts.size() == 0) {
            final Fragment fragment = new NoContactsFragment();
            openFragment(fragment);
        }
        logger().info("Refreshing contacts overview with " + allContacts.size() + " contacts from"+this.contactSupplier);
        this.adapter.addAll(allContacts);
        this.adapter.notifyDataSetChanged();
    }

    private Logger logger() {
        return Logger.getLogger(this.getClass().getCanonicalName() + "{" + this.toString() + "}");
    }

    @Override
    public void onPause() {
        super.onPause();
        this.contactSupplier.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        this.contactSupplier.onResume();
    }

    /**
     * opens a fragment through replacing another fragment
     */
    private void openFragment(final Fragment fragment) {
        if (null != fragment) {
            final FragmentManager manager = getFragmentManager();
            final FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.main_menu_content, fragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    }

}

@@ -157,16 +157,47 @@
 action;%0A
+import android.content.Intent;%0A
 import a
@@ -210,24 +210,24 @@
 .os.Bundle;%0A
-
 import andro
@@ -346,32 +346,62 @@
 w.MenuInflater;%0A
+import android.view.MenuItem;%0A
 import android.v
@@ -567,16 +567,16 @@
 dger.R;%0A
-
 import d
@@ -620,16 +620,97 @@
 er.App;%0A
+import de.fau.amos.virtualledger.android.views.contacts.add.AddContactsActivity;%0A
 import d
@@ -2162,32 +2162,441 @@
 , menu);%0A    %7D%0A%0A
+    @Override%0A    public boolean onOptionsItemSelected(final MenuItem item) %7B%0A        switch (item.getItemId()) %7B%0A            case R.id.contacts_add_bar_add:%0A                final Intent addContactsIntent = new Intent(getActivity(), AddContactsActivity.class);%0A                startActivity(addContactsIntent);%0A                return true;%0A        %7D%0A%0A        return super.onOptionsItemSelected(item);%0A    %7D%0A%0A%0A
     @Override%0A  

add activity of contacts is started if user selects the add icon in the contacts fragment