package com.ericsson.uecontrol.gui.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.ericsson.uecontrol.R;
import com.ericsson.uecontrol.core.UeControlExecutor;
import com.ericsson.uecontrol.gui.MainActivity;
import com.ericsson.uecontrol.gui.util.BehaviourListAdapter;

import java.util.ArrayList;

/**
 * This fragment will show a read only list of defined UeBehaviours
 */
public class BehaviourListFragment extends Fragment {

    /**
     * The fragment's ListView/GridView.
     */
    private AdapterView listView;

    /**
     * The Adapter which will be used to populate the ListView/GridView with
     * Views.
     */
    private BehaviourListAdapter adapter;
    private UeControlExecutor executor;


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

        if(MainActivity.getExecutor() != null) {
            UeControlExecutor executor = MainActivity.getExecutor();
            adapter = new BehaviourListAdapter(getActivity(), executor.getBehaviourList());
        }
        else
            adapter = new BehaviourListAdapter(getActivity(), new ArrayList());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item, container, false);

        // Set the adapter
        listView = (AdapterView) view.findViewById(android.R.id.list);
        listView.setAdapter(adapter);

        setEmptyText("No Behaviours Defined");

        return view;
    }

    @Override
    public void onResume() {
        if(adapter != null) {
            adapter.generateIds();
            adapter.notifyDataSetChanged();
        }
        super.onResume();
    }


    /**
     * The default content for this Fragment has a TextView that is shown when
     * the list is empty. If you would like to change the text, call this method
     * to supply the text it should use.
     */
    public void setEmptyText(CharSequence emptyText) {
        View emptyView = listView.getEmptyView();

        if (emptyText instanceof TextView) {
            ((TextView) emptyView).setText(emptyText);
        }
    }

}
