package hr.fer.labust.diverapp.fragment;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

import hr.fer.labust.diverapp.MainActivity;
import hr.fer.labust.diverapp.R;

public class MessagesFragment extends Fragment {
    private View mView;
    private MainActivity mActivity;
    private ListView mMessagesListView;
    private ArrayAdapter<String> mMessagesListAdapter;

    private static final ArrayList<String> mMessagesList = new ArrayList<>();

    public static void addMessage(String message) {
        mMessagesList.add(message);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (MainActivity)getActivity();
    }

    @Override
    public void onStart() {
        super.onStart();
        mActivity.setPreviousTabIndex();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_messages, container, false);

            mMessagesListView = (ListView)mView.findViewById(R.id.messages_list);
            mMessagesListAdapter = new ArrayAdapter<String>(mActivity.getBaseContext(), android.R.layout.simple_list_item_1, mMessagesList);
            mMessagesListView.setAdapter(mMessagesListAdapter);

            int[] colors = {Color.BLUE, Color.BLUE, Color.BLUE};
            GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.RIGHT_LEFT, colors);
            mMessagesListView.setDivider(gradientDrawable);
            mMessagesListView.setDividerHeight(3);
        }
        else {
            ViewGroup parent = (ViewGroup)mView.getParent();

            if (parent != null) {
                parent.removeView(mView);
            }
        }

        return mView;
    }

    public void update() {
        mMessagesListAdapter.notifyDataSetChanged();
    }
}



